1

I've following aes encryption code in Java which I want to write it in C#, but it is not giving same output.
Java Code

public String doEncryptString(String salt, String password,String token) throws CryptoException {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            SecretKeySpec secretKeySpec = generateKeySpec(salt,password);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] inputBytes = token.getBytes();
            byte[] outputBytes = cipher.doFinal(inputBytes);
            return Base64Utils.encodeToString(outputBytes);
        } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
                | IllegalBlockSizeException ex) {
            throw new CryptoException("Error encrypting password", ex);
        }
    } 

private SecretKeySpec generateKeySpec(String salt,String password) throws CryptoException{
        try {

        String generatedkey=salt+password;
        byte[] key = generatedkey.getBytes("UTF-8");
        MessageDigest sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16); // use only first 128 bit
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        return secretKeySpec;
    } catch (NoSuchAlgorithmException | IOException ex) {
        throw new CryptoException("Error encrypting password", ex);
    }
    }

This is what I've tried in C#

public static string DoEncrypt(string salt, string password, string token)
        {
            var tdes = new AesManaged();
            tdes.Key = GenerateKey(salt, password);
            tdes.Mode = CipherMode.ECB;
            tdes.Padding = PaddingMode.PKCS7;
            ICryptoTransform crypt = tdes.CreateEncryptor();
            byte[] plain = Encoding.UTF8.GetBytes(token);
            byte[] cipher = crypt.TransformFinalBlock(plain, 0, plain.Length);
            return Convert.ToBase64String(cipher);
        }

        private static byte[] GenerateKey(string salt, string password)
        {
            string generatedkey = $"{salt}{password}";
            var key = Encoding.UTF8.GetBytes(generatedkey);
            var sha1 = SHA1Managed.Create();
            key = sha1.ComputeHash(key);
            return key.Take(16).ToArray(); // use only first 128 bit
        }

string/token to encrypt : ZHKRIWB310XVVWG315PI7UZZWU1V0YYL5WE9JL
Java output: eUjNH8kcgWtlEmuCFHMPwnCFWjy5Pye/gF+itrPs1g8AjtAEZQqlzW/v7kEt2haG
My C# code output: O8sKdJWH+XCOIbexZPEwN5NxWqpWRHC5b3ZsihT8cfBqpI1eVr3PEr9Eq39a5pMn

I don't know what I am doing wrong here. Any help would be appreciated. Thanks

Update

My apologies everyone. The code translated in C# in working fine. By mistake, I was passing different salt value. Thanks everyone.

Munesh Kumar
  • 481
  • 3
  • 10

1 Answers1

0

What's in TRANSFORMATION from the Java code?

You need also to use the same mode and padding to get the same results, meaning ECB and PKCS7 in your case.

Java seems to offer only PKCS5 padding? But it seems to be compatible with PKCS7? I'm not a Java dev and can't provide details, but there is a discussion here: https://crypto.stackexchange.com/questions/9043/what-is-the-difference-between-pkcs5-padding-and-pkcs7-padding where they say:

Some cryptographic libraries such as the SUN provider in Java indicate PKCS#5 where PKCS#7 should be used - "PKCS5Padding" should have been "PKCS7Padding". This is - with high probability - a legacy from the time that only 8 byte block ciphers such as (triple) DES symmetric cipher were available.

And by the way: for production never use ECB mode as it's unsafe.

Matt
  • 164
  • 1
  • 7
  • sorry I forgot to add constant value. In Java code, TRANSFORMATION value is "AES". I have updated in question as well. Thanks for reply. Since you asked to use same mode and padding, I think in Java the default cipher for AES is AES/ECB/PKCS5Padding, see discussion here https://stackoverflow.com/questions/6258047 . I'll look into PKCS5 padding vs PKCS7 padding. – Munesh Kumar Jun 18 '20 at 07:31