javax.crypto.Cipher.unwrap()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(269)

本文整理了Java中javax.crypto.Cipher.unwrap()方法的一些代码示例,展示了Cipher.unwrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Cipher.unwrap()方法的具体详情如下:
包路径:javax.crypto.Cipher
类名称:Cipher
方法名:unwrap

Cipher.unwrap介绍

[英]Unwraps a key using this cipher instance.
[中]使用此密码实例打开密钥。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

public static Key decodeTransmittedKey( byte[] sessionKey, byte[] transmittedKey, boolean privateKey )
 throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
 KeySpec keySpec = null;
 Key keyKey = null;
 if ( transmittedKey == null || sessionKey == null ) {
  return null;
 }
 if ( !privateKey ) {
  keySpec = new X509EncodedKeySpec( sessionKey );
  keyKey = KeyFactory.getInstance( PUBLIC_KEY_ALGORITHM ).generatePublic( keySpec );
 } else {
  keySpec = new PKCS8EncodedKeySpec( sessionKey );
  keyKey = KeyFactory.getInstance( PUBLIC_KEY_ALGORITHM ).generatePrivate( keySpec );
 }
 Cipher keyCipher = Cipher.getInstance( TRANSMISSION_CIPHER_PARAMS );
 keyCipher.init( Cipher.UNWRAP_MODE, keyKey );
 return keyCipher.unwrap( transmittedKey, SINGLE_KEY_ALGORITHM, Cipher.SECRET_KEY );
}

代码示例来源:origin: apache/incubator-dubbo

keyCipher.init(Cipher.UNWRAP_MODE, _privateKey);
Key key = keyCipher.unwrap(encKey, algorithm, Cipher.SECRET_KEY);
_bodyIn = _in.readInputStream();

代码示例来源:origin: apache/incubator-dubbo

keyCipher.init(Cipher.UNWRAP_MODE, _cert);
Key key = keyCipher.unwrap(encKey, algorithm, Cipher.SECRET_KEY);
_bodyIn = _in.readInputStream();

代码示例来源:origin: aws/aws-sdk-java

keyWrapAlgo, securityProvider);
cipher.init(Cipher.UNWRAP_MODE, kek);
return (SecretKey) cipher.unwrap(cekSecured, keyWrapAlgo,
                 Cipher.SECRET_KEY);

代码示例来源:origin: apache/accumulo

@SuppressFBWarnings(value = "CIPHER_INTEGRITY",
  justification = "integrity not needed for key wrap")
public static Key unwrapKey(byte[] fek, Key kek) {
 Key result = null;
 try {
  Cipher c = Cipher.getInstance(KEY_WRAP_TRANSFORM);
  c.init(Cipher.UNWRAP_MODE, kek);
  result = c.unwrap(fek, "AES", Cipher.SECRET_KEY);
 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
  throw new CryptoException("Unable to unwrap file encryption key", e);
 }
 return result;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

keyWrapAlgo, securityProvider);
cipher.init(Cipher.UNWRAP_MODE, kek);
return (SecretKey) cipher.unwrap(cekSecured, keyWrapAlgo,
    Cipher.SECRET_KEY);

代码示例来源:origin: 0xbb/otp-authenticator

/**
   * Unwrap a {@link SecretKey} using the private key assigned to this
   * wrapper.
   *
   * @param blob a wrapped {@link SecretKey} as previously returned by
   *            {@link #wrap(SecretKey)}.
   */
  public SecretKey unwrap(byte[] blob) throws GeneralSecurityException {
    mCipher.init(Cipher.UNWRAP_MODE, mPair.getPrivate());

    return (SecretKey) mCipher.unwrap(blob, "AES", Cipher.SECRET_KEY);
  }
}

代码示例来源:origin: com.caucho/resin

public Key decryptKey(String keyAlgorithm, byte []encKey)
{
 try {
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(Cipher.UNWRAP_MODE, _authKeyPair.getPrivate());
  Key key = cipher.unwrap(encKey, keyAlgorithm, Cipher.SECRET_KEY);
  return key;
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: jscep/jscep

private Key unwrapKey(PrivateKey wrappingKey, byte[] wrappedKey) throws GeneralSecurityException {
  Cipher unwrapper = Cipher.getInstance(RSA);
  unwrapper.init(Cipher.UNWRAP_MODE, wrappingKey);
  try {
    return unwrapper.unwrap(wrappedKey, DES, Cipher.SECRET_KEY);
  } catch (InvalidKeyException e) {
    LOGGER.error("Cannot unwrap symetric key.  Are you using a valid key pair?");
    throw e;
  }
}

代码示例来源:origin: baratine/baratine

public Key decryptKey(String keyAlgorithm, byte []encKey)
{
 try {
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(Cipher.UNWRAP_MODE, _authKeyPair.getPrivate());
  Key key = cipher.unwrap(encKey, keyAlgorithm, Cipher.SECRET_KEY);
  return key;
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: baratine/baratine

public Key decryptKey(String keyAlgorithm, byte []encKey)
{
 try {
  Cipher cipher = Cipher.getInstance("RSA");
  cipher.init(Cipher.UNWRAP_MODE, _authKeyPair.getPrivate());
  Key key = cipher.unwrap(encKey, keyAlgorithm, Cipher.SECRET_KEY);
  return key;
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: apptentive/apptentive-android

private static SecretKey unwrapSymmetricKey(KeyPair wrapperKey, String wrappedKeyData) throws NoSuchPaddingException,
                                               NoSuchAlgorithmException,
                                               InvalidKeyException {
  byte[] encryptedKeyData = Base64.decode(wrappedKeyData, Base64.DEFAULT);
  Cipher cipher = Cipher.getInstance(WRAPPER_TRANSFORMATION);
  cipher.init(Cipher.UNWRAP_MODE, wrapperKey.getPrivate());
  return (SecretKey) cipher.unwrap(encryptedKeyData, DEFAULT_KEY_ALGORITHM, Cipher.SECRET_KEY);
}

代码示例来源:origin: apache/cxf

public static Key unwrapKey(byte[] wrappedBytes,
                    String wrappedKeyAlgo,
                    Key unwrapperKey,
                    KeyProperties keyProps,
                    int wrappedKeyType)  throws SecurityException {
  try {
    Cipher c = initCipher(unwrapperKey, keyProps, Cipher.UNWRAP_MODE);
    return c.unwrap(wrappedBytes, wrappedKeyAlgo, wrappedKeyType);
  } catch (Exception ex) {
    throw new SecurityException(ex);
  }
}

代码示例来源:origin: org.apache.cxf/cxf-rt-security

public static Key unwrapKey(byte[] wrappedBytes,
                    String wrappedKeyAlgo,
                    Key unwrapperKey,
                    KeyProperties keyProps,
                    int wrappedKeyType)  throws SecurityException {
  try {
    Cipher c = initCipher(unwrapperKey, keyProps, Cipher.UNWRAP_MODE);
    return c.unwrap(wrappedBytes, wrappedKeyAlgo, wrappedKeyType);
  } catch (Exception ex) {
    throw new SecurityException(ex);
  }
}

代码示例来源:origin: stackoverflow.com

Cipher rsa = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
rsa.init(Cipher.WRAP_MODE, keyPair.getPublic());
byte[] wrapped = rsa.wrap(aesKey);

rsa.init(Cipher.UNWRAP_MODE, keyPair.getPrivate());
SecretKey unwrappedAESKey = (SecretKey) rsa.unwrap(wrapped, "RSA", Cipher.SECRET_KEY);

代码示例来源:origin: stackoverflow.com

SecretKey sk = new SecretKeySpec(new byte[16], "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec gcmSpec = new GCMParameterSpec(128, new byte[12]);
cipher.init(Cipher.WRAP_MODE, sk, gcmSpec);
byte[] wrappedKey = cipher.wrap(sk);
System.out.println(Hex.toHexString(wrappedKey));

cipher.init(Cipher.UNWRAP_MODE, sk, gcmSpec);
SecretKey unwrap = (SecretKey) cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
System.out.println(Hex.toHexString(unwrap.getEncoded()));

代码示例来源:origin: com.amazonaws/aws-dynamodb-encryption-java

protected SecretKey unwrapKey(Map<String, String> description, byte[] encryptedKey, String wrappingAlgorithm)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
  if (unwrappingKey instanceof DelegatedKey) {
    return (SecretKey)((DelegatedKey)unwrappingKey).unwrap(encryptedKey,
        description.get(CONTENT_KEY_ALGORITHM), Cipher.SECRET_KEY, null, wrappingAlgorithm);
  } else {
    Cipher cipher = Cipher.getInstance(wrappingAlgorithm);
    cipher.init(Cipher.UNWRAP_MODE, unwrappingKey, Utils.getRng());
    return (SecretKey) cipher.unwrap(encryptedKey,
        description.get(CONTENT_KEY_ALGORITHM), Cipher.SECRET_KEY);
  }
}

代码示例来源:origin: aws/aws-dynamodb-encryption-java

protected SecretKey unwrapKey(Map<String, String> description, byte[] encryptedKey, String wrappingAlgorithm)
    throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
  if (unwrappingKey instanceof DelegatedKey) {
    return (SecretKey)((DelegatedKey)unwrappingKey).unwrap(encryptedKey,
        description.get(CONTENT_KEY_ALGORITHM), Cipher.SECRET_KEY, null, wrappingAlgorithm);
  } else {
    Cipher cipher = Cipher.getInstance(wrappingAlgorithm);
    cipher.init(Cipher.UNWRAP_MODE, unwrappingKey, Utils.getRng());
    return (SecretKey) cipher.unwrap(encryptedKey,
        description.get(CONTENT_KEY_ALGORITHM), Cipher.SECRET_KEY);
  }
}

代码示例来源:origin: com.madgag.spongycastle/pkix

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);
}

代码示例来源:origin: com.madgag.spongycastle/bcpkix-jdk15on

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);
}

相关文章