Message-Id: <199705081150.HAA04359@www5.clever.net>
From: "allan mays" <civstaff@civ.com>
To: <java-security@web2.javasoft.com>
Subject: Cipher DES Key Question
Date: Thu, 8 May 1997 07:41:55 -0400
I am designing a micropayments system using java and I am
testing the jce for some capabilities.... I have run into the
following problem... I need to create a Key object based upon
a string or byte array that was passed in from a file or a packet
received from the net... is this possible or must I use a
SecureRandom in order to create a key?
Below is the code I'm working with.
Any ideas or thoughts appreciated.
thanks,
allan mays
creative internet ventures,inc.
import java.security.*;
/**
*
*/
class Des {
public static void main(String[] args) {
int iR = 1;
try{
iR = desTest();
if (iR == 1)
System.out.println("Good!");
}
catch(NoSuchAlgorithmException e1) {
System.out.println("Error: NoSuchAlgorithmException");
}
catch(KeyException e2) {
System.out.println("Error: KeyException");
}
}
public static int desTest()throws NoSuchAlgorithmException, KeyException
{
int iRet = 1;
String s = new String("test1234567890");
byte[] byteTest = new byte[14];
s.getBytes (0, s.length(),byteTest, 0) ;
SecureRandom random = new SecureRandom();
KeyGenerator keygen = KeyGenerator.getInstance("DES");
keygen.initialize(random);
Key key = keygen.generateKey();
Cipher des = Cipher.getInstance("DES/ECB/PKCS#5");
des.initEncrypt(key);
byte[] ciphertext = des.crypt(byteTest);
des.initDecrypt(key);
byte[] decrypted = des.crypt(ciphertext);
for (int i = 0; i < byteTest.length; i++){
if (decrypted[i] != byteTest[i])
iRet = 0;
}
return iRet;
}
}