在.NET中实现PHP AES函数(C#)

时间:2010-02-05 10:00:58

标签: asp.net encryption aes

我已经完成了一项任务,我需要使用AES加密来加密用户的ID,他们想要的是我需要在网站中传递一个参数,就像这样。

网址:http://www.site.com/event/sample.jce 参数:?param =加密文本

除此之外,有一个附加的php示例,他们希望我跟进加密,但我不知道如何在.NET中转换这个?

function getEncrypt($ sStr,$ sKey,$ sIV){     $ sCipher = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$ sKey,$ sStr,MCRYPT_MODE_CFB,$ sIV);     return bin2hex($ sCipher); }

$ sStr =“13410 ## 13”;
$ sKey =“mediaservice1234”; $ sKey =“kjcemsdev3jangho”; //不要改变

$ sIV =“fs0tjwkdgh0akstp”; //不要改变

$ tmp = getEncrypt($ sStr,$ sKey,$ sIV);

有人可以帮我理解这些代码吗?或者更好,如果他们可以帮我转换这个.Nt代码?谢谢。 :)

2 个答案:

答案 0 :(得分:0)

试试这个,你需要设置FeedbackSize和Padding:

public static string Encrypt(string plaintext)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            //RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.FeedbackSize = 8;
            rijndaelCipher.Mode = CipherMode.CFB;
            rijndaelCipher.KeySize = 128;
           // rijndaelCipher.BlockSize = 128;
            rijndaelCipher.BlockSize = 128;
            rijndaelCipher.Padding = PaddingMode.Zeros;
            byte[] plaintextByte = System.Text.Encoding.ASCII.GetBytes(plaintext);
            //Rfc2898DeriveBytes
            ASCIIEncoding textConverter = new ASCIIEncoding();
            rijndaelCipher.Key = textConverter.GetBytes(PRIVATEKEY); ;
            rijndaelCipher.IV = Convert.FromBase64String(PRIVATEIV);


            ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor();

            //http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged(VS.80).aspx
            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            //Convert the data to a byte array.
            byte[] toEncrypt = textConverter.GetBytes(plaintext);


            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();
            byte[] encrypted = new byte[16];
            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();


            return Convert.ToBase64String(encrypted);
        }

答案 1 :(得分:-1)

您可以在此处找到有关在.net中使用AES加密的更多信息:

http://msdn.microsoft.com/en-us/library/system.security.cryptography.rijndaelmanaged%28VS.80%29.aspx

代码很简单。