[Unity] [随风去旅行] unity3d 数据加/解密

查看:760 |回复:18 | 2015-7-10 01:32:08

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 成林 于 2018-5-16 21:39 编辑

22.jpg
现如今 手游是个很火的时代,对于做游戏的来说,这是个很好的发展趋势,
可是现在我们程序不单仅仅完成游戏功能就可以了,还要学会保护好自己的游戏数据内容。
如何去保护?
相信每一位程序都有自己独特的见解。
因此,我在此分享下自己对数据加/解密的处理方式。

好了 话不多说直接上代码:

  1. [/font]using System.Collections;
  2. using System.Text;
  3. using System.Security.Cryptography;
  4. using System;
  5. <font color="#0000ff">[font=黑体]//</font>
  6. <font color="#ff00ff">//                  _ooOoo_
  7. //                 o8888888o
  8. //                 88" . "88
  9. //                 (| -_- |)
  10. //                 O\  =  /O
  11. //              ____/`---'\____
  12. //            .'  \\|     |//  `.
  13. //          /  \\|||  :  |||//  \
  14. //          /  _||||| -:- |||||-  \
  15. //          |   | \\\  -  /// |   |
  16. //          | \_|  ''\---/''  |   |
  17. //          \  .-\__  `-`  ___/-. /
  18. //        ___`. .'  /--.--\  `. . __
  19. //     ."" '<  `.___\_<|>_/___.'  >'"".
  20. //    | | :  `- \`.;`\ _ /`;.`/ - ` : | |
  21. //    \  \ `-.   \_ __\ /__ _/   .-` /  /
  22. //=====`-.____`-.___\_____/___.-`____.-'======
  23. //                  `=---='
  24. //
  25. //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  26. //          佛祖保佑       永无Bug
  27. //          快加工资       不改需求
  28. //</font>
  29. public class ADDJIEMI : MonoBehaviour [/font]<font color="#0000ff">[font=黑体]</font>{
  30. public UIInput _input;
  31. <font color="#00ff00">//获取输入框的值</font>
  32. private string inputText;
  33. <font color="#00ff00">//被加密内容</font>
  34. private string strEncryption;
  35. private string strkeyValue;
  36. void Start()
  37. {
  38. <font color="#00ff00">//加密和解密采用相同的key,可以任意数字,但是必须为32位</font>
  39. strkeyValue = "12345678901234567890198915689039";
  40. }
  41. public void encryptionClick()
  42. {
  43. inputText = _input.value;
  44. strEncryption=encryptionContent(inputText, strkeyValue);
  45. Debug.Log(strEncryption);
  46. }
  47. public void decipherClick()
  48. {
  49. inputText = decipheringContent(strEncryption, strkeyValue);
  50. Debug.Log(inputText);
  51. }
  52. /// <summary>
  53. <font color="#00ff00">/// 内容加密</font>
  54. /// </summary>
  55. /// <param name="ContentInfo"><font color="#0000ff">要加密内容</font></param>
  56. /// <param name="strkey"><font color="#0000ff">key值</font></param>
  57. /// <returns></returns>
  58. public string encryptionContent(string ContentInfo,string strkey)
  59. {
  60. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
  61. RijndaelManaged encryption = new RijndaelManaged();
  62. encryption.Key = keyArray;
  63. encryption.Mode = CipherMode.ECB;
  64. encryption.Padding = PaddingMode.PKCS7;
  65. ICryptoTransform cTransform = encryption.CreateEncryptor();
  66. byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(ContentInfo);
  67. byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
  68. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  69. }

  70. /// <summary>
  71. <font color="#00ff00">/// 内容解密</font>
  72. /// </summary>
  73. /// <param name="encryptionContent"><font color="#0000ff">被加密内容</font></param>
  74. /// <param name="strkey"><font color="#0000ff">key值</font></param>
  75. /// <returns></returns>
  76. public string decipheringContent(string encryptionContent,string strkey)
  77. {
  78. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(strkey);
  79. RijndaelManaged decipher = new RijndaelManaged();
  80. decipher.Key = keyArray;
  81. decipher.Mode = CipherMode.ECB;
  82. decipher.Padding = PaddingMode.PKCS7;
  83. ICryptoTransform cTransform = decipher.CreateDecryptor();
  84. byte[] _EncryptArray = Convert.FromBase64String(encryptionContent);
  85. byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
  86. return UTF8Encoding.UTF8.GetString(resultArray);
  87. }
  88. }
点击此处复制文本
使用:
ngui创建一个input(输入框) 加2个button(按钮),
直接把代码拖到输入框对象上,
然后分别拖input对象给2个按钮绑定事件就可以了。

注:ngui其实不建议使用直接拖动绑定事件,
最好使用Event代码做事件,
由于我只是测试,所以就直接拖啦
以后有时间的话 我会发一篇如何使用ngui的event代码做事件  在此就不详述了

里面代码非常简洁,同时也有注释 相信大家都能看懂吧!
直接运行看效果,
点击加密后的结果


通过调用上面加密方法,就把我们的数据内容加密成乱码了哟。
再看点击解密后的结果


这样就把刚才生成的乱码,通过解密方法,还原了哟。



其实,上面方法不仅仅适用于简单字符串加/解密,仔细想想,对文本、xml等之类的文件内容 也是可行的哟!
比如:对xml加/解密
1.读取xml文件,获取返回一个string 的xml内容
2.然后通过我上面的方法 对该 内容进行一次包裹,这样在内存中就是一段乱码,别人想改都不可能的,嘿嘿
3.要使用的时候 ,就通过上面的解密方法 就可以任意操作啦


是不是既方便又安全啊 ,是的话就点个”赞“呗!


还有 服务器与客户端数据交互的时候 也可以使用哟 至少我是这样用的 哈哈哈


好啦 ,上面就是作者对数据保护的方法,如果大家还有更好的方式,求赐教,

2015-7-10 01:32:08  
 赞 赞 1

使用道具 登录

18个回答,把该问题分享到群,邀请大神一起回答。
2#
元素帖子强,满满正能量!
回复 收起回复
2015-7-10 07:36:52   回复
 赞 赞 1

使用道具 登录

3#
元素帖子强,满满正能量!
回复 收起回复
2015-7-10 08:45:18   回复
 赞 赞 1

使用道具 登录

4#
资源发布哪家强?元素首发称大王!
回复 收起回复
2015-7-10 09:03:57   回复
 赞 赞 1

使用道具 登录

5#
楼主碉堡了~
回复 收起回复
2015-7-10 09:25:14   回复
 赞 赞 1

使用道具 登录

6#
{:1_154:}
回复 收起回复
2015-7-10 09:57:02   回复
 赞 赞 1

使用道具 登录

7#
支持个,顶顶顶顶顶。。。。
回复 收起回复
2015-7-10 11:36:51   回复
 赞 赞 1

使用道具 登录

9#
我和我的小伙伴们都惊呆了!
回复 收起回复
2015-7-10 13:23:10   回复
 赞 赞 1

使用道具 登录

10#
感谢楼主分享!
回复 收起回复
2015-7-10 13:41:48   回复
 赞 赞 1

使用道具 登录

11#
感谢楼主分享!!!!
回复 收起回复
2015-7-10 18:08:34   回复
 赞 赞 1

使用道具 登录

12#
高端大气上档次,低调奢华有内涵!
回复 收起回复
2015-8-7 08:05:17   回复
 赞 赞 1

使用道具 登录

13#
元素帖子强,满满正能量!
回复 收起回复
2016-1-25 13:41:11   回复
 赞 赞 1

使用道具 登录

14#
高端大气上档次,低调奢华有内涵!
回复 收起回复
2018-3-7 23:13:33   回复
 赞 赞 1

使用道具 登录

15#
666
回复 收起回复
2018-3-10 10:06:08   回复
 赞 赞 1

使用道具 登录

16#
666
回复 收起回复
2018-10-31 08:34:39   回复
 赞 赞 1

使用道具 登录

17#
回复 收起回复
2018-12-26 07:43:03   回复
 赞 赞 1

使用道具 登录

18#
感谢分享
回复 收起回复
2018-12-26 08:42:59   回复
 赞 赞 1

使用道具 登录

19#
不错,不错
回复 收起回复
2018-12-26 14:35:54   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表