aspose file tools*
The moose likes Java in General and the fly likes javax.crypto difficulties Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "javax.crypto difficulties" Watch "javax.crypto difficulties" New topic
Author

javax.crypto difficulties

john smith
Greenhorn

Joined: Jun 27, 2005
Posts: 3
Hi,

I would like to encrypt some data, store it in a DB (as a string), then get the data and decrypt it. My code fails with a paddingexception. I can see why - in the code below the bytearray []encryptedBAFromDB is only 16 bytes long. Regardless, even if I pad it to 32 bytes (same as encrypted[]), the values are completely different.

If I use the original bytearray (c.doFinal(encrypted)) then there is no problem - the toString() does not work obviously - though I havent been able to understand why....

Any hints appreciated.

rgds
jim

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

public class CipherTest {
public static void main(String args[]) {
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
Key key = kg.generateKey();

c.init(Cipher.ENCRYPT_MODE, key);
byte input[] = "This is a string to encrypt....".getBytes();
byte encrypted[] = c.doFinal(input);
byte iv[] = c.getIV();
System.out.println("Encrypted string: [" + encrypted + "]");
//Store this in the DB
String encryptedString=encrypted.toString();

IvParameterSpec dps = new IvParameterSpec(iv);
c.init(Cipher.DECRYPT_MODE, key, dps);

//Now 'get' from the DB as a string
String encryptedStringFromDB=encryptedString;
byte []encryptedBAFromDB=encryptedStringFromDB.getBytes();
int numBytes=encryptedBAFromDB.length;
byte[] paddedByteArray =null;
if ((numBytes % 8) != 0) {
paddedByteArray = new byte[numBytes+(8-(numBytes%8))];
for(int i = 0; i<numBytes; i++) {
paddedByteArray[i] = encryptedBAFromDB[i];
}
for(int i = numBytes; i<paddedByteArray.length; i++) {
paddedByteArray[i] = 0;
}
byte output[] = c.doFinal(paddedByteArray);
System.out.print("Decrypted string: [" + new String(output) + "]");
} else {
byte output[] = c.doFinal(encryptedBAFromDB);
System.out.print("Decrypted string: [" + new String(output) + "]");
}


} catch (Exception e) {
e.printStackTrace();
}
}
}
john smith
Greenhorn

Joined: Jun 27, 2005
Posts: 3
OK - I'll reply to myself then...;-) Seems I should have posted this to the basic java group - I found my schoolboy error - I needed to base64 encode my bytearrary to make to properly handle storing as a string.

thanks anyway (Code posted below for clarity)

rgds
j
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;

public class CipherTest {
public static void main(String args[]) {
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
Key key = kg.generateKey();

c.init(Cipher.ENCRYPT_MODE, key);
byte input[] = "This is a string to encrypt....".getBytes();
byte encrypted[] = c.doFinal(input);
byte iv[] = c.getIV();

//Store this in the DB
String encryptedString=new sun.misc.BASE64Encoder().encode(encrypted);
System.out.println("Encrypted string: [" + encryptedString + "]");

IvParameterSpec dps = new IvParameterSpec(iv);
c.init(Cipher.DECRYPT_MODE, key, dps);

//Now 'get' from the DB as a string
String encryptedStringFromDB=encryptedString;
byte []encryptedBAFromDB=new sun.misc.BASE64Decoder().decodeBuffer(encryptedStringFromDB);
byte output[] = c.doFinal(encryptedBAFromDB);
System.out.print("Decrypted string: [" + new String(output) + "]");


} catch (Exception e) {
e.printStackTrace();
}
}
}
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 436
Hi,

Good show! spreading knowledge, telling others what you have learnt is really a good act!

Maki Jav
[ December 20, 2006: Message edited by: Maki Jav ]

Help gets you when you need it!
 
 
subject: javax.crypto difficulties