01: package org.bouncycastle.jce.provider.test;
02:
03: import java.security.Key;
04: import java.security.Security;
05:
06: import javax.crypto.Cipher;
07: import javax.crypto.KeyGenerator;
08: import javax.crypto.SealedObject;
09:
10: import org.bouncycastle.jce.provider.BouncyCastleProvider;
11: import org.bouncycastle.util.test.SimpleTestResult;
12: import org.bouncycastle.util.test.Test;
13: import org.bouncycastle.util.test.TestResult;
14:
15: public class SealedTest implements Test {
16: final static String provider = "BC";
17:
18: public String getName() {
19: return "SealedObject";
20: }
21:
22: public TestResult perform() {
23: try {
24: KeyGenerator keyGen = KeyGenerator.getInstance("DES",
25: provider);
26: Key key = keyGen.generateKey();
27: Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding",
28: provider);
29:
30: c.init(Cipher.ENCRYPT_MODE, key);
31: String object = "Hello world";
32: SealedObject so = new SealedObject(object, c);
33: c.init(Cipher.DECRYPT_MODE, key);
34:
35: Object o = so.getObject(c);
36: if (!o.equals(object)) {
37: return new SimpleTestResult(false,
38: "Result object 1 not equal" + "orig: " + object
39: + " res: " + o);
40: }
41:
42: o = so.getObject(key);
43: if (!o.equals(object)) {
44: return new SimpleTestResult(false,
45: "Result object 2 not equal" + "orig: " + object
46: + " res: " + o);
47: }
48:
49: o = so.getObject(key, provider);
50: if (!o.equals(object)) {
51: return new SimpleTestResult(false,
52: "Result object 3 not equal" + "orig: " + object
53: + " res: " + o);
54: }
55:
56: return new SimpleTestResult(true, getName() + ": Okay");
57: } catch (Exception e) {
58: return new SimpleTestResult(false, getName()
59: + ": failed excpetion - " + e.toString(), e);
60: }
61: }
62:
63: public static void main(String[] args) {
64: Security.addProvider(new BouncyCastleProvider());
65:
66: Test test = new SealedTest();
67: TestResult result = test.perform();
68:
69: System.out.println(result.toString());
70: }
71: }
|