private void Run3(int id, char[] password, byte[] salt, int iCount, byte[] result) { PbeParametersGenerator generator = new Pkcs12ParametersGenerator(new Sha1Digest()); generator.Init(PbeParametersGenerator.Pkcs12PasswordToBytes(password), salt, iCount); ICipherParameters key = generator.GenerateDerivedMacParameters(160); if (!Arrays.AreEqual(result, ((KeyParameter)key).GetKey())) { Fail("id " + id + " Failed"); } }
private static KeyParameter KeyGen(string salt) { string text = Guid.NewGuid().ToString(); saltString = salt; byte[] bytes = Encoding.UTF8.GetBytes(saltString); char[] password = text.ToCharArray(); byte[] password2 = PbeParametersGenerator.Pkcs12PasswordToBytes(password); IDigest digest = new Sha1Digest(); PbeParametersGenerator pbeParametersGenerator = new Pkcs12ParametersGenerator(digest); pbeParametersGenerator.Init(password2, bytes, 32768); return((KeyParameter)pbeParametersGenerator.GenerateDerivedParameters("AES", 256)); }
private void Run2(int id, char[] password, byte[] salt, int iCount, byte[] result) { PbeParametersGenerator generator = new Pkcs12ParametersGenerator(new Sha1Digest()); generator.Init(PbeParametersGenerator.Pkcs12PasswordToBytes(password), salt, iCount); ParametersWithIV parameters = (ParametersWithIV) generator.GenerateDerivedParameters("DES", 64, 64); if (!Arrays.AreEqual(result, parameters.GetIV())) { Fail("id " + id + " Failed"); } }
public override void PerformTest() { int iCount = 100; byte[] salt = DigestUtilities.DoFinal(digest); PbeParametersGenerator pGen = new Pkcs12ParametersGenerator(digest); pGen.Init( PbeParametersGenerator.Pkcs12PasswordToBytes(password), salt, iCount); ParametersWithIV parameters = (ParametersWithIV) pGen.GenerateDerivedParameters(baseAlgorithm, keySize, ivSize); KeyParameter encKey = (KeyParameter)parameters.Parameters; IBufferedCipher c; if (baseAlgorithm.Equals("RC4")) { c = CipherUtilities.GetCipher(baseAlgorithm); c.Init(true, encKey); } else { c = CipherUtilities.GetCipher(baseAlgorithm + "/CBC/PKCS7Padding"); c.Init(true, parameters); } byte[] enc = c.DoFinal(salt); c = CipherUtilities.GetCipher(algorithm); // PBEKeySpec keySpec = new PBEKeySpec(password, salt, iCount); // SecretKeyFactory fact = SecretKeyFactory.getInstance(algorithm); // // c.Init(false, fact.generateSecret(keySpec)); Asn1Encodable algParams = PbeUtilities.GenerateAlgorithmParameters( algorithm, salt, iCount); ICipherParameters cipherParams = PbeUtilities.GenerateCipherParameters( algorithm, password, algParams); c.Init(false, cipherParams); byte[] dec = c.DoFinal(enc); if (!AreEqual(salt, dec)) { Fail("" + algorithm + "failed encryption/decryption test"); } // NB: We don't support retrieving parameters from cipher // // // // get the parameters // // // AlgorithmParameters param = c.getParameters(); // PBEParameterSpec spec = (PBEParameterSpec)param.getParameterSpec(PBEParameterSpec.class); // // if (!AreEqual(salt, spec.getSalt())) // { // Fail("" + algorithm + "failed salt test"); // } // // if (iCount != spec.getIterationCount()) // { // Fail("" + algorithm + "failed count test"); // } // NB: This section just repeats earlier test passing 'param' separately // // // // try using parameters // // // keySpec = new PBEKeySpec(password); // // c.Init(false, fact.generateSecret(keySpec), param); // // dec = c.DoFinal(enc); // // if (!AreEqual(salt, dec)) // { // Fail("" + algorithm + "failed encryption/decryption test"); // } }