Monday, February 13, 2012

AES Encryption/Descryption Between Android and C#?


Now a days people are more concerning about securing the data while transmitting over the http.Here is a code snippets which shows that how the Data in encrypted using Two different Keys called a Pass Phrase and Salt Key and Encrypt the data Also a code to decrypt the encrypted data in C# using the same Pass Phrase and Salt key.
Here Salt Key is just a Name give to a IVParamSpec Parameter.
//Android Code 

public class MyCrypto {
    public static final String TAG = "crypto";

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

    private static String TRANSFORMATION_CIPHER = "AES/CBC/PKCS5Padding";
    private static String ALGORITHM_CIPHER = "AES";
    // Replace below with a 16-byte key, share between Java and C#
    private static byte[] saltSecretKey = {0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f};

    private static String ALGORITHM_DIGEST = "MD5";

    public MyCrypto(String passphrase) {
        byte[] passwordKey = encodeDigest(passphrase);
        try {
            aesCipher = Cipher.getInstance(TRANSFORMATION_CIPHER);
            secretKey = new SecretKeySpec(passwordKey, ALGORITHM_CIPHER);
            ivParameterSpec = new IvParameterSpec(saltSecretKey);
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "No such algorithm " + ALGORITHM_CIPHER, e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "No such padding PKCS5", e);
        }              
    }

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

    public byte[] encrypt(byte[] clearData) {
    byte[] encryptedData;
        try {
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
            encryptedData = aesCipher.doFinal(clearData);
        } catch (InvalidKeyException e) {
            Log.e(TAG, "Invalid key", e);
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            Log.e(TAG, "Invalid algorithm " + ALGORITHM_CIPHER, e);
            return null;
        } 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(ALGORITHM_DIGEST);            
            return digest.digest(text.getBytes());
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "No such algorithm " + ALGORITHM_DIGEST, e);
        }
        return null;
    }    
}

// C# Code

public class MyCrypto
{
private ICryptoTransform rijndaelDecryptor;
// Replace below with a 16-byte key, share between Java and C#
private static byte[] saltSecretKey = {0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f};

public MyCrypto(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);
}
}



2 comments:

  1. What is rawsecretkey here?? in c# code

    ReplyDelete
  2. It’s quite shocking to see when your Binance account is temporary disabled all of sudden? The worst part is you are not even aware of the reasons behind the error. To know the reason behind the issue, you are always free to speak to the skilled professionals who are Binance Support NUmber ready to take your queries and provide the best results. Dial Binance support number 877-209-3306 which is the alluring way to get your problems fixed under the assistance of professionals.

    ReplyDelete