public byte[] encrypt(string cleartext) { m_cipher.init(true, m_parameters); byte[] input = m_encoding.GetBytes(cleartext); int outputSize = ((input.Length + BLOCK_SIZE) / BLOCK_SIZE) * BLOCK_SIZE; byte[] output = new byte[outputSize]; int len = m_cipher.processBytes(input, 0, input.Length, output, 0); len = len + m_cipher.doFinal(output, len); return(output); }
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()); }
private byte[] decryptBlock( byte[] in_enc, int inOff, int inLen, byte[] z) //throws InvalidCipherTextException { byte[] M = null; KeyParameter macKey = null; KDFParameters kParam = new KDFParameters(z, param.getDerivationV()); int macKeySize = param.getMacKeySize(); kdf.init(kParam); inLen -= mac.getMacSize(); if (cipher == null) // stream mode { byte[] buf = new byte[inLen + (macKeySize / 8)]; M = new byte[inLen]; kdf.generateBytes(buf, 0, buf.Length); for (int i = 0; i != inLen; i++) { M[i] = (byte)(in_enc[inOff + i] ^ buf[i]); } macKey = new KeyParameter(buf, inLen, (macKeySize / 8)); } else { int cipherKeySize = ((IESWithCipherParameters)param).getCipherKeySize(); byte[] buf = new byte[(cipherKeySize / 8) + (macKeySize / 8)]; cipher.init(false, new KeyParameter(buf, 0, (cipherKeySize / 8))); byte[] tmp = new byte[cipher.getOutputSize(inLen)]; int off = cipher.processBytes(in_enc, inOff, inLen, tmp, 0); off += cipher.doFinal(tmp, off); M = new byte[off]; Array.Copy(tmp, 0, M, 0, off); macKey = new KeyParameter(buf, (cipherKeySize / 8), (macKeySize / 8)); } byte[] macIV = param.getEncodingV(); mac.init(macKey); mac.update(in_enc, inOff, inLen); mac.update(macIV, 0, macIV.Length); mac.doFinal(macBuf, 0); inOff += inLen; for (int t = 0; t < macBuf.Length; t++) { if (macBuf[t] != in_enc[inOff + t]) { throw (new InvalidCipherTextException("Mac codes failed to equal.")); } } return(M); }