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 ); }
keyCipher.init(Cipher.UNWRAP_MODE, _privateKey); Key key = keyCipher.unwrap(encKey, algorithm, Cipher.SECRET_KEY); _bodyIn = _in.readInputStream();
keyCipher.init(Cipher.UNWRAP_MODE, _cert); Key key = keyCipher.unwrap(encKey, algorithm, Cipher.SECRET_KEY); _bodyIn = _in.readInputStream();
keyWrapAlgo, securityProvider); cipher.init(Cipher.UNWRAP_MODE, kek); return (SecretKey) cipher.unwrap(cekSecured, keyWrapAlgo, Cipher.SECRET_KEY);
@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; }
keyWrapAlgo, securityProvider); cipher.init(Cipher.UNWRAP_MODE, kek); return (SecretKey) cipher.unwrap(cekSecured, keyWrapAlgo, Cipher.SECRET_KEY);
/** * 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); } }
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); } }
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; } }
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); } }
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); } }
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); }
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); } }
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); } }
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);
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()));
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); } }
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); } }
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); }
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); }