byte[] tempBytes = null;
byte[] myBytes = new byte[15];
Date creationDate = new Date();
ASN1Sequence sequenceASN1 = null;
ASN1InputStream inASN1 = null;
SymmetricKeyCipher symmetricKeyCipher = null;
int i = 0;
int myInt = 30;
/*
* Sample : Creating a ASN1 structure, Encrypt and decrypt it with
* symmetric key algorithm (check equality before and after), Encode and
* decode it in Base64 (check equality before and after).
*/
System.out.println("---[Bouncy Castle Sample]---");
try {
/* ASN1 Sample */
myBytes[0] = 1;
myBytes[1] = 2;
myBytes[2] = 4;
myBytes[8] = -3;
System.out.println("[ASN1 Sample]");
System.out
.println("**Sample 1 : Conctructor with direct java object**");
token1 = new Token(userId, myBytes, myInt, creationDate);
inASN1 = new ASN1InputStream(token1.getEncoded());
System.out.println("DumpAsString on Java Object");
System.out.println(ASN1Dump.dumpAsString(token1));
System.out.println("DumpAsString on Object from ASN1InputStream");
System.out.println(ASN1Dump.dumpAsString(inASN1.readObject()));
// -
System.out.println("**Sample 2 : Conctructor with ASN1 sequence**");
sequenceASN1 = ASN1Sequence.getInstance(token1.toASN1Object());
token2 = new Token(sequenceASN1);
inASN1 = new ASN1InputStream(token2.getEncoded());
System.out.println("DumpAsString on Java Object");
System.out.println(ASN1Dump.dumpAsString(token2));
System.out.println("DumpAsString on Object from ASN1InputStream");
System.out.println(ASN1Dump.dumpAsString(inASN1.readObject()));
System.out.println("--");
System.out.println("------");
System.out.println("--\n");
/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
/* Cipher Sample */
System.out.println("[Cipher Sample]");
symmetricKeyCipher = new SymmetricKeyCipher();
token1 = new Token(userId, myBytes, myInt, creationDate);
msgToCrypt = token1.getEncoded();
System.out.println("Token As String before encryption : ");
System.out.println(ASN1Dump.dumpAsString(token1));
System.out.println("**Sample 1 : Encryption**");
System.out.println("Original bytes : ");
System.out.print("[");
for (i = 0; i < msgToCrypt.length; i++) {
System.out.print(msgToCrypt[i]);
System.out.print(";");
}
System.out.println("]");
tempEncrypt = symmetricKeyCipher.encrypt(msgToCrypt);
System.out.println("Encrypted bytes : ");
System.out.print("[");
for (i = 0; i < tempEncrypt.length; i++) {
System.out.print(tempEncrypt[i]);
System.out.print(";");
}
System.out.println("]");
System.out.println("Encrypted bytes in HEX :");
System.out.println(new String(Hex.encode(tempEncrypt)));
System.out.println("**Sample 2 : Decryption**");
tempDecrypt = symmetricKeyCipher.decrypt(tempEncrypt);
System.out.println("Decrypted bytes : ");
System.out.print("[");
for (i = 0; i < tempDecrypt.length; i++) {
System.out.print(tempDecrypt[i]);
System.out.print(";");