当前位置: 首页>>代码示例>>Java>>正文


Java SecretKeyFactory.translateKey方法代码示例

本文整理汇总了Java中javax.crypto.SecretKeyFactory.translateKey方法的典型用法代码示例。如果您正苦于以下问题:Java SecretKeyFactory.translateKey方法的具体用法?Java SecretKeyFactory.translateKey怎么用?Java SecretKeyFactory.translateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.SecretKeyFactory的用法示例。


在下文中一共展示了SecretKeyFactory.translateKey方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: generateAndTranslateKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:30,代码来源:PBKDF2Translate.java

示例2: translateSpoiledKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:30,代码来源:PBKDF2Translate.java

示例3: generateAndTranslateKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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.");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:PBKDF2TranslateTest.java

示例4: translateSpoiledKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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");
    }

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:PBKDF2TranslateTest.java

示例5: testMyOwnSecretKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:39,代码来源:PBKDF2Translate.java

示例6: runTest

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
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);
    }

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:39,代码来源:SecKFTranslateTest.java

示例7: testMyOwnSecretKey

import javax.crypto.SecretKeyFactory; //导入方法依赖的package包/类
/**
 * 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.");
    }

}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:36,代码来源:PBKDF2TranslateTest.java


注:本文中的javax.crypto.SecretKeyFactory.translateKey方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。