3

I'm trying to encrypt a string in Android app and then decrypt it in ASP.Net server. I'm not getting any errors but decryption doesn't return true results. Here is Android side:

public void clckBtn(View v) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(
                    "MyDifficultPassw".getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal("tryToEncrypt".getBytes());
            System.out.println(toHex(encrypted));

        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }


    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private final static String HEX = "0123456789ABCDEF";

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

The output is: CE3E99F50E6D30201E38D4955F07BA7C

Asp.Net side :

 protected void Page_Load(object sender, EventArgs e)
        {
            using (Aes myAes = Aes.Create())
            {
                string asd = DecryptStringFromBytes_Aes(GetBytes("CE3E99F50E6D30201E38D4955F07BA7C"), GetBytes("MyDifficultPassw"), myAes.IV);
                int we = 0;
            }

        }

        static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key
, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an Aes object 
            // with the specified key and IV. 
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                aesAlg.Padding = PaddingMode.None;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key
, aesAlg.IV);



                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {

                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt
, decryptor, CryptoStreamMode.Read))
                    {

                        using (StreamReader srDecrypt = new StreamReader(
csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting 

                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }

        static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

In Asp.Net Aes class requires a IV (initialization vector). In Android there is no such thing. I think the problem is something about that.

MilesDyson
  • 738
  • 1
  • 11
  • 33
  • Basically, you would need to ensure that these match: key, IV, padding, mode. I have not programmed Android, but a quick search on google pointed me to [this](http://stackoverflow.com/a/16854800/706456). Maybe you can get a solution from there. – oleksii Mar 05 '14 at 15:18
  • 1
    You're using ECB mode in the Java code and CBC mode in the .NET code. Also, I suspect this code won't actually accomplish what you're after ([related question](http://security.stackexchange.com/questions/52584/why-can-we-still-crack-snapchat-photos-in-12-lines-of-ruby)). Perhaps [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security) is more suitable? – ntoskrnl Mar 05 '14 at 16:17

1 Answers1

0

I'm not entirely sure what was wrong with my code. Maybe @ntoskrnl was right. Nevertheless I followed an example and it worked. There is the code , that I followed. I'm giving the full code in here too. In case somebody needs it:

Java side:

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
import android.util.Log;

public class Crypto {
     public static final String TAG = "smsfwd";

        private static Cipher aesCipher;
        private static SecretKey secretKey;
        private static IvParameterSpec ivParameterSpec;

        private static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
        private static String CIPHER_ALGORITHM = "AES";
        // Replace me with a 16-byte key, share between Java and C#
        private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

        private static String MESSAGEDIGEST_ALGORITHM = "MD5";

        public Crypto(String passphrase) {
            byte[] passwordKey = encodeDigest(passphrase);

            try {
                aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
            } catch (NoSuchAlgorithmException e) {
                Log.e(TAG, "No such algorithm " + CIPHER_ALGORITHM, e);
            } catch (NoSuchPaddingException e) {
                Log.e(TAG, "No such padding PKCS5", e);
            }

            secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
            ivParameterSpec = new IvParameterSpec(rawSecretKey);
        }

        public String encryptAsBase64(byte[] clearData) {
            byte[] encryptedData = encrypt(clearData);
            return  Base64.encodeToString(encryptedData, Base64.DEFAULT);
        }

        public byte[] encrypt(byte[] clearData) {
            try {
                aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            } catch (InvalidKeyException e) {
                Log.e(TAG, "Invalid key", e);
                return null;
            } catch (InvalidAlgorithmParameterException e) {
                Log.e(TAG, "Invalid algorithm " + CIPHER_ALGORITHM, e);
                return null;
            }

            byte[] encryptedData;
            try {
                encryptedData = aesCipher.doFinal(clearData);
            } catch (IllegalBlockSizeException e) {
                Log.e(TAG, "Illegal block size", e);
                return null;
            } catch (BadPaddingException e) {
                Log.e(TAG, "Bad padding", e);
                return null;
            }
            return encryptedData;
        }

        private byte[] encodeDigest(String text) {
            MessageDigest digest;
            try {
                digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM);
                return digest.digest(text.getBytes());
            } catch (NoSuchAlgorithmException e) {
                Log.e(TAG, "No such algorithm " + MESSAGEDIGEST_ALGORITHM, e);
            }

            return null;
        }
}

MainActivity:

public void clck(View v) {
        try {

            Crypto crpt = new Crypto("MyDifficultPassw");
            System.out.println(crpt.encryptAsBase64("tryToEncrypt".getBytes()));

        } catch (Exception e) {
            System.out.println(e.toString());
        }

    }

The outpıt was : 2xrT+9gHAw4Nh9H57kluwQ== ASP.Net side:

protected void Page_Load(object sender, EventArgs e)
        {
                Crypto crp = new Crypto("MyDifficultPassw");
                string asd = crp.DecryptFromBase64("2xrT+9gHAw4Nh9H57kluwQ==");
        }

        public class Crypto
        {
            private ICryptoTransform rijndaelDecryptor;
            // Replace me with a 16-byte key, share between Java and C#
            private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

            public Crypto(string passphrase)
            {
                byte[] passwordKey = encodeDigest(passphrase);
                RijndaelManaged rijndael = new RijndaelManaged();
                rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
            }

            public string Decrypt(byte[] encryptedData)
            {
                byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
                return Encoding.ASCII.GetString(newClearData);
            }

            public string DecryptFromBase64(string encryptedBase64)
            {
                return Decrypt(Convert.FromBase64String(encryptedBase64));
            }

            private byte[] encodeDigest(string text)
            {
                MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] data = Encoding.ASCII.GetBytes(text);
                return x.ComputeHash(data);
            }
        }

        static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key
, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an Aes object 
            // with the specified key and IV. 
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                aesAlg.Padding = PaddingMode.None;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key
, aesAlg.IV);



                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {

                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt
, decryptor, CryptoStreamMode.Read))
                    {

                        using (StreamReader srDecrypt = new StreamReader(
csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting 

                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }
Community
  • 1
  • 1
MilesDyson
  • 738
  • 1
  • 11
  • 33