summary refs log tree commit diff
path: root/crypto/test/src/test/EncryptedPrivateKeyInfoTest.cs
blob: baac5ff7f4508751da550b36b4206b49eac2ce63 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;

using NUnit.Framework;

using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Test;

namespace Org.BouncyCastle.Tests
{
	[TestFixture]
	public class EncryptedPrivateKeyInfoTest
		: SimpleTest
	{
		private const string alg = "1.2.840.113549.1.12.1.3"; // 3 key triple DES with SHA-1

		public override void PerformTest()
		{
			IAsymmetricCipherKeyPairGenerator fact = GeneratorUtilities.GetKeyPairGenerator("RSA");
			fact.Init(new KeyGenerationParameters(new SecureRandom(), 512));

			AsymmetricCipherKeyPair keyPair = fact.GenerateKeyPair();

			AsymmetricKeyParameter priKey = keyPair.Private;
			AsymmetricKeyParameter pubKey = keyPair.Public;

			//
			// set up the parameters
			//
			byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			int iterationCount = 100;
			Asn1Encodable defParams = PbeUtilities.GenerateAlgorithmParameters(alg, salt, iterationCount);
			char[] password1 = { 'h', 'e', 'l', 'l', 'o' };

//				AlgorithmParameters parameters = AlgorithmParameters.getInstance(alg);
//
//				parameters.init(defParams);

			//
			// set up the key
			//
//				PBEKeySpec pbeSpec = new PBEKeySpec(password1);
//				SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg);

//				IBufferedCipher cipher = CipherUtilities.GetCipher(alg);
			IWrapper wrapper = WrapperUtilities.GetWrapper(alg);

			ICipherParameters parameters = PbeUtilities.GenerateCipherParameters(
				alg, password1, defParams);

//				cipher.Init(IBufferedCipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), parameters);
			wrapper.Init(true, parameters);

//				byte[] wrappedKey = cipher.Wrap(priKey);
			byte[] pkb = PrivateKeyInfoFactory.CreatePrivateKeyInfo(priKey).GetDerEncoded();
			byte[] wrappedKey = wrapper.Wrap(pkb, 0, pkb.Length);

			//
			// create encrypted object
			//

			// TODO Figure out what this was supposed to do
//				EncryptedPrivateKeyInfo pInfo = new EncryptedPrivateKeyInfo(parameters, wrappedKey);
			PrivateKeyInfo plain = PrivateKeyInfoFactory.CreatePrivateKeyInfo(priKey);
			EncryptedPrivateKeyInfo pInfo = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
				alg, password1, salt, iterationCount,  plain);


			//
			// decryption step
			//
			char[] password2 = { 'h', 'e', 'l', 'l', 'o' };

//				pbeSpec = new PBEKeySpec(password2);
//
//				cipher = CipherUtilities.GetCipher(pInfo.EncryptionAlgorithm);
//
//				cipher.Init(false, keyFact.generateSecret(pbeSpec), pInfo.getAlgParameters());
//
//				PKCS8EncodedKeySpec keySpec = pInfo.getKeySpec(cipher);
			PrivateKeyInfo decrypted = PrivateKeyInfoFactory.CreatePrivateKeyInfo(password2, pInfo);

//				if (!MessageDigest.isEqual(priKey.GetEncoded(), keySpec.GetEncoded()))
			if (!decrypted.Equals(plain))
			{
				Fail("Private key does not match");
			}

			//
			// using ICipherParameters test
			//
//			pbeSpec = new PBEKeySpec(password1);
//			keyFact = SecretKeyFactory.getInstance(alg);
//			cipher = CipherUtilities.GetCipher(alg);
			wrapper = WrapperUtilities.GetWrapper(alg);

//			cipher.init(IBufferedCipher.WRAP_MODE, keyFact.generateSecret(pbeSpec), parameters);
			wrapper.Init(true, parameters);

//			wrappedKey = cipher.wrap(priKey);
			wrappedKey = wrapper.Wrap(pkb, 0, pkb.Length);

			//
			// create encrypted object
			//

			// TODO Figure out what this was supposed to do
//			pInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(), wrappedKey);
			plain = PrivateKeyInfoFactory.CreatePrivateKeyInfo(priKey);
			pInfo = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo(
				alg, password1, salt, iterationCount,  plain);

			//
			// decryption step
			//
//			pbeSpec = new PBEKeySpec(password2);
//
//			cipher = CipherUtilities.GetCipher(pInfo.getAlgName());
//
//			cipher.init(IBufferedCipher.DECRYPT_MODE, keyFact.generateSecret(pbeSpec), pInfo.getAlgParameters());
//
//			keySpec = pInfo.getKeySpec(cipher);
			decrypted = PrivateKeyInfoFactory.CreatePrivateKeyInfo(password2, pInfo);

//			if (!MessageDigest.isEqual(priKey.GetEncoded(), keySpec.GetEncoded()))
			if (!decrypted.Equals(plain))
			{
				Fail("Private key does not match");
			}
		}

		public override string Name
		{
			get { return "EncryptedPrivateKeyInfoTest"; }
		}

		public static void Main(
			string[] args)
		{
			RunTest(new EncryptedPrivateKeyInfoTest());
		}

		[Test]
		public void TestFunction()
		{
			string resultText = Perform().ToString();

			Assert.AreEqual(Name + ": Okay", resultText);
		}
	}
}