public static string Encrypt(this string plainText) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Padding = PaddingMode.Zeros; aes.Mode = CipherMode.CBC; aes.Key = Encoding.Default.GetBytes(key); aes.GenerateIV(); ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key,aes.IV); byte[] buffer = Encoding.ASCII.GetBytes(plainText); String encryptedText = Convert.ToBase64String(Encoding.Default.GetBytes(Encoding.Default.GetString(AESEncrypt.TransformFinalBlock(buffer,buffer.Length)))); String mac = ""; using (var hmacsha256 = new HMACSHA256(Encoding.Default.GetBytes(key))) { hmacsha256.ComputeHash(Encoding.Default.GetBytes(Convert.ToBase64String(aes.IV) + encryptedText)); mac = ByteArrToString(hmacsha256.Hash); } var keyValues = new Dictionary<string,object> { { "iv",Convert.ToBase64String(aes.IV) },{ "value",encryptedText },{ "mac",mac },}; JavaScriptSerializer serializer = new JavaScriptSerializer(); //return serializer.Serialize(keyValues); return Convert.ToBase64String(Encoding.ASCII.GetBytes(serializer.Serialize(keyValues))); } public static string Decrypt(this string cipherText) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Padding = PaddingMode.Zeros; aes.Mode = CipherMode.CBC; aes.Key = Encoding.Default.GetBytes(key); dynamic payload = GetJsonPayload(cipherText); //return Encoding.Default.GetString(Convert.FromBase64String(cipherText)); //cipherText = Convert.ToBase64String(Encoding.Default.GetBytes(payload["value"])); aes.IV = Convert.FromBase64String(payload["iv"]); ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key,aes.IV); byte[] buffer = Convert.FromBase64String(payload["value"]); return (Encoding.Default.GetString(AESDecrypt.TransformFinalBlock(buffer,buffer.Length))).ToString(); }
https://github.com/laravel/framework/blob/5.1/src/Illuminate/Encryption/Encrypter.php
我正在使用上面的代码,当我从Laravel解密任何东西时,它都有效.问题是当我从c#加密一个字符串时,我无法在PHP中解密它.
解决方法
将填充更改为PaddingMode.PKCS7可以正常工作!如果其他人仍然需要这个,完整的代码发布在下面.
public static string Encrypt(this string plainText) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; aes.Key = Encoding.Default.GetBytes(key); aes.GenerateIV(); ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key,}; JavaScriptSerializer serializer = new JavaScriptSerializer(); //return serializer.Serialize(keyValues); return Convert.ToBase64String(Encoding.ASCII.GetBytes(serializer.Serialize(keyValues))); } public static string Decrypt(this string cipherText) { RijndaelManaged aes = new RijndaelManaged(); aes.KeySize = 256; aes.BlockSize = 128; aes.Padding = PaddingMode.PKCS7; aes.Mode = CipherMode.CBC; aes.Key = Encoding.Default.GetBytes(key); dynamic payload = GetJsonPayload(cipherText); //return Encoding.Default.GetString(Convert.FromBase64String(cipherText)); //cipherText = Convert.ToBase64String(Encoding.Default.GetBytes(payload["value"])); aes.IV = Convert.FromBase64String(payload["iv"]); ICryptoTransform AESDecrypt = aes.CreateDecryptor(aes.Key,buffer.Length))).ToString(); }