美文网首页知识的搬运者
Unity项目之AES加密

Unity项目之AES加密

作者: 会奔跑的蘑菇 | 来源:发表于2020-04-07 19:38 被阅读0次
using System.Text;

using System.Security.Cryptography;

using System;

public class EncryptDecipherTool

{

    //加密和解密采用相同的key,可以任意数字,但是必须为32位

    private static string key = "12345678123456781234567812345678";

    public static string Encrypt(string content)

    {

        return Encrypt(content, key);

    }

    //AES加密

    public static string Encrypt(string content, string k)

    {

        byte[] keyBytes = UTF8Encoding.UTF8.GetBytes(k);

        RijndaelManaged rm = new RijndaelManaged();

        rm.Key = keyBytes;

        rm.Mode = CipherMode.ECB;

        rm.Padding = PaddingMode.PKCS7;

        ICryptoTransform ict = rm.CreateEncryptor();

        byte[] contentBytes = UTF8Encoding.UTF8.GetBytes(content);

        byte[] resultBytes = ict.TransformFinalBlock(contentBytes, 0, contentBytes.Length);

        return Convert.ToBase64String(resultBytes, 0, resultBytes.Length);

    }

    public static string Decipher(string content)

    {

        return Decipher(content, key);

    }

    //AES解密

    public static string Decipher(string content, string k)

    {

        byte[] keyBytes = UTF8Encoding.UTF8.GetBytes(k);

        RijndaelManaged rm = new RijndaelManaged();

        rm.Key = keyBytes;

        rm.Mode = CipherMode.ECB;

        rm.Padding = PaddingMode.PKCS7;

        ICryptoTransform ict = rm.CreateDecryptor();

        byte[] contentBytes = Convert.FromBase64String(content);

        byte[] resultBytes = ict.TransformFinalBlock(contentBytes, 0, contentBytes.Length);

        return UTF8Encoding.UTF8.GetString(resultBytes);

    }

}

相关文章

网友评论

    本文标题:Unity项目之AES加密

    本文链接:https://www.haomeiwen.com/subject/bykophtx.html