Beispiel #1
0
        public override IBufferedCipher createCipher(bool encrypt)
        {
            IDigest digest=getDigest();

            PbeParametersGenerator pGen = new Pkcs12ParametersGenerator(digest);
            pGen.Init(
                PbeParametersGenerator.Pkcs12PasswordToBytes(pbe.Password),
                pbe.Salt,
                pbe.Iterations);

            ParametersWithIV parameters = (ParametersWithIV)
                pGen.GenerateDerivedParameters(pbe.BaseAlgorithm, pbe.KeySize, pbe.IvSize);

            KeyParameter encKey = (KeyParameter) parameters.Parameters;

            IBufferedCipher c;
            if (pbe.BaseAlgorithm.Equals(Kpbe.Algorithms.RC4))
            {
                c = CipherUtilities.GetCipher(pbe.BaseAlgorithm);

                c.Init(encrypt, encKey);
            }
            else
            {
                c = CipherUtilities.GetCipher(pbe.BaseAlgorithm + "/"+pbe.Mode+"/"+pbe.Padding);

                c.Init(encrypt, parameters);
            }

            return c;
        }
Beispiel #2
0
		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");
			}
		}
Beispiel #3
0
		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");
			}
		}
Beispiel #4
0
			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");
//				}
			}