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


Java Cipher.unwrap方法代码示例

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


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

示例1: extractSecretKey

import javax.crypto.Cipher; //导入方法依赖的package包/类
protected Key extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey)
    throws CMSException
{
    Cipher keyEncryptionCipher = helper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());

    try
    {
        IvParameterSpec ivSpec = new IvParameterSpec(ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets());

        keyEncryptionCipher.init(Cipher.UNWRAP_MODE, new SecretKeySpec(derivedKey, keyEncryptionCipher.getAlgorithm()), ivSpec);

        return keyEncryptionCipher.unwrap(encryptedContentEncryptionKey, contentEncryptionAlgorithm.getAlgorithm().getId(), Cipher.SECRET_KEY);
    }
    catch (GeneralSecurityException e)
    {
        throw new CMSException("cannot process content encryption key: " + e.getMessage(), e);
    }
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:19,代码来源:JcePasswordRecipient.java

示例2: unwrapPrivateKeyByKey

import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
 * 使用密钥解包密钥
 * @param key
 * @param unwrapKey
 * @return
 */
public PrivateKey unwrapPrivateKeyByKey(byte[] key,Key unwrapKey)
{
	try {
   		if(key==null || key.length<=0 || unwrapKey==null)
   		{
   			return null;
   		}
		Cipher cipher=Cipher.getInstance(algorithm);
		//使用私钥包裹模式
		cipher.init(Cipher.UNWRAP_MODE,unwrapKey);
		return (PrivateKey) cipher.unwrap(key, algorithm,Cipher.PRIVATE_KEY);
	} catch (Exception e) {
		log.error(e.getMessage(),e);
	}
   	return null;
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:23,代码来源:RsaUtil.java

示例3: unwrapPublicKeyByKey

import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
 * 使用密钥解包密钥
 * @param key
 * @param unwrapKey
 * @return
 */
public PublicKey unwrapPublicKeyByKey(byte[] key,Key unwrapKey)
{
	try {
   		if(key==null || key.length<=0 || unwrapKey==null)
   		{
   			return null;
   		}
		Cipher cipher=Cipher.getInstance(algorithm);
		//使用私钥包裹模式
		cipher.init(Cipher.UNWRAP_MODE,unwrapKey);
		return (PublicKey) cipher.unwrap(key, algorithm,Cipher.PUBLIC_KEY);
	} catch (Exception e) {
		log.error(e.getMessage(),e);
	}
   	return null;
}
 
开发者ID:juebanlin,项目名称:util4j,代码行数:23,代码来源:RsaUtil.java

示例4: test

import javax.crypto.Cipher; //导入方法依赖的package包/类
private static void test(KeyPair kp, SecretKey secretKey,
        Cipher wrapCipher, Cipher unwrapCipher)
        throws Exception {
    String algo = secretKey.getAlgorithm();
    wrapCipher.init(Cipher.WRAP_MODE, kp.getPublic());
    byte[] wrappedKey = wrapCipher.wrap(secretKey);
    unwrapCipher.init(Cipher.UNWRAP_MODE, kp.getPrivate());
    Key unwrappedKey =
            unwrapCipher.unwrap(wrappedKey, algo, Cipher.SECRET_KEY);

    System.out.println("Test " + wrapCipher.getProvider().getName() +
            "/" + unwrapCipher.getProvider().getName() + ": ");
    if (!Arrays.equals(secretKey.getEncoded(),
            unwrappedKey.getEncoded())) {
        throw new Exception("Test Failed!");
    }
    System.out.println("Passed");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:TestRSACipherWrap.java

示例5: unwrapSessionKey

import javax.crypto.Cipher; //导入方法依赖的package包/类
private Key unwrapSessionKey(ASN1ObjectIdentifier wrapAlg, SecretKey agreedKey, ASN1ObjectIdentifier contentEncryptionAlgorithm, byte[] encryptedContentEncryptionKey)
    throws CMSException, InvalidKeyException, NoSuchAlgorithmException
{
    Cipher keyCipher = helper.createCipher(wrapAlg);
    keyCipher.init(Cipher.UNWRAP_MODE, agreedKey);
    return keyCipher.unwrap(encryptedContentEncryptionKey, helper.getBaseCipherName(contentEncryptionAlgorithm), Cipher.SECRET_KEY);
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:8,代码来源:JceKeyAgreeRecipient.java

示例6: doTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
private static void doTest(String provider, String algo) throws Exception {
    SecretKey key;
    SecretKey keyToWrap;

    // init a secret Key
    KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER);
    kg.init(KEY_LENGTH);
    key = kg.generateKey();
    keyToWrap = kg.generateKey();

    // initialization
    Cipher cipher = Cipher.getInstance(algo, provider);
    cipher.init(Cipher.WRAP_MODE, key);
    AlgorithmParameters params = cipher.getParameters();

    // wrap the key
    byte[] keyWrapper = cipher.wrap(keyToWrap);
    try {
        // check if we can't wrap it again with the same key/IV
        keyWrapper = cipher.wrap(keyToWrap);
        throw new RuntimeException(
                "FAILED: expected IllegalStateException hasn't "
                        + "been thrown ");
    } catch (IllegalStateException ise) {
        System.out.println(ise.getMessage());
        System.out.println("Expected exception");
    }

    // unwrap the key
    cipher.init(Cipher.UNWRAP_MODE, key, params);
    cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);

    // check if we can unwrap second time
    Key unwrapKey = cipher.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);

    if (!Arrays.equals(keyToWrap.getEncoded(), unwrapKey.getEncoded())) {
        throw new RuntimeException(
                "FAILED: original and unwrapped keys are not equal");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:41,代码来源:KeyWrapper.java

示例7: runTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
private static void runTest(DataTuple dataTuple, boolean supportedKeyLength)
        throws NoSuchAlgorithmException, NoSuchProviderException,
        NoSuchPaddingException, InvalidKeyException,
        IllegalBlockSizeException {
    Cipher algorithmCipher = Cipher.getInstance(
            dataTuple.algorithm, PROVIDER_NAME);
    Cipher oidCipher = Cipher.getInstance(dataTuple.oid, PROVIDER_NAME);

    if (algorithmCipher == null) {
        throw new RuntimeException(String.format(
                "Test failed: algorithm string %s getInstance failed.%n",
                dataTuple.algorithm));
    }

    if (oidCipher == null) {
        throw new RuntimeException(
                String.format("Test failed: OID %s getInstance failed.%n",
                        dataTuple.oid));
    }

    if (!algorithmCipher.getAlgorithm().equals(
            dataTuple.algorithm)) {
        throw new RuntimeException(String.format(
                "Test failed: algorithm string %s getInstance "
                        + "doesn't generate expected algorithm.%n",
                dataTuple.oid));
    }

    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(dataTuple.keyLength);
    SecretKey key = kg.generateKey();

    // Wrap the key
    algorithmCipher.init(Cipher.WRAP_MODE, key);
    if (!supportedKeyLength) {
        throw new RuntimeException(String.format(
                "The key length %d is not supported, so the initialization"
                        + " of algorithmCipher should fail.%n",
                dataTuple.keyLength));
    }

    // Unwrap the key
    oidCipher.init(Cipher.UNWRAP_MODE, key);
    if (!supportedKeyLength) {
        throw new RuntimeException(String.format(
                "The key length %d is not supported, so the initialization"
                        + " of oidCipher should fail.%n",
                dataTuple.keyLength));
    }

    byte[] keyWrapper = algorithmCipher.wrap(key);
    Key unwrappedKey = oidCipher.unwrap(keyWrapper, "AES",
            Cipher.SECRET_KEY);

    // Comparison
    if (!Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded())) {
        throw new RuntimeException("Key comparison failed");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:60,代码来源:TestAESWrapOids.java

示例8: runTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
public boolean runTest(Provider p, String algo, PrintStream out)
        throws Exception {

    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;

    String baseAlgo
            = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");

    try {
        // Initialization
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt,
                ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec(
                "Secret Key".toCharArray()));
        Cipher ci = Cipher.getInstance(algo);

        if (isAES) {
            ci.init(Cipher.WRAP_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.WRAP_MODE, key, aps);
        }

        byte[] keyWrapper = ci.wrap(key);
        if (isAES) {
            ci.init(Cipher.UNWRAP_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.UNWRAP_MODE, key, aps);
        }

        Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);

        if (baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256")) {
            out.print(
                    "InvalidKeyException not thrown when keyStrength > 128");
            return false;
        }

        return (Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded()));

    } catch (InvalidKeyException ex) {

        if ((baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256"))) {
            out.println("Expected InvalidKeyException, keyStrength > 128");
            return true;
        } else {
            throw ex;
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:58,代码来源:TestCipherKeyWrapperPBEKey.java

示例9: runTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
public boolean runTest(Provider p, String algo, PrintStream out)
        throws Exception {

    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;

    String baseAlgo
            = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");

    boolean isUnlimited =
        (Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE);

    try {
        // Initialization
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt,
                ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec(
                "Secret Key".toCharArray()));
        Cipher ci = Cipher.getInstance(algo);
        if (isAES) {
            ci.init(Cipher.WRAP_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.WRAP_MODE, key, aps);
        }

        byte[] keyWrapper = ci.wrap(key);
        if (isAES) {
            ci.init(Cipher.UNWRAP_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.UNWRAP_MODE, key, aps);
        }

        Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);

        if ((baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
            out.print(
                    "Expected InvalidKeyException not thrown");
            return false;
        }

        return (Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded()));

    } catch (InvalidKeyException ex) {

        if ((baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
            out.print(
                    "Expected InvalidKeyException thrown");
            return true;
        } else {
            throw ex;
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:61,代码来源:TestCipherKeyWrapperPBEKey.java


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