Rijndael encryption from Action Script to C#

你说的曾经没有我的故事 提交于 2019-12-04 18:06:24

This is an implementation of Rijndael Encryption which one of my websites is currently using. See if this does the trick:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CMS.Core.Domain
{
    /// <summary>
    /// Summary description for EncryptionManager
    /// </summary>
    public static class EncryptionManager
    {
        public static string EncryptRijndael(string value, string encryptionKey) {
            try {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged {
                    BlockSize = 128,
                    IV = key,
                    KeySize = 128,
                    Key = key
                };

                var transform = rijndael.CreateEncryptor();
                using (var ms = new MemoryStream()) {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) {
                        byte[] buffer = Encoding.UTF8.GetBytes(value);

                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }
                    ms.Close();
                    return Convert.ToBase64String(ms.ToArray());
                }
            }
            catch {
                return string.Empty;
            }
        }

        public static string DecryptRijndael(string value, string encryptionKey)
        {
            try
            {
                var key = Encoding.UTF8.GetBytes(encryptionKey); //must be 16 chars
                var rijndael = new RijndaelManaged
                                               {
                                                   BlockSize = 128,
                                                   IV = key,
                                                   KeySize = 128,
                                                   Key = key
                                               };

                var buffer = Convert.FromBase64String(value);
                var transform = rijndael.CreateDecryptor();
                string decrypted;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
                    {
                        cs.Write(buffer, 0, buffer.Length);
                        cs.FlushFinalBlock();
                        decrypted = Encoding.UTF8.GetString(ms.ToArray());
                        cs.Close();
                    }
                    ms.Close();
                }

                return decrypted;
            }
            catch
            {
                return null;
            }
        }
    }
}

Update

One thing I just noticed with your input is that your encryption key is only 9 characters and my code above requires a 16 character key. I am not sure if this is a hard requirement of the Rijndael encryption algorithm, but the above code will not work with an encryption key that is not exactly 16 characters.

I came across this link which addresses both C# and ActionScript for the AES Rijndael encryption

http://ryoushin.com/cmerighi/en-us/42,2007-03-02/AES-Rijndael_with_ActionScript_and_ASP_Net.aspx

You could try this wrapper for Rijndael as it may be an issue with the IV or the passphrase padding (I'd be interested to know if it doesn't work)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!