This discussion is archived
12 Replies Latest reply: Sep 12, 2012 3:39 AM by EJP RSS

Encrption Decrption in Web Service

Ajay Sharma Newbie
Currently Being Moderated
Hi..

I am using AES in my web service for encryption Decryption.

Problem I am facing it while Decrpting I am again getting some code.

For eg.

I passed "Scott" String to encrpt method it returns me encrpted string
Them i copied that string and passed it as paramaeter to decrpt method but it didn't returned me scott. It returned me another key..



What is the problem can any body tell.


Regards,
Ajay Sharma
  • 1. Re: Encrption Decrption in Web Service
    tschodt Pro
    Currently Being Moderated
    Ajay Sharma wrote:
    I am using AES in my web service for encryption Decryption.

    Problem I am facing it while Decrpting I am again getting some code.

    For eg.

    I passed "Scott" String to encrpt method it returns me encrpted string
    Them i copied that string and passed it as paramaeter to decrpt method but it didn't returned me scott. It returned me another key..

    What is the problem can any body tell.
    Does your AES encryption produce a text string as output, or binary?

    Maybe you need to pass the AES encryption output through a base64 encoder to make it text,
    text that you can copy and paste.

    And again through a base64 decoder before passing it to AES decryption.
  • 2. Re: Encrption Decrption in Web Service
    Ajay Sharma Newbie
    Currently Being Moderated
    code snippet for encryption :

    ----------------------
    aesCipher.init(Cipher.ENCRYPT_MODE, key);

    byte[] encValue = aesCipher.doFinal(valueToEncypt.getBytes());

    System.out.println("Encryption encoding");
    encryptedValue = new BASE64Encoder().encode(encValue);
    System.out.println("Encrypted Value = "+encryptedValue );
    -------------------------

    code snippet for decryption:

    -----------------------

    aesCipher.init(Cipher.DECRYPT_MODE, key);
    byte[] encValue = valueToDecrypt.getBytes();

    byte[] plainTxtBytes = aesCipher.doFinal(encValue);
    decryptedValue = plainTxtBytes.toString();

    -----------------------------

    Is any changes required ?


    Regards,
    Ajay
  • 3. Re: Encrption Decrption in Web Service
    handat Expert
    Currently Being Moderated
    You need to base64 decode your string before you try to decrypt it.
  • 4. Re: Encrption Decrption in Web Service
    tschodt Pro
    Currently Being Moderated
    handat wrote:
    You need to base64 decode your string before you try to decrypt it.
    I was assuming he was only using the base64 value for display purposes
    but we cannot tell from the incomplete code he posts.
  • 5. Re: Encrption Decrption in Web Service
    handat Expert
    Currently Being Moderated
    tschodt wrote:
    handat wrote:
    You need to base64 decode your string before you try to decrypt it.
    I was assuming he was only using the base64 value for display purposes
    but we cannot tell from the incomplete code he posts.
    I was assuming he was sending encryptedValue which he base64 encoded, but you are right, he could had discarded that and send something else.
  • 6. Re: Encrption Decrption in Web Service
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi..

    I Achieved Encryption Decryption with AES algorithm using my own key.

    But now new problem came. I have a very large string
    I am able to Encrypt it and Decrypted in Normal class contains main method. But when I am trying same in web service it not decrypting the value.

    It getting stuck at following code while decrypting in web service

    -------------------------

    cipherText = cipher.doFinal(cipherText);

    -------------------------

    Why this is happening. I am able to encrypt decrypt all except the big string. and that too in web service only not in general class.


    Regards,
    Ajay Sharma
  • 7. Re: Encrption Decrption in Web Service
    EJP Guru
    Currently Being Moderated
    How about you take the hint and post the entire method?
  • 8. Re: Encrption Decrption in Web Service
    Ajay Sharma Newbie
    Currently Being Moderated
    HI..

    here is my decrypt method code.
        private final static String characterEncoding = "UTF-8";
        private final static String cipherTransformation = "AES/CBC/PKCS5Padding";
        private final static String aesEncryptionAlgorithm = "AES";
    
    // Method for caller
     public String decrypt(String encryptedText, String key)
        {   
            String decryptedValue="";
            
            try{    
                //byte[] cipheredBytes = Base64.decode(encryptedText, Base64.DEFAULT);
                byte[] cipheredBytes = new BASE64Decoder().decodeBuffer(encryptedText);
                System.out.println("Ciphered Byte Value = "+cipheredBytes );
                byte[] keyBytes = getKeyBytes(key);
                System.out.println("Key Bytes Value = "+keyBytes );
                byte[] decryptBytes = decrypt(cipheredBytes, keyBytes, keyBytes);
                System.out.println("Decrypted Bytes Value = "+decryptBytes );
                decryptedValue = new String(decryptBytes);
                System.out.println("Decrypted Value = "+decryptedValue );
            //return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
            }
            catch(Exception e){
            
                e.getMessage();
            }
             return(decryptedValue);
        }
    
    ----------------------
    
        private static byte[] getKeyBytes(String key) throws UnsupportedEncodingException{
        byte[] keyBytes= new byte[16];
        byte[] parameterKeyBytes= key.getBytes(characterEncoding);
        System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
        return keyBytes;
        }
    
    
    ------------------------------------
    
        public  static byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws Exception
        {
            System.out.println("Inside Decrpt method return byte [] Value ");
            
            Cipher cipher = Cipher.getInstance(cipherTransformation);
            System.out.println("Inside Decrpt method Cipher Value = "+cipher);
            
            SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);
            System.out.println("Inside Decrpt method secretKeySpecy Value = "+secretKeySpecy);
            
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
            System.out.println("Inside Decrpt method ivParameterSpec Value = "+ivParameterSpec);
            
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
            System.out.println("Inside Decrpt method DECRYPT_MODE ");
            
            cipherText = cipher.doFinal(cipherText); // Not executing anything from this line.
            
            System.out.println("Inside Decrpt method cipherText Value = "+cipherText);
            
            return cipherText;
        }
    ----------------------

    Does any body why this code not working in Web service ?


    Regards,
    Ajay

    Edited by: Ajay Sharma on Sep 7, 2012 5:54 PM

    Edited by: EJP on 7/09/2012 22:37: added the missing {noformat}
    {noformat} tags. Please use them in future. Otherwise your code is illegible and you won't attract answers.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 9. Re: Encrption Decrption in Web Service
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi..

    Thanks for applying code tags..

    How you edited my post ?? Its interesting :)


    Do you know the problem with my code ? Why it's not working properly




    Regards,
    Ajay

    Edited by: Ajay Sharma on Sep 7, 2012 6:20 PM
  • 10. Re: Encrption Decrption in Web Service
    EJP Guru
    Currently Being Moderated
    Ajay Stop bumping this thread. It will either get answered or not, nothing you can do about it. If you bump it again it will be locked.
  • 11. Re: Encrption Decrption in Web Service
    Ajay Sharma Newbie
    Currently Being Moderated
    Hi..

    I posted questions last time. I thought i forgot to post so I posted today. Now I think it got removed.

    Will you tell me WHY post is removed.


    Regards,
    Ajay
  • 12. Re: Encrption Decrption in Web Service
    EJP Guru
    Currently Being Moderated
    Because bumps are not welcome here, as I just said.

    Locking this thread.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points