0

I am working on a wp8 app that requires encryption and decryption. Actually I need a way to encrypt some data using AES. I already have the key(as a string). I need a c# equivalent for the below java code can somebody help?

public static String encryptWithAES(String payload, String aesKey) {

    byte[] raw = aesKey.getBytes();
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES/ECB/PKCS5Padding");
    try {
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        byte[] encrypted;
        encrypted = cipher.doFinal(payload.getBytes());
        cipher = null;
        return Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (Exception e) {
        System.out.println("Error in encryptWithAES!!!");
        e.printStackTrace();
    }
    return null;
}

Here is what i did :

public static byte[] EncryptWithAES(string dataToEncrypt, String Key)
    {

        byte[] encryptedData;

        byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(Key);


        using (AesManaged aesEnc = new AesManaged())
        {
            aesEnc.Key = keyBytes;
            aesEnc.IV = new byte[16];

            //Create encryptor for converting
            ICryptoTransform encryptor = aesEnc.CreateEncryptor(aesEnc.Key, aesEnc.IV);


            using (MemoryStream memStream = new MemoryStream())
            {
                using (CryptoStream crypStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter srmWriter= new StreamWriter(crypStream))
                    {

                        srmWriter.Write(dataToEncrypt);
                    }
                    encryptedData = memStream.ToArray();
                }
            }
        }
        return encryptedData;
    }

The error I am getting is where i set the key: ie aesEnc.Key= keyBytes;

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
Akhil S A
  • 93
  • 7
  • We are not a code translation service. Have you made an attempt at this yet? It is easier to help you when you have already started it. – gunr2171 Aug 18 '14 at 20:20
  • possible duplicate of [Using AES encryption in C#](http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp) – Elliott Frisch Aug 18 '14 at 20:23
  • @gunr2171: I have already tried this. But sorry sir, with no fruit.I am new to this and am very sry if this kind of questions are not welcome here. @ Elliot: i referred that and tried. Idont know what is going wrong. Iam uploading my code here. Thanks for your replies – Akhil S A Aug 18 '14 at 20:59

1 Answers1

1

Your C# code works fine. You write, "The error I am getting is where i set the key: ie aesEnc.Key= keyBytes;" but you don't mention what the error is. I would guess you are getting a Cryptographic Exception with message, "Specified key is not a valid size for this algorithm."

As an experiment, try this:

byte[] output = EncryptWithAES("Hello", Encoding.UTF8.GetString(new byte[16]));

or

byte[] output = EncryptWithAES("Hello", Encoding.UTF8.GetString(new byte[32]));

and see if you still get that error setting the key. That is, make sure your key string produces an array of either 16 or 32 bytes.

Jim Flood
  • 8,144
  • 3
  • 36
  • 48
  • Thanks Jim. Was getting the error you mentioned.sorry for not mentioning it.Now I am able to execute the function without error(provided 16 byte key). But the return value is a string: "system.Byte[]". The passed key is:"1wted3e3l9c22sdvit5b14==". data is Hello world And I call the function in another page : byte[] ans = Encryption.EncryptWithAES("Hello world", key); – Akhil S A Aug 19 '14 at 00:21
  • Hi Jim, so silly of me.. This worked actually. I was trying to log the output using wrong method. I tried Convert.ToString() . It returned the string "system.byte[]". I used Convert.ToBase64String then ang got the answer. thanks a lot for the help – Akhil S A Aug 19 '14 at 00:43