Results 1 to 5 of 5
- 02-21-2011, 06:11 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Problem with Cipher class in javax.crypto
Hi all,
I am facing a problem when I try to encrypt and decrypt the simple string text with the Cipher class in javax.crypto. This problem comes with some combination of plainText and Key. An example combination is given below
The sample code is given below
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
String key= "hellovij";
String input = "Botlaaaa";
when i perform encryption and decryption i am getting the following output
Before Encryption input is :-->Botlaaaa
After Encryption cipher is :--> ?▐V¬◄♠┴
After Decryption input is :-->▄YA♫Γ╛♣Y (here it should be Botlaaa)
Please help me in solving this issue..
Thanks in advan:mad:ce.
- 02-21-2011, 07:39 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Seeing the code might help. As well as knowing how you are displaying the decoded String. And, hopefully, you are "storing" the encoded bytes as bytes, and not as a String. There is a world of difference, especially where ciphers are concerned.
No! Really? I never would have guessed.(here it should be Botlaaa)Take those above points into account.
Please help me in solving this issue..
Thanks in advan:mad:ce.Last edited by masijade; 02-21-2011 at 07:39 AM. Reason: typos
- 02-21-2011, 09:52 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Thanks for your reply...
Here is my code
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Crypto
{
private Cipher cipher;
private String stringKey;
private String input;
private String cipherText;
private SecretKeySpec key;
public Crypto() throws Exception {
cipher = Cipher.getInstance("DES/ECB/NoPadding");
stringKey = "hellovij";
input = "Botlaaaa";
}
public static void main(String[] args) throws Exception
{
Crypto crypto = new Crypto();
crypto.key = new SecretKeySpec(crypto.stringKey.getBytes(), "DES");
System.out.println("Before Encryption input is :-->" + crypto.input );
crypto.cipherText = crypto.encrypt(crypto.input);
System.out.println("After Encryption cipher is :-->" + crypto.cipherText );
crypto.input = crypto.decrypt(crypto.cipherText);
System.out.println("After Decryption input is :-->" + crypto.input );
}
public String encrypt(String str) throws Exception {
cipher.init(cipher.ENCRYPT_MODE,key);
byte outputBytes[] = cipher.doFinal(str.getBytes());
return new String(outputBytes);
}
public String decrypt(String str) throws Exception{
cipher.init(cipher.DECRYPT_MODE,key);
byte outputBytes[] = cipher.doFinal(str.getBytes());
return new String(outputBytes);
}
}
Please need your help
Thank you very much
- 02-21-2011, 10:07 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Like I said, don't do this
You do know that Java Strings use two-byte characters, right? And that new String(byte[]) reads those bytes as if they were in the default system encoding which probably only uses single byte characters, right? So, doing new String has just added additional bytes to your encoding. As I said, handle the encoded bytes as a nyte array, do not make a String out of it.Java Code:return new String(outputBytes);
- 02-25-2011, 09:11 AM #5
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
java.lang.NoClassDefFoundError: javax/xml/crypto/dsig/XMLSignatureException
By anoopasta in forum XMLReplies: 0Last Post: 05-15-2010, 09:26 AM -
javax.crypto.Cipher
By Bill86 in forum Advanced JavaReplies: 1Last Post: 12-18-2009, 09:17 PM -
javax.xml.ws.Service problem
By magnum99 in forum Advanced JavaReplies: 0Last Post: 10-08-2009, 06:10 PM -
How to cipher a string without using Cipher class?
By arnab321 in forum New To JavaReplies: 1Last Post: 09-08-2009, 11:19 PM -
IBM JCE: Cannot set up certs for trusted CAs at javax.crypto.f
By jevans in forum Advanced JavaReplies: 1Last Post: 10-11-2008, 04:36 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks