Java Code Examples for javax.crypto.SecretKeyFactory#translateKey()
The following examples show how to use
javax.crypto.SecretKeyFactory#translateKey() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PBKDF2Translate.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean generateAndTranslateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key2 = skf.translateKey(key1); // check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("generateAndTranslateKey test case failed: the " + "key1 and key2 values in its primary encoding format are " + "not the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 2
Source File: PBKDF2Translate.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. * * @return true if InvalidKeyException occurred; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public boolean translateSpoiledKey() throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); try { SecretKey key2 = skf.translateKey(key1); } catch (InvalidKeyException ike) { // this is expected return true; } return false; }
Example 3
Source File: PBKDF2TranslateTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * */ public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key2 = skf.translateKey(key1); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm."); } }
Example 4
Source File: PBKDF2TranslateTest.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * */ public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key2 = skf.translateKey(key1); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm."); } }
Example 5
Source File: PBKDF2Translate.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. * * @return true if InvalidKeyException occurred; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public boolean translateSpoiledKey() throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); try { SecretKey key2 = skf.translateKey(key1); } catch (InvalidKeyException ike) { // this is expected return true; } return false; }
Example 6
Source File: PBKDF2Translate.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean generateAndTranslateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key2 = skf.translateKey(key1); // check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("generateAndTranslateKey test case failed: the " + "key1 and key2 values in its primary encoding format are " + "not the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 7
Source File: PBKDF2TranslateTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. */ public void translateSpoiledKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(salt); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); try { skf.translateKey(key1); throw new RuntimeException( "translateSpoiledKey test case failed, should throw" + " InvalidKeyException when spoil the key"); } catch (InvalidKeyException ike) { out.println("Expected exception when spoil the key"); } }
Example 8
Source File: PBKDF2Translate.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean generateAndTranslateKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key2 = skf.translateKey(key1); // check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("generateAndTranslateKey test case failed: the " + "key1 and key2 values in its primary encoding format are " + "not the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 9
Source File: PBKDF2TranslateTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. */ public void translateSpoiledKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(salt); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); try { skf.translateKey(key1); throw new RuntimeException( "translateSpoiledKey test case failed, should throw" + " InvalidKeyException when spoil the key"); } catch (InvalidKeyException ike) { out.println("Expected exception when spoil the key"); } }
Example 10
Source File: PBKDF2TranslateTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. */ public void translateSpoiledKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(salt); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); try { skf.translateKey(key1); throw new RuntimeException( "translateSpoiledKey test case failed, should throw" + " InvalidKeyException when spoil the key"); } catch (InvalidKeyException ike) { out.println("Expected exception when spoil the key"); } }
Example 11
Source File: PBKDF2TranslateTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * */ public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key2 = skf.translateKey(key1); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm."); } }
Example 12
Source File: PBKDF2TranslateTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - derive PBKDF2 key * using the given algorithm; - translate the key - check if the translated * and original keys have the same key value. * */ public void generateAndTranslateKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { // derive PBKDF2 key SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key2 = skf.translateKey(key1); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key2=" + new String(key2.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "generateAndTranslateKey test case failed: the key1 and" + " key2 values in its primary encoding format are" + " not the same for " + algoForTest + " algorithm."); } }
Example 13
Source File: PBKDF2Translate.java From hottub with GNU General Public License v2.0 | 6 votes |
/** * The test case scenario implemented in the method: - create my own secret * Key2 as an instance of a class implements PBEKey - spoil the key (set * iteration count to 0, for example) - try to translate key - * InvalidKeyException is expected. * * @return true if InvalidKeyException occurred; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public boolean translateSpoiledKey() throws NoSuchAlgorithmException, InvalidKeySpecException { // derive the key SecretKey key1 = getMyOwnSecretKey(); // spoil the key ((MyPBKDF2SecretKey) key1).spoil(); // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); try { SecretKey key2 = skf.translateKey(key1); } catch (InvalidKeyException ike) { // this is expected return true; } return false; }
Example 14
Source File: PBKDF2Translate.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * The test case scenario implemented in the method: - derive Key1 for the * given PBKDF2 algorithm - create my own secret Key2 as an instance of a * class implements PBEKey - translate Key2 - check if the key value of the * translated key and Key1 are the same. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean testMyOwnSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); SecretKey key2 = getMyOwnSecretKey(); // Is it actually the same? if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("We shouldn't be here. The key1 and key2 values " + "in its primary encoding format have to be the same!"); return false; } // Translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key3 = skf.translateKey(key2); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) { System.err.println("testMyOwnSecretKey test case failed: the key1 " + "and key3 values in its primary encoding format are not " + "the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 15
Source File: SecKFTranslateTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private void runTest(Algorithm algo) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException, BadPaddingException { AlgorithmParameterSpec[] aps = new AlgorithmParameterSpec[1]; byte[] plainText = new byte[800]; SecretKey key1 = algo.intSecurityKey(aps); Random random = new Random(); // Initialization SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.toString(), SUN_JCE); random.nextBytes(plainText); Cipher ci = Cipher.getInstance(algo.toString(), SUN_JCE); // Encryption ci.init(Cipher.ENCRYPT_MODE, key1, aps[0]); byte[] cipherText = new byte[ci.getOutputSize(plainText.length)]; int offset = ci.update(plainText, 0, plainText.length, cipherText, 0); ci.doFinal(cipherText, offset); // translate key SecretKey key2 = skf.translateKey(key1); // Decryption ci.init(Cipher.DECRYPT_MODE, key2, aps[0]); byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)]; ci.doFinal(cipherText, 0, cipherText.length, recoveredText); // Comparison if (!Arrays.equals(plainText, recoveredText)) { System.out.println("Key1:" + new String(key1.getEncoded()) + " Key2:" + new String(key2.getEncoded())); throw new RuntimeException("Testing translate key failed with " + algo); } }
Example 16
Source File: PBKDF2TranslateTest.java From jdk8u_jdk with GNU General Public License v2.0 | 5 votes |
/** * The test case scenario implemented in the method: - derive Key1 for the * given PBKDF2 algorithm - create my own secret Key2 as an instance of a * class implements PBEKey - translate Key2 - check if the key value of the * translated key and Key1 are the same. */ private void testMyOwnSecretKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); SecretKey key2 = getMyOwnSecretKey(salt); // Is it actually the same? if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { throw new RuntimeException( "We shouldn't be here. The key1 and key2 values in its" + " primary encoding format have to be the same!"); } // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key3 = skf.translateKey(key2); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key3=" + new String(key3.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "testMyOwnSecretKey test case failed: the key1 and key3" + " values in its primary encoding format are not" + " the same for " + algoForTest + " algorithm."); } }
Example 17
Source File: PBKDF2Translate.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * The test case scenario implemented in the method: - derive Key1 for the * given PBKDF2 algorithm - create my own secret Key2 as an instance of a * class implements PBEKey - translate Key2 - check if the key value of the * translated key and Key1 are the same. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean testMyOwnSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); SecretKey key2 = getMyOwnSecretKey(); // Is it actually the same? if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("We shouldn't be here. The key1 and key2 values " + "in its primary encoding format have to be the same!"); return false; } // Translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key3 = skf.translateKey(key2); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) { System.err.println("testMyOwnSecretKey test case failed: the key1 " + "and key3 values in its primary encoding format are not " + "the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 18
Source File: PBKDF2TranslateTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
/** * The test case scenario implemented in the method: - derive Key1 for the * given PBKDF2 algorithm - create my own secret Key2 as an instance of a * class implements PBEKey - translate Key2 - check if the key value of the * translated key and Key1 are the same. */ private void testMyOwnSecretKey(byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { SecretKey key1 = getSecretKeyForPBKDF2(algoForTest, salt); SecretKey key2 = getMyOwnSecretKey(salt); // Is it actually the same? if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { throw new RuntimeException( "We shouldn't be here. The key1 and key2 values in its" + " primary encoding format have to be the same!"); } // translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoForTest); SecretKey key3 = skf.translateKey(key2); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) { System.out.println("Key1=" + new String(key1.getEncoded()) + " key3=" + new String(key3.getEncoded()) + " salt=" + new String(salt)); throw new RuntimeException( "testMyOwnSecretKey test case failed: the key1 and key3" + " values in its primary encoding format are not" + " the same for " + algoForTest + " algorithm."); } }
Example 19
Source File: PBKDF2Translate.java From openjdk-jdk8u with GNU General Public License v2.0 | 5 votes |
/** * The test case scenario implemented in the method: - derive Key1 for the * given PBKDF2 algorithm - create my own secret Key2 as an instance of a * class implements PBEKey - translate Key2 - check if the key value of the * translated key and Key1 are the same. * * @return true if the test case passed; false - otherwise. * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws InvalidKeyException */ public boolean testMyOwnSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException { SecretKey key1 = getSecretKeyForPBKDF2(algoToTest); SecretKey key2 = getMyOwnSecretKey(); // Is it actually the same? if (!Arrays.equals(key1.getEncoded(), key2.getEncoded())) { System.err.println("We shouldn't be here. The key1 and key2 values " + "in its primary encoding format have to be the same!"); return false; } // Translate key SecretKeyFactory skf = SecretKeyFactory.getInstance(algoToTest); SecretKey key3 = skf.translateKey(key2); // Check if it still the same after translation if (!Arrays.equals(key1.getEncoded(), key3.getEncoded())) { System.err.println("testMyOwnSecretKey test case failed: the key1 " + "and key3 values in its primary encoding format are not " + "the same for " + algoToTest + "algorithm."); return false; } return true; }
Example 20
Source File: SecKFTranslateTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private void runTest(Algorithm algo) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException, BadPaddingException { AlgorithmParameterSpec[] aps = new AlgorithmParameterSpec[1]; byte[] plainText = new byte[800]; SecretKey key1 = algo.intSecurityKey(aps); Random random = new Random(); // Initialization SecretKeyFactory skf = SecretKeyFactory.getInstance(algo.toString(), SUN_JCE); random.nextBytes(plainText); Cipher ci = Cipher.getInstance(algo.toString(), SUN_JCE); // Encryption ci.init(Cipher.ENCRYPT_MODE, key1, aps[0]); byte[] cipherText = new byte[ci.getOutputSize(plainText.length)]; int offset = ci.update(plainText, 0, plainText.length, cipherText, 0); ci.doFinal(cipherText, offset); // translate key SecretKey key2 = skf.translateKey(key1); // Decryption ci.init(Cipher.DECRYPT_MODE, key2, aps[0]); byte[] recoveredText = new byte[ci.getOutputSize(plainText.length)]; ci.doFinal(cipherText, 0, cipherText.length, recoveredText); // Comparison if (!Arrays.equals(plainText, recoveredText)) { System.out.println("Key1:" + new String(key1.getEncoded()) + " Key2:" + new String(key2.getEncoded())); throw new RuntimeException("Testing translate key failed with " + algo); } }