unity3d 数据加/解密

unity3D项目中的c#脚本加解密方法。

1、加密

 1 /// <summary>
 2 
 3 /// 内容加密
 4 
 5 /// </summary>
 6 
 7 /// <param name="ContentInfo">要加密内容</param>
 8 
 9 /// <param name="strkey">key值</param>
10 
11 /// <returns></returns>
12 
13 public string encryptionContent(string ContentInfo,string strkey)
14 
15 {
16 
17 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
18 
19 RijndaelManaged encryption = new RijndaelManaged();
20 
21 encryption.Key = keyArray;
22 
23 encryption.Mode = CipherMode.ECB;
24 
25 encryption.Padding = PaddingMode.PKCS7;
26 
27 ICryptoTransform cTransform = encryption.CreateEncryptor();
28 
29 byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(ContentInfo);
30 
31 byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
32 
33 return Convert.ToBase64String(resultArray, 0, resultArray.Length);
34 
35 }

2、解密

 1 /// <summary>
 2 
 3 /// 内容解密
 4 
 5 /// </summary>
 6 
 7 /// <param name="encryptionContent">被加密内容</param>
 8 
 9 /// <param name="strkey">key值</param>
10 
11 /// <returns></returns>
12 
13 public string decipheringContent(string encryptionContent,string strkey)
14 
15 {
16 
17 byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
18 
19 RijndaelManaged decipher = new RijndaelManaged();
20 
21 decipher.Key = keyArray;
22 
23 decipher.Mode = CipherMode.ECB;
24 
25 decipher.Padding = PaddingMode.PKCS7;
26 
27 ICryptoTransform cTransform = decipher.CreateDecryptor();
28 
29 byte[] _EncryptArray = Convert.FromBase64String(encryptionContent);
30 
31 byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
32 
33 return UTF8Encoding.UTF8.GetString(resultArray);
34 
35 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。