C#中的PHP MCRYPT_RIJNDAEL_128加密

前端之家收集整理的这篇文章主要介绍了C#中的PHP MCRYPT_RIJNDAEL_128加密前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试用C#重写这个函数.但是C#输出PHP不匹配

PHP版本

  1. // Encrypt data using AES128-cbc
  2. function encrypt($data,$key,$iv) {
  3. $cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
  4. mcrypt_generic_init($cipher,$iv);
  5. $multipass = mcrypt_generic($cipher,$data);
  6. mcrypt_generic_deinit($cipher);
  7. return $multipass;
  8. }

C#版本

  1. public static string encrypt(string encryptionString,string iv,string key)
  2. {
  3. byte[] clearTextBytes = Encoding.UTF8.GetBytes(encryptionString);
  4. var rijn = SymmetricAlgorithm.Create();
  5. rijn.KeySize = 128;
  6. rijn.Mode = CipherMode.CBC;
  7. var ms = new MemoryStream();
  8. var cs = new CryptoStream(ms,rijn.CreateEncryptor(Encoding.UTF8.GetBytes(key),Encoding.UTF8.GetBytes(iv)),CryptoStreamMode.Write);
  9. cs.Write(clearTextBytes,clearTextBytes.Length);
  10. cs.Close();
  11. var tmp = Encoding.UTF8.GetString(ms.ToArray());
  12. return tmp;
  13. }

解决方法

使用PHP加密/解密:
  1. class Cipher {
  2. private $key,$iv;
  3. function __construct() {
  4. $this->key = "edrtjfjfjlldldld";
  5. $this->iv = "56666852251557009888889955123458";
  6. }
  7. function encrypt($text) {
  8.  
  9. $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_256,MCRYPT_MODE_CBC);
  10. $padding = $block - (strlen($text) % $block);
  11. $text .= str_repeat(chr($padding),$padding);
  12. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256,$this->key,$text,MCRYPT_MODE_CBC,$this->iv);
  13.  
  14. return base64_encode($crypttext);
  15. }
  16.  
  17. function decrypt($input) {
  18. $dectext = mcrypt_decrypt(MCRYPT_RIJNDAEL_256,base64_decode($input),$this->iv);
  19. return $dectext;
  20. }
  21. }

使用C#加密/解密:

  1. public class RijndaelSimple
  2. {
  3. const string iv = "56666852251557009888889955123458";
  4. const string key = "edrtjfjfjlldldld";
  5.  
  6. static public String EncryptRJ256(string plainText)
  7. {
  8. var encoding = new UTF8Encoding();
  9. var Key = encoding.GetBytes(key);
  10. var IV = encoding.GetBytes(iv);
  11. byte[] encrypted;
  12.  
  13. using (var rj = new RijndaelManaged())
  14. {
  15. try
  16. {
  17. rj.Padding = PaddingMode.PKCS7;
  18. rj.Mode = CipherMode.CBC;
  19. rj.KeySize = 256;
  20. rj.BlockSize = 256;
  21. rj.Key = Key;
  22. rj.IV = IV;
  23.  
  24. var ms = new MemoryStream();
  25.  
  26. using (var cs = new CryptoStream(ms,rj.CreateEncryptor(Key,IV),CryptoStreamMode.Write))
  27. {
  28. using (var sr = new StreamWriter(cs))
  29. {
  30. sr.Write(plainText);
  31. sr.Flush();
  32. cs.FlushFinalBlock();
  33. }
  34. encrypted = ms.ToArray();
  35. }
  36. }
  37. finally
  38. {
  39. rj.Clear();
  40. }
  41. }
  42.  
  43. return Convert.ToBase64String(encrypted);
  44. }
  45.  
  46. static public String DecryptRJ256(string input)
  47. {
  48. byte[] cypher = Convert.FromBase64String(input);
  49.  
  50. var sRet = "";
  51.  
  52. var encoding = new UTF8Encoding();
  53. var Key = encoding.GetBytes(key);
  54. var IV = encoding.GetBytes(iv);
  55.  
  56. using (var rj = new RijndaelManaged())
  57. {
  58. try
  59. {
  60. rj.Padding = PaddingMode.PKCS7;
  61. rj.Mode = CipherMode.CBC;
  62. rj.KeySize = 256;
  63. rj.BlockSize = 256;
  64. rj.Key = Key;
  65. rj.IV = IV;
  66. var ms = new MemoryStream(cypher);
  67.  
  68. using (var cs = new CryptoStream(ms,rj.CreateDecryptor(Key,CryptoStreamMode.Read))
  69. {
  70. using (var sr = new StreamReader(cs))
  71. {
  72. sRet = sr.ReadLine();
  73. }
  74. }
  75. }
  76. finally
  77. {
  78. rj.Clear();
  79. }
  80. }
  81.  
  82. return sRet;
  83. }
  84.  
  85. }

猜你在找的C#相关文章