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 }

 

posted @ 2014-11-11 11:15  【Winco】  阅读(717)  评论(0编辑  收藏  举报