Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using JCE Cypher with DES algo and HEXADECIMAL KEY

843810Jan 27 2005 — edited Jan 28 2005
Hello

We developped an encryption mecanisme with Cypher initialized by SecretKey based on 8 characters size String.

My Customers gave me an Hexadecimal key of 16 digits and a simulation sample with DES pure Algorythme and asked me to verify if my java encryption has the same behaviour with this HEXA key.

The problem is my mecanisme with DES and SecretKey objet accept only byte[8] array <=> String 8 char size. I tried to convert the HEXA key to binary and then to bytes array and then to String 8 but no way !! usable keys but not the same result as the sample !!! :(((

for example we used this code to convert :

private static byte[] hexaToBin(String hex) {
byte value[] = new byte[hex.length() / 2];
for (int i = 0; i < value.length; i++) {
value[i] =
(byte) (Integer.parseInt(hex.substring(2 * i, 2 * i + 2), 16)
- 128);
}
return value;

}
we tried also several schemes of DES using the converted key but .. no way not the same result!

private void encrypt() {

String scheme1 = "DES/ECB/NoPadding";
String scheme2 = "DES/CBC/NoPadding";
String scheme3 = "DES/CFB/NoPadding";
String scheme4 = "DES/OFB/NoPadding";
String scheme5 = "DES/PCBC/NoPadding";

String secretKeyString = "F12C40545BD09D2F";
String input = "1055139730400002";

StringBuffer sb = new StringBuffer();

try {

DESKeySpec desKeySpec = new DESKeySpec(hexaToBin(secretKeyString));

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

Cipher cipher1 = Cipher.getInstance(scheme1);

log.debug(cipher1.getAlgorithm() + "||" + cipher1.getProvider());

cipher1.init(Cipher.ENCRYPT_MODE, secretKey);


byte[] ctext = cipher1.doFinal(hexaToBin(input));

log.debug("Hexa value = " + new HexDumpEncoder().encode(ctext));

Cipher cipher2 = Cipher.getInstance(scheme2);
log.debug(cipher2.getAlgorithm() + "||" + cipher2.getProvider());
cipher2.init(Cipher.ENCRYPT_MODE, secretKey);
ctext = cipher2.doFinal(hexaToBin(input));
log.debug("Hexa value = " + new HexDumpEncoder().encode(ctext));

Cipher cipher3 = Cipher.getInstance(scheme3);
log.debug(cipher3.getAlgorithm() + "||" + cipher3.getProvider());
cipher3.init(Cipher.ENCRYPT_MODE, secretKey);
ctext = cipher3.doFinal(hexaToBin(input));
log.debug("Hexa value = " + new HexDumpEncoder().encode(ctext));

Cipher cipher4 = Cipher.getInstance(scheme4);
log.debug(cipher4.getAlgorithm() + "||" + cipher4.getProvider());
cipher4.init(Cipher.ENCRYPT_MODE, secretKey);
ctext = cipher4.doFinal(hexaToBin(input));
log.debug("Hexa value = " + new HexDumpEncoder().encode(ctext));

Cipher cipher5 = Cipher.getInstance(scheme5);
log.debug(cipher5.getAlgorithm() + "||" + cipher5.getProvider());
cipher5.init(Cipher.ENCRYPT_MODE, secretKey);
ctext = cipher5.doFinal(hexaToBin(input));
log.debug("Hexa value = " + new HexDumpEncoder().encode(ctext));

}
catch (Exception e) {
log.error("Exception Encryption = " + e);

}
}

I can give you more detail an will be really grateful if someone can help me to use JCE with Hexa key if it's possble

sincerly yours

Raef
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2005
Added on Jan 27 2005
6 comments
307 views