001: package org.bouncycastle.jce.provider.test;
002:
003: import java.security.AlgorithmParameters;
004: import java.security.KeyPair;
005: import java.security.KeyPairGenerator;
006: import java.security.MessageDigest;
007: import java.security.PrivateKey;
008: import java.security.PublicKey;
009: import java.security.SecureRandom;
010: import java.security.Security;
011: import java.security.spec.PKCS8EncodedKeySpec;
012:
013: import javax.crypto.Cipher;
014: import javax.crypto.EncryptedPrivateKeyInfo;
015: import javax.crypto.SecretKeyFactory;
016: import javax.crypto.spec.PBEKeySpec;
017: import javax.crypto.spec.PBEParameterSpec;
018:
019: import org.bouncycastle.jce.provider.BouncyCastleProvider;
020: import org.bouncycastle.util.test.SimpleTestResult;
021: import org.bouncycastle.util.test.Test;
022: import org.bouncycastle.util.test.TestResult;
023:
024: public class EncryptedPrivateKeyInfoTest implements Test {
025: String alg = "1.2.840.113549.1.12.1.3"; // 3 key triple DES with SHA-1
026:
027: public TestResult perform() {
028: try {
029: KeyPairGenerator fact = KeyPairGenerator.getInstance("RSA",
030: "BC");
031: fact.initialize(512, new SecureRandom());
032:
033: KeyPair keyPair = fact.generateKeyPair();
034:
035: PrivateKey priKey = keyPair.getPrivate();
036: PublicKey pubKey = keyPair.getPublic();
037:
038: //
039: // set up the parameters
040: //
041: byte[] salt = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
042: int iterationCount = 100;
043: PBEParameterSpec defParams = new PBEParameterSpec(salt,
044: iterationCount);
045:
046: AlgorithmParameters params = AlgorithmParameters
047: .getInstance(alg, "BC");
048:
049: params.init(defParams);
050:
051: //
052: // set up the key
053: //
054: char[] password1 = { 'h', 'e', 'l', 'l', 'o' };
055:
056: PBEKeySpec pbeSpec = new PBEKeySpec(password1);
057: SecretKeyFactory keyFact = SecretKeyFactory.getInstance(
058: alg, "BC");
059: Cipher cipher = Cipher.getInstance(alg, "BC");
060:
061: cipher.init(Cipher.WRAP_MODE, keyFact
062: .generateSecret(pbeSpec), params);
063:
064: byte[] wrappedKey = cipher.wrap(priKey);
065:
066: //
067: // create encrypted object
068: //
069:
070: EncryptedPrivateKeyInfo pInfo = new EncryptedPrivateKeyInfo(
071: params, wrappedKey);
072:
073: //
074: // decryption step
075: //
076: char[] password2 = { 'h', 'e', 'l', 'l', 'o' };
077:
078: pbeSpec = new PBEKeySpec(password2);
079:
080: cipher = Cipher.getInstance(pInfo.getAlgName(), "BC");
081:
082: cipher.init(Cipher.DECRYPT_MODE, keyFact
083: .generateSecret(pbeSpec), pInfo.getAlgParameters());
084:
085: PKCS8EncodedKeySpec keySpec = pInfo.getKeySpec(cipher);
086:
087: if (!MessageDigest.isEqual(priKey.getEncoded(), keySpec
088: .getEncoded())) {
089: return new SimpleTestResult(false,
090: "Private key does not match");
091: }
092:
093: //
094: // using Cipher parameters test
095: //
096: pbeSpec = new PBEKeySpec(password1);
097: keyFact = SecretKeyFactory.getInstance(alg, "BC");
098: cipher = Cipher.getInstance(alg, "BC");
099:
100: cipher.init(Cipher.WRAP_MODE, keyFact
101: .generateSecret(pbeSpec), params);
102:
103: wrappedKey = cipher.wrap(priKey);
104:
105: //
106: // create encrypted object
107: //
108:
109: pInfo = new EncryptedPrivateKeyInfo(cipher.getParameters(),
110: wrappedKey);
111:
112: //
113: // decryption step
114: //
115: pbeSpec = new PBEKeySpec(password2);
116:
117: cipher = Cipher.getInstance(pInfo.getAlgName(), "BC");
118:
119: cipher.init(Cipher.DECRYPT_MODE, keyFact
120: .generateSecret(pbeSpec), pInfo.getAlgParameters());
121:
122: keySpec = pInfo.getKeySpec(cipher);
123:
124: if (!MessageDigest.isEqual(priKey.getEncoded(), keySpec
125: .getEncoded())) {
126: return new SimpleTestResult(false,
127: "Private key does not match");
128: }
129:
130: return new SimpleTestResult(true, getName() + ": Okay");
131: } catch (Exception e) {
132: return new SimpleTestResult(false, getName()
133: + ": exception - " + e.toString(), e);
134: }
135: }
136:
137: public String getName() {
138: return "EncryptedPrivateKeyInfoTest";
139: }
140:
141: public static void main(String[] args) {
142: Security.addProvider(new BouncyCastleProvider());
143:
144: Test test = new EncryptedPrivateKeyInfoTest();
145: TestResult result = test.perform();
146:
147: System.out.println(result.toString());
148: }
149: }
|