3

So I finally found something a little more understable in implementing a AES 128 encryption for a .Net Wcf Service with the same encryption. My problem with it now is that whenever it would try to decrypt the string it would have a step where it does a FromBase64String convert which will give me an error:

static public string DecryptString(string message, string key)
{
    string output = "";
    Rijndael aes = new RijndaelManaged();

    try
    {
        byte[] encrypted = Convert.FromBase64String(message);
        byte[] cipherText = GetCipherText(encrypted);

        aes.Key = Convert.FromBase64String(key);
        aes.Mode = CipherMode.CBC;

        aes.IV = GetIV(encrypted);

        using (MemoryStream ms = new MemoryStream())
        {
            using (ICryptoTransform decryptor = aes.CreateDecryptor())
            {
                using (CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Write))
                {
                    cs.Write(cipherText, 0, cipherText.Length);
                    cs.FlushFinalBlock();

                    byte[] decrypted = ms.ToArray();
                    output = Encoding.UTF8.GetString(decrypted);
                }
            }
        }
    }

The error is:

Index was outside the bounds of the array.

and it happens on the

cs.FlushFinalBlock();

This is the encryption that it produced for the message "heythere" and a key of "25f9e794323b453885f5181f1b624d0b"

0suql40BUGiDoFA4SdXJAA==

This is coming from my .Net encryption:

unNWQfm9RaU/HgKlDNEmoXZaTzsuBoTNsA2UvDKZhc4=

PS For the iPhone encryption of AES 128, this is where I got the codes from:

AES interoperability between .Net and iPhone?

Community
  • 1
  • 1
gdubs
  • 2,724
  • 9
  • 55
  • 102
  • Where is the iPhone code? The question you reference does not have it (the link in the question points to an empty page). One thing I noticed is that you are using the encrypted text as the IV; that's almost certainly wrong. – Ameen Feb 05 '13 at 07:09
  • 1
    Also, AES is a "specialized" version of Rijndael. You need to make sure you set the correct key size and paddings ( `KeySize = 256; BlockSize = 128; Mode = CipherMode.CBC; Padding = PaddingMode.PKCS7`) – Ameen Feb 05 '13 at 07:12
  • @gdubs: Your code might be astonishment to some people. Accomplish it at least in a correct braced block would be helpful in readability. – Ken Kin Feb 05 '13 at 07:14
  • I don't see any Objective-C. Why is the question tagged as such? – Jonathan Grynspan Feb 09 '13 at 00:43
  • The error is not reproducible. Please provide a correct minimum working example. – Ark-kun Feb 10 '13 at 19:56

2 Answers2

0

Look at AES Keeping your documents secure article on MSDN and scroll down to the section entitled "Using the AES Class", "Figure 15 Using AES" has a nice and simple example of encryption / decryption.

P.S. Be aware of padding that is used in certain block cypher (like AES) modes (like CBC), these can be a pain when different systems are talking to each other as padding / block sizes can vary from one system to another... see padding fun

EDIT: Just spotted this little gem... simple-2-way-encryption-for-c-sharp

Community
  • 1
  • 1
Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
0

You can find a ready sample for both iPhone and .NET here

In general, cipher algorithm are universal and should work across different languages just you need to ensure that both algorithms (i.e. in .NET and iPhone):

  • Using exactly the the same key that is encoded in the same way for storage/transmission (typically base64)
  • Have exactly same parameters including padding schema, mode of operation (e.g. CBC/ECB)
iTech
  • 18,192
  • 4
  • 57
  • 80
  • that's actually what I tried to use to encrypt. decrypting is where i have some problems with since I did it differently from how he did it. – gdubs Feb 10 '13 at 15:50