This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

javax.crypto difficulties  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}
}
}
 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!