Results 1 to 5 of 5
  1. #1
    vinayak.top is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Angry 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.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by vinayak.top View Post
    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
    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.
    (here it should be Botlaaa)
    No! Really? I never would have guessed.


    Please help me in solving this issue..
    Thanks in advan:mad:ce.
    Take those above points into account.
    Last edited by masijade; 02-21-2011 at 08:39 AM. Reason: typos

  3. #3
    vinayak.top is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Like I said, don't do this
    Java Code:
    return new String(outputBytes);
    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.

  5. #5
    vinayak.top is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 05-15-2010, 10:26 AM
  2. javax.crypto.Cipher
    By Bill86 in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2009, 10:17 PM
  3. javax.xml.ws.Service problem
    By magnum99 in forum Advanced Java
    Replies: 0
    Last Post: 10-08-2009, 07:10 PM
  4. How to cipher a string without using Cipher class?
    By arnab321 in forum New To Java
    Replies: 1
    Last Post: 09-09-2009, 12:19 AM
  5. Replies: 1
    Last Post: 10-11-2008, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •