1

I am trying to use JCrytion 3.0.1 to encrypt data in javascript and then decrypt it on server by java. My question is how I can get the encrypted string to decrypt it in java using Cipher class which requires Byte[] to process.
This is how I generate my key from java

final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
final KeyPair key = keyGen.generateKeyPair();


And then to decrypt data I used

public  String decrypt(byte[] text, PrivateKey key) {
    byte[] dectyptedText = null;
    try {
        final Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        dectyptedText = cipher.doFinal(text); // this is where I get error when trying to use getBytes() to convert encrypted string to byte[]
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return new String(dectyptedText);
}


This is my javascript on client side

var encryptedUser = $.jCryption.encrypt(username, keys);
var encryptedPassword = $.jCryption.encrypt(password, keys);
            alert("Start Submiting");
            $.ajax({
                url: "LoginAuthentication",
                data:{username:encryptedUser, password:encryptedPassword},
                type:'POST'
            }).done(function(){
                alert("Successfully Access !!!");
            });

I try to use getBytes() but it doesn't work
It works well with other texts I test. My Only problem is how to convert encrypted string from javascript using JCryption to byte[] to decrypt it in Java.
Thank You

looooongname
  • 107
  • 2
  • 14
  • Use SSL (https) for anything involving authentication! https://www.owasp.org/index.php/Authentication_Cheat_Sheet – Andreas Jan 01 '15 at 10:44
  • I've never encountered that protocol, but it seems to consist of pure crap. Honestly, what's the point? It's not even safe for confidentiality, it doesn't provide authentication nor integrity. It just provides a fake sense of security. – Maarten Bodewes Jan 01 '15 at 18:54
  • Thank You for Your Recommendations! – looooongname Jan 01 '15 at 21:38

0 Answers0