微信小程式加密解密 C# 以及 “填充無效,無法被移除錯誤” 解決方案
阿新 • • 發佈:2020-08-20
1 using System; 2 using System.Security.Cryptography; 3 using System.Text; 4 5 namespace Wechat 6 { 7 public static class Security 8 { 9 public static string Decrypt(string key, string iv, string data) 10 { 11 var rgbKey = Convert.FromBase64String(key); 12var rgbIV = Convert.FromBase64String(iv); 13 var bytes = Convert.FromBase64String(data); 14 15 try 16 { 17 using var managed = new AesManaged() 18 { 19 Mode = CipherMode.CBC, 20 BlockSize = 128, 21 Padding = PaddingMode.PKCS7 22 }; 23 using var decryptor = managed.CreateDecryptor(rgbKey, rgbIV); 24 var final = decryptor.TransformFinalBlock(bytes, 0, bytes.Length); 25 return Encoding.UTF8.GetString(final); 26} 27 catch 28 { 29 return null; 30 } 31 } 32 33 public static string Encrypt(string key, string iv, string data) 34 { 35 var rgbKey = Convert.FromBase64String(key); 36 var rgbIV = Convert.FromBase64String(iv); 37 var bytes = Encoding.UTF8.GetBytes(data); 38 39 try 40 { 41 using var managed = new AesManaged() 42 { 43 Mode = CipherMode.CBC, 44 BlockSize = 128, 45 Padding = PaddingMode.PKCS7 46 }; 47 using var encryptor = managed.CreateEncryptor(rgbKey, rgbIV); 48 var final = encryptor.TransformFinalBlock(bytes, 0, bytes.Length); 49 return Convert.ToBase64String(final); 50 } 51 catch 52 { 53 return null; 54 } 55 } 56 } 57 }
實現加密解密 程式碼
1 using System; 2 using System.IO; 3 using System.Security.Cryptography; 4 5 namespace RijndaelManaged_Example 6 { 7 class RijndaelExample 8 { 9 public static void Main() 10 { 11 try 12 { 13 14 string original = "Here is some data to encrypt!"; 15 16 // Create a new instance of the RijndaelManaged 17 // class. This generates a new key and initialization 18 // vector (IV). 19 using (RijndaelManaged myRijndael = new RijndaelManaged()) 20 { 21 22 myRijndael.GenerateKey(); 23 myRijndael.GenerateIV(); 24 // Encrypt the string to an array of bytes. 25 byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV); 26 27 // Decrypt the bytes to a string. 28 string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV); 29 30 //Display the original data and the decrypted data. 31 Console.WriteLine("Original: {0}", original); 32 Console.WriteLine("Round Trip: {0}", roundtrip); 33 } 34 35 } 36 catch (Exception e) 37 { 38 Console.WriteLine("Error: {0}", e.Message); 39 } 40 } 41 static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV) 42 { 43 // Check arguments. 44 if (plainText == null || plainText.Length <= 0) 45 throw new ArgumentNullException("plainText"); 46 if (Key == null || Key.Length <= 0) 47 throw new ArgumentNullException("Key"); 48 if (IV == null || IV.Length <= 0) 49 throw new ArgumentNullException("IV"); 50 byte[] encrypted; 51 // Create an RijndaelManaged object 52 // with the specified key and IV. 53 using (RijndaelManaged rijAlg = new RijndaelManaged()) 54 { 55 rijAlg.Key = Key; 56 rijAlg.IV = IV; 57 58 // Create an encryptor to perform the stream transform. 59 ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); 60 61 // Create the streams used for encryption. 62 using (MemoryStream msEncrypt = new MemoryStream()) 63 { 64 using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 65 { 66 using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 67 { 68 69 //Write all data to the stream. 70 swEncrypt.Write(plainText); 71 } 72 encrypted = msEncrypt.ToArray(); 73 } 74 } 75 } 76 77 78 // Return the encrypted bytes from the memory stream. 79 return encrypted; 80 81 } 82 83 static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV) 84 { 85 // Check arguments. 86 if (cipherText == null || cipherText.Length <= 0) 87 throw new ArgumentNullException("cipherText"); 88 if (Key == null || Key.Length <= 0) 89 throw new ArgumentNullException("Key"); 90 if (IV == null || IV.Length <= 0) 91 throw new ArgumentNullException("IV"); 92 93 // Declare the string used to hold 94 // the decrypted text. 95 string plaintext = null; 96 97 // Create an RijndaelManaged object 98 // with the specified key and IV. 99 using (RijndaelManaged rijAlg = new RijndaelManaged()) 100 { 101 rijAlg.Key = Key; 102 rijAlg.IV = IV; 103 104 // Create a decryptor to perform the stream transform. 105 ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); 106 107 // Create the streams used for decryption. 108 using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 109 { 110 using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 111 { 112 using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 113 { 114 // Read the decrypted bytes from the decrypting stream 115 // and place them in a string. 116 plaintext = srDecrypt.ReadToEnd(); 117 } 118 } 119 } 120 121 } 122 123 return plaintext; 124 125 } 126 } 127 }