Example usage for javax.crypto Cipher PRIVATE_KEY

List of usage examples for javax.crypto Cipher PRIVATE_KEY

Introduction

In this page you can find the example usage for javax.crypto Cipher PRIVATE_KEY.

Prototype

int PRIVATE_KEY

To view the source code for javax.crypto Cipher PRIVATE_KEY.

Click Source Link

Document

Constant used to indicate the to-be-unwrapped key is a "private key".

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    SecureRandom random = new SecureRandom();

    KeyPairGenerator fact = KeyPairGenerator.getInstance("RSA", "BC");
    fact.initialize(1024, random);/*from  ww  w. j  a  v a2s.c  om*/

    KeyPair keyPair = fact.generateKeyPair();
    Key wrapKey = createKeyForAES(256, random);
    cipher.init(Cipher.WRAP_MODE, wrapKey);

    byte[] wrappedKey = cipher.wrap(keyPair.getPrivate());
    cipher.init(Cipher.UNWRAP_MODE, wrapKey);
    Key key = cipher.unwrap(wrappedKey, "RSA", Cipher.PRIVATE_KEY);
    System.out.println(keyPair.getPrivate().equals(key));

}

From source file:netinf.common.security.impl.CryptoAlgorithmImpl.java

@Override
public PrivateKey decryptPrivateKey(String algorithmUsedToEncryptTheKey, String algorithmKeyIsUsedFor, Key key,
        String keyToDecrypt) throws NetInfCheckedSecurityException {
    try {/*from   w  w  w  .  j  a va2 s.c om*/
        Cipher cipher = Cipher.getInstance(algorithmUsedToEncryptTheKey);
        cipher.init(Cipher.UNWRAP_MODE, key);
        return (PrivateKey) cipher.unwrap(Utils.stringToBytes(keyToDecrypt), algorithmKeyIsUsedFor,
                Cipher.PRIVATE_KEY);
    } catch (NoSuchAlgorithmException e) {
        throw new NetInfCheckedSecurityException("Unknown cipher-algorithm: " + e.getMessage());
    } catch (NoSuchPaddingException e) {
        throw new NetInfCheckedSecurityException("Unknown cipher-padding: " + e.getMessage());
    } catch (InvalidKeyException e) {
        throw new NetInfCheckedSecurityException("Invalid Key. " + e.getMessage());
    }
}

From source file:org.opentravel.schemacompiler.security.PasswordHelper.java

/**
 * Returns an decryption cipher that is based on the private encryption key file located on the
 * application's classpath.//from ww  w .  j  av  a  2  s .c  om
 * 
 * @return Cipher
 * @throws GeneralSecurityException
 *             thrown if encryption key is not valid
 * @throws IOException
 *             thrown if the contents of the private key file cannot be loaded
 */
private static Cipher loadDecryptionCipher() throws GeneralSecurityException, IOException {
    BigInteger[] keyComponents = loadKeyFile(PRIVATE_KEYFILE);
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(keyComponents[0], keyComponents[1]);
    KeyFactory factory = KeyFactory.getInstance(ENCRYPTION_ALGORITHM);
    PrivateKey privateKey = factory.generatePrivate(keySpec);
    Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);

    cipher.init(Cipher.PRIVATE_KEY, privateKey);
    return cipher;
}