1. C# AES 加密解密算法
AESHelper.cs
using System;
using System.Text;
using UnityEngine;
namespace com.sdqy.tool
{
public class AESHelper : MonoBehaviour
{
/// <summary>
/// 注意 key 需要为 16 位
/// </summary>
public string privateKey = "sdqy2018love2018";
public string inputString = "sdqy20180323";
void Start()
{
Debug.Log("密钥 " + privateKey);
Debug.Log("加密前内容 " + inputString);
string encryptString = AesEncrypt(inputString, privateKey);
Debug.Log("加密后内容 " + encryptString);
string decryptString = AesDecrypt(encryptString, privateKey);
Debug.Log("解密后内容 " + decryptString);
}
/// <summary>
/// AES 加密
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesEncrypt(string str, string key)
{
if (string.IsNullOrEmpty(str)) return null;
try
{
Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
System.Security.Cryptography.RijndaelManaged rm = new
System.Security.Cryptography.RijndaelManaged
{
Key = Encoding.UTF8.GetBytes(key),