Micro-channel encryption and decryption applet C # and fill the void, can not be removed wrong solution Padding is invalid and can not be removed

Decrypt the encrypted source code

 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);
12             var 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 }

Second, talk about why the first time or intermittent decryption failure?

Micro letter applet code button click I started -> get iv, encryptedData -> wx.login -> get code -> code, iv, encryptedData sending server -> then get session_key then decrypt the code,

Where is the problem then, click on the button in place, no wx.login get code when, iv and encryptedData existed a session_key, and after session_key get is different,

and so

Before clicking the "Get Info" button should first get code or use wx.login session_key, then click the button, so the onload inside or onshow should wx.login get code or session_key

Guess you like

Origin www.cnblogs.com/microestc/p/11828654.html