关于Unity文件加密和字符串加密AES

https://blog.csdn.net/qq_40558087/article/details/120146288
这是一篇关于Unity利用AES给文件加密和解密的文章,因为上一年其实我也接触过AES加解密,偶然的机会重新利用这个机会来搞一下,以上的链接我觉得对于很多人来说都很容易看的懂,所以就推荐了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是UnityAES加密和解密的示例代码: 加密代码: ```csharp using System.Security.Cryptography; using UnityEngine; public static class AESEncryption { private static readonly byte[] Key = new byte[16] { 215, 254, 100, 91, 10, 69, 23, 146, 12, 175, 237, 65, 38, 205, 85, 111 }; private static readonly byte[] IV = new byte[16] { 183, 221, 44, 251, 126, 7, 77, 234, 154, 150, 97, 24, 165, 2, 153, 201 }; public static string Encrypt(string plainText) { byte[] encrypted; using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = IV; ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); using (System.IO.MemoryStream msEncrypt = new System.IO.MemoryStream()) { using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) { using (System.IO.StreamWriter swEncrypt = new System.IO.StreamWriter(csEncrypt)) { swEncrypt.Write(plainText); } encrypted = msEncrypt.ToArray(); } } } return System.Convert.ToBase64String(encrypted); } } ``` 解密代码: ```csharp using System.Security.Cryptography; using UnityEngine; public static class AESEncryption { private static readonly byte[] Key = new byte[16] { 215, 254, 100, 91, 10, 69, 23, 146, 12, 175, 237, 65, 38, 205, 85, 111 }; private static readonly byte[] IV = new byte[16] { 183, 221, 44, 251, 126, 7, 77, 234, 154, 150, 97, 24, 165, 2, 153, 201 }; public static string Decrypt(string cipherText) { byte[] cipherBytes = System.Convert.FromBase64String(cipherText); using (Aes aesAlg = Aes.Create()) { aesAlg.Key = Key; aesAlg.IV = IV; ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); using (System.IO.MemoryStream msDecrypt = new System.IO.MemoryStream(cipherBytes)) { using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { using (System.IO.StreamReader srDecrypt = new System.IO.StreamReader(csDecrypt)) { return srDecrypt.ReadToEnd(); } } } } } } ``` 注意:这个示例代码中的密钥和向量都是为了演示而写死的,请根据实际需求使用更安全的方式生成密钥和向量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值