当前位置:   article > 正文

Unity数据的加密与解密_unity 对base64字符串加密解密

unity 对base64字符串加密解密

好久没有更新博客了大笑

在这里分享在Unity中对数据的加密与解密的处理


加密与解密一般可以应用到文件配置,但是我主要运用到客户端与服务器端交互时候

【加密】

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. public class Encryption : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// 需要加密内容
  11. /// </summary>
  12. public InputField inputCtt;
  13. /// <summary>
  14. /// 加密结果
  15. /// </summary>
  16. public InputField inputRes;
  17. /// <summary>
  18. /// 32位任意数值,作为是加密解码约定数字
  19. /// </summary>
  20. private string keyValue = "01234567890123456789012345678901";
  21. public void OnBtnEncryption()
  22. {
  23. if (inputCtt.text.Length != 0)
  24. {
  25. string str = ConductEncryption(inputCtt.text, keyValue);
  26. inputRes.text = str;
  27. }
  28. else
  29. {
  30. Debug.Log("请输入加密内容");
  31. }
  32. }
  33. /// <summary>
  34. /// 加密
  35. /// </summary>
  36. /// <param name="_input">在输入框中需要加密内容</param>
  37. /// <param name="_keyValue"></param>
  38. /// <returns></returns>
  39. private string ConductEncryption(string _input,string _keyValue)
  40. {
  41. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(_keyValue);
  42. //加密格式
  43. RijndaelManaged encryption = new RijndaelManaged();
  44. encryption.Key = keyArray;
  45. encryption.Mode = CipherMode.ECB;
  46. encryption.Padding = PaddingMode.PKCS7;
  47. //生成加密锁
  48. ICryptoTransform cTransform = encryption.CreateEncryptor();
  49. byte[] _EncryptArray = UTF8Encoding.UTF8.GetBytes(_input);
  50. byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
  51. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  52. }
  53. }

【解密】

  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System;
  5. using System.Text;
  6. using System.Security.Cryptography;
  7. public class Decrypt : MonoBehaviour
  8. {
  9. /// <summary>
  10. /// 获得需要解密的字符串
  11. /// </summary>
  12. public InputField valueDense;
  13. /// <summary>
  14. /// 32位任意数值,作为是加密解码约定数字
  15. /// </summary>
  16. private string keyValue = "01234567890123456789012345678901";
  17. public void OnBtnDecrypt()
  18. {
  19. if (valueDense.text.Length != 0)
  20. {
  21. string str = ConductDecrypt(valueDense.text,keyValue);
  22. valueDense.text = str;
  23. }
  24. else
  25. {
  26. Debug.Log("请输入需要解密的值");
  27. }
  28. }
  29. private string ConductDecrypt(string _valueDense, string _keyValue)
  30. {
  31. byte[] keyArray = UTF8Encoding.UTF8.GetBytes(_keyValue);
  32. RijndaelManaged decipher = new RijndaelManaged();
  33. decipher.Key = keyArray;
  34. decipher.Mode = CipherMode.ECB;
  35. decipher.Padding = PaddingMode.PKCS7;
  36. ICryptoTransform cTransform = decipher.CreateDecryptor();
  37. byte[] _EncryptArray = Convert.FromBase64String(_valueDense);
  38. byte[] resultArray = cTransform.TransformFinalBlock(_EncryptArray, 0, _EncryptArray.Length);
  39. return UTF8Encoding.UTF8.GetString(resultArray);
  40. }
  41. }

【检测】

在第一个输入框上输入需要加密的内容,当点击加密按钮后,加密后的数据便输出在第二个输入框中

再点击解密按钮后,便有解密后的数据输出



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/118487
推荐阅读
相关标签
  

闽ICP备14008679号