decryption not working

Here us my code using crypto-js  https://code.google.com/p/crypto-js/#AES

why decryption not working. I am not getting back the value after decryption.

<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script>
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
    alert("encrypted="+encrypted);
    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");    
    alert("decrypted="+decrypted);
</script>
</head>
<body>
</body>
</html>

Open in new window

cofactorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

Put the script inside some event... like window.onload!

window.onload = function() {
    var encrypted = CryptoJS.AES.encrypt("Message", "Secret Passphrase");
    alert("encrypted="+encrypted);
    var decrypted = CryptoJS.AES.decrypt(encrypted, "Secret Passphrase");    
    alert("decrypted="+decrypted);
}
0
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Or put a button to call that script.
0
Rainer JeschorCommented:
Hi,
its "just" an encoding issue.
The following returns the correct decrypted text:
alert("decrypted="+decrypted.toString(CryptoJS.enc.Utf8) );

Open in new window

Live sample:
http://jsbin.com/dewuwabelo/2/edit?html,js,output

HTH
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cofactorAuthor Commented:
@Rainer Jeschor,
Yes. This works.

However I need some change . I want to decrypt the same in Java (but not in javascript)

I took the encrypted message from my above javascript and plugged into java AES decrypt method to decrypt using Java... it did not work out.


The following is not working.

String decrypthis="U2FsdGVkX1+yIFCljAqTM8xPxN60hj5tjAIFWc/pbmo=";  // this is  AES encrypted.

I tried calling this method  
decrypt("Secret Passphrase",decrypthis) 

Open in new window

 ....but it failed to decrypt and thrown error "java.security.InvalidKeyException: Invalid AES key length: 12 bytes"

Here is the java decrypt method :

public String decrypt(String key, String encrypted) {
		    try {
		        Key k = new SecretKeySpec(new Base64().decode(key), "AES");
		        Cipher c = Cipher.getInstance("AES");
		        c.init(Cipher.DECRYPT_MODE, k);
		        byte[] decodedValue = new Base64().decode(encrypted);
		        byte[] decValue = c.doFinal(decodedValue);
		        String decryptedValue = new String(decValue);
		        return decryptedValue;
		    } catch (IllegalBlockSizeException ex) {
		    	System.out.println(ex);
		    } catch (BadPaddingException ex) {
		    	System.out.println(ex);
		    } catch (InvalidKeyException ex) {
		    	System.out.println(ex);
		    } catch (NoSuchAlgorithmException ex) {
		    	System.out.println(ex);
		    } catch (NoSuchPaddingException ex) {
		    	System.out.println(ex);
		    }
		    return null;
		}

Open in new window



Is there any workaround ?
0
cofactorAuthor Commented:
comments please
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.