0

I try to encrypt a String using AES Encrypt for different platform. I can successfully execute it PHP and Java. But when i try it in ASP .NET its giving different Value. JAVA Code

String input="Text";
String key="1234567891234567";
    byte[] crypted = null;
            try {
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, skey);
                crypted = cipher.doFinal(input.getBytes());
            } catch (Exception exception) {
                throw exception;
            }
            return new String(Base64.encodeBase64(crypted));

ASP Code:

AesManaged tdes = new AesManaged();
 tdes.Key = Encoding.UTF8.GetBytes("1234567891234567");
                    tdes.Mode = CipherMode.ECB;
                    tdes.Padding = PaddingMode.PKCS7;
                    ICryptoTransform crypt = tdes.CreateEncryptor();
                    byte[] plain = Encoding.UTF8.GetBytes(Text);
                    byte[] cipher = crypt.TransformFinalBlock(plain, 0,plain.Length);
                    encryptedText = Convert.ToBase64String(cipher);

What i am doing wrong in second part? Thanks

2 Answers 2

1

Two possible problems with your code examples. I can't currently test the ASP code, so this is a theory at the moment:

  1. In your Java code you call getBytes() on two occasions and fail to supply a charset. This means you will use your default platform charset. Conversely, in your ASP code you specify UTF-8. I would strongly recommend you change your Java code to getBytes("UTF-8") to force it to match.

  2. In a similar vein, you return your base64 result using the following code:

    return new String(Base64.encodeBase64(crypted));
    

    I'm assuming this is commons-code Base64? If so, the encodeBase64 method returns UTF-8 bytes, yet you are relying again on your default platform charset when interpreting the bytes into a string. Either change this to:

    return new String(Base64.encodeBase64(crypted), "UTF-8");
    

    or use the convenience method:

    return Base64.encodeBase64String(crypted);
    

If your platform's default encoding is not UTF-8, then the above errors may be causing your problems. If your default encoding is UTF-8, then the changes above will ensure your code is more portable (but probably won't fix your problem).

Sign up to request clarification or add additional context in comments.

Comments

0

You can use below code to get exact same result as your JAVA code.

Visual Basic Code:

Imports System.Security.Cryptography

Public Shared Function encrypt(data As String, key As String)
    Dim tdes As AesManaged = New AesManaged()
    tdes.Key = Encoding.UTF8.GetBytes(key)
    tdes.Mode = CipherMode.ECB
    tdes.Padding = PaddingMode.PKCS7
    Dim crypt As ICryptoTransform = tdes.CreateEncryptor()
    Dim plain As Byte() = Encoding.UTF8.GetBytes(data)
    Dim cipher As Byte() = crypt.TransformFinalBlock(plain, 0, plain.Length)
    Dim encryptedText As String = Convert.ToBase64String(cipher)
    Return encryptedText
End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.