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()); }
public static CipherParameters generateCipherParameters( String algorithm, char[] password, ASN1Encodable pbeParameters) { String mechanism = (string)algorithms[algorithm.ToUpper()]; byte[] key; CipherParameters parameters = null; String type = (String)algorithmType[mechanism]; byte[] salt = null; int iterationCount = 0; if (isPKCS12(mechanism)) { PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(pbeParameters); salt = pbeParams.getIV(); iterationCount = pbeParams.getIterations().intValue(); key = PBEParametersGenerator.PKCS12PasswordToBytes(password); } else if (isPKCS5Scheme2(mechanism)) { PBKDF2Params pbeParams = PBKDF2Params.getInstance(pbeParameters); salt = pbeParams.getSalt(); iterationCount = pbeParams.getIterationCount().intValue(); key = PBEParametersGenerator.PKCS5PasswordToBytes(password); } else { PBEParameter pbeParams = PBEParameter.getInstance(pbeParameters); salt = pbeParams.getSalt(); iterationCount = pbeParams.getIterationCount().intValue(); key = PBEParametersGenerator.PKCS5PasswordToBytes(password); } if (mechanism.StartsWith("PBEwithSHA-1")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new SHA1Digest(), key, salt, iterationCount); if (mechanism.Equals("PBEwithSHA-1and128bitRC4")) { parameters = generator.generateDerivedParameters(128); } else if (mechanism.Equals("PBEwithSHA-1and40bitRC4")) { parameters = generator.generateDerivedParameters(40); } else if (mechanism.Equals("PBEwithSHA-1and3-keyDESEDE-CBC")) { parameters = generator.generateDerivedParameters(192, 64); } else if (mechanism.Equals("PBEwithSHA-1and2-keyDESEDE-CBC")) { parameters = generator.generateDerivedParameters(128, 64); } else if (mechanism.Equals("PBEwithSHA-1and128bitRC2-CBC")) { parameters = generator.generateDerivedParameters(128, 64); } else if (mechanism.Equals("PBEwithSHA-1and40bitRC2-CBC")) { parameters = generator.generateDerivedParameters(40, 64); } else if (mechanism.Equals("PBEwithSHA1andDES-CBC")) { parameters = generator.generateDerivedParameters(64, 64); } else if (mechanism.Equals("PBEwithSHA1andRC2-CBC")) { parameters = generator.generateDerivedParameters(128, 64); } } else if (mechanism.StartsWith("PBEwithMD5")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new MD5Digest(), key, salt, iterationCount); if (mechanism.Equals("PBEwithMD5andDES-CBC")) { parameters = generator.generateDerivedParameters(64, 64); } else if (mechanism.Equals("PBEwithMD5andRC2-CBC")) { parameters = generator.generateDerivedParameters(64, 64); } } else if (mechanism.StartsWith("PBEwithMD2")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new MD2Digest(), key, salt, iterationCount); if (mechanism.Equals("PBEwithMD2andDES-CBC")) { parameters = generator.generateDerivedParameters(64, 64); } else if (mechanism.Equals("PBEwithMD2andRC2-CBC")) { parameters = generator.generateDerivedParameters(64, 64); } } else if (mechanism.StartsWith("PBEwithHmac")) { if (mechanism.Equals("PBEwithHmacSHA-1")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new SHA1Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(160); } else if (mechanism.Equals("PBEwithHmacSHA-224")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new SHA224Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(224); } else if (mechanism.Equals("PBEwithHmacSHA-256")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new SHA256Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(256); } else if (mechanism.Equals("PBEwithHmacRIPEMD128")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new RIPEMD128Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(128); } else if (mechanism.Equals("PBEwithHmacRIPEMD160")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new RIPEMD160Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(160); } else if (mechanism.Equals("PBEwithHmacRIPEMD256")) { PBEParametersGenerator generator = makePBEGenerator((String)algorithmType[mechanism], new RIPEMD256Digest(), key, salt, iterationCount); parameters = generator.generateDerivedMacParameters(256); } } for (int i = 0; i != key.Length; i++) { key[i] = 0; } return(parameters); }