public static EncryptedPrivateKeyInfo createEncryptedPrivateKeyInfo( String algorithm, char[] passPhrase, byte[] salt, int iterationCount, PrivateKeyInfo keyInfo) { if (!PBEUtil.isPBEAlgorithm(algorithm)) { throw new Exception("attempt to use non-PBE algorithm with PBE EncryptedPrivateKeyInfo generation"); } ASN1Encodable parameters = PBEUtil.generateAlgorithmParameters(algorithm, salt, iterationCount); CipherParameters keyParameters = PBEUtil.generateCipherParameters(algorithm, passPhrase, parameters); byte[] encoding = null; Object engine = PBEUtil.createEngine(algorithm); if (engine is BufferedBlockCipher) { BufferedBlockCipher cipher = (BufferedBlockCipher)engine; cipher.init(true, keyParameters); byte[] keyBytes = keyInfo.getEncoded(); int encLen = cipher.getOutputSize(keyBytes.Length); encoding = new byte[encLen]; int off = cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0); cipher.doFinal(encoding, off); } else if (engine is StreamCipher) { StreamCipher cipher = (StreamCipher)engine; cipher.init(true, keyParameters); byte[] keyBytes = keyInfo.getEncoded(); encoding = new byte[keyBytes.Length]; cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0); } return(new EncryptedPrivateKeyInfo(new AlgorithmIdentifier(PBEUtil.getObjectIdentifier(algorithm), parameters), encoding)); }
public static PrivateKeyInfo createPrivateKeyInfo( char[] passPhrase, EncryptedPrivateKeyInfo encInfo) { CipherParameters keyParameters = PBEUtil.generateCipherParameters(encInfo.getEncryptionAlgorithm().getObjectId(), passPhrase, encInfo.getEncryptionAlgorithm().getParameters()); Object engine = PBEUtil.createEngine(encInfo.getEncryptionAlgorithm().getObjectId()); byte[] encoding = null; if (engine is BufferedBlockCipher) { BufferedBlockCipher cipher = (BufferedBlockCipher)engine; cipher.init(false, keyParameters); byte[] keyBytes = encInfo.getEncryptedData(); int encLen = cipher.getOutputSize(keyBytes.Length); encoding = new byte[encLen]; int off = cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0); cipher.doFinal(encoding, off); } else if (engine is StreamCipher) { StreamCipher cipher = (StreamCipher)engine; cipher.init(false, keyParameters); byte[] keyBytes = encInfo.getEncryptedData(); encoding = new byte[keyBytes.Length]; cipher.processBytes(keyBytes, 0, keyBytes.Length, encoding, 0); } ASN1InputStream aIn = new ASN1InputStream(new MemoryStream(encoding)); return(PrivateKeyInfo.getInstance(aIn.readObject())); }
ASN1Sequence decryptData( AlgorithmIdentifier algId, byte[] data, char[] password) { PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters()); CipherParameters keyParameters = PBEUtil.generateCipherParameters(algId.getObjectId(), password, pbeParams); byte[] encoding = null; Object engine = PBEUtil.createEngine(algId.getObjectId()); if (engine is BufferedBlockCipher) { BufferedBlockCipher cipher = (BufferedBlockCipher)engine; cipher.init(false, keyParameters); int encLen = cipher.getOutputSize(data.Length); encoding = new byte[encLen]; int off = cipher.processBytes(data, 0, data.Length, encoding, 0); cipher.doFinal(encoding, off); } else if (engine is StreamCipher) { StreamCipher cipher = (StreamCipher)engine; cipher.init(false, keyParameters); encoding = new byte[data.Length]; cipher.processBytes(data, 0, data.Length, encoding, 0); } ASN1InputStream bIn = new ASN1InputStream(new MemoryStream(encoding)); return((ASN1Sequence)bIn.readObject()); }