[Unity优化]数据的加密与解密

原文链接:http://www.manew.com/forum.php?mod=viewthread&tid=21682&_dsign=f0374ae5


using UnityEngine;
using System.Collections;
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);
    }

    //加密
    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);
    }

    //解密
    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);
    }

}

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {

    public InputField inputField;

    public void Encrypt()
    {
        inputField.text = EncryptDecipherTool.Encrypt(inputField.text);
    }

    public void Decipher()
    {
        inputField.text = EncryptDecipherTool.Decipher(inputField.text);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值