john smith

Greenhorn
+ Follow
since Jun 27, 2005
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by john smith

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();
}
}
}
18 years ago
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();
}
}
}
18 years ago
Hi, simple question (I think - though Ive searched & could not find an answer). I have a command line java program that I sometimes want to attach a debugger to, so I call 'java -cp $classpath -Xdebug -X...etc classname'. By default the class does not wait for input so I have 2 choices:

1. attach the debugger very quickly and hope I attch before my breakpoint is hit.
2. add some code to wait for key input to give me the time to attach.

I use option 2, and pass an extra property via -Dpause=yes. There are other ways I could do this but what I would like to do is find out if java -Xdebug is used instead of having to pass an extra property. Sounds straightforward, and it probably is, but I cannot get it...

any anwers?

thanks
j
18 years ago