• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

javax.crypto difficulties

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic