说明:
RSA加密算法是最常用的非对称加密算法,RSA是第一个比较完善的公开密钥算法,它既能用于加密,也能用于数字签名。RSA以它的三个发明者Ron Rivest, Adi Shamir, Leonard Adleman的名字首字母命名,其他的介绍就自行百度了,这里主要将一下如何在Unity中使用RSA对数据进行加密、解密、以及签名、验证签名等。
RSA加解密通用理解
1:甲方通过系统生成一对公钥及私钥(*****每次生成的均不同*****),然后将私钥告诉其他方;
2:甲方通过公钥对数据进行加密,并将加密后的数据告诉给其他方;
3:其他方在已知私钥+加密后的数据情况下,使用私月即可解密得到明文;
RSA验证通用理解
1:甲方与其他方约定某一公共知道的数据A;
2:在数据传输时,首先使用私钥对数据A进行加密,然后得到签名数据;
3:其他方得到签名数据后,在已知私钥+数据A加密数据+签名数据情况下,经行签名验证,即可验证是否签名正确;
详细代码如下
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System;
public class RSAPro : MonoBehaviour
{
string publickey;
string privatekey;
string RSAed;
public string mingwen;
RSACryptoServiceProvider rsa;
void Start()
{
rsa = new RSACryptoServiceProvider();
publickey = rsa.ToXmlString(false);
privatekey = rsa.ToXmlString(true);
Debug.Log("公钥:" + publickey);
Debug.Log("私钥:" + privatekey);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RSAed = RSAEncrypt(publickey, mingwen);
Debug.Log("加密后:" + RSAed);
Debug.Log("解密后:" + RSADecrypt(privatekey, RSAed));
UnicodeEncoding ByteConverter = new UnicodeEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes("ABC");
//RAS数字签名
RSAParameters Key = rsa.ExportParameters(true);
byte[] signedData = HashAndSignBytes(dataToEncrypt, Key);
if (VerifySignedHash(dataToEncrypt, signedData, Key))
{
Debug.Log("数据验证通过");
}
else
{
Debug.Log("没有通过");
}
}
}
/// <summary>
/// 加密
/// </summary>
/// <param name="publickey">公钥</param>
/// <param name="content">所加密的内容</param>
/// <returns>加密后的内容</returns>
string RSAEncrypt(string publickey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(publickey);
cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);
return Convert.ToBase64String(cipherbytes); ;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="privatekey">私钥</param>
/// <param name="content">加密后的内容</param>
/// <returns>解密后的内容</returns>
string RSADecrypt(string privatekey, string content)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
byte[] cipherbytes;
rsa.FromXmlString(privatekey);
cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);
return Encoding.UTF8.GetString(cipherbytes);
}
/// <summary>
/// 签名
/// </summary>
/// <param name="DataToSign"></param>
/// <param name="Key"></param>
/// <returns></returns>
static byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(Key);
return RSA.SignData(DataToSign, new SHA1CryptoServiceProvider());
}
catch
{
return null;
}
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="DataToVerify"></param>
/// <param name="SignedData"></param>
/// <param name="Key"></param>
/// <returns></returns>
static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key)
{
try
{
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.ImportParameters(Key);
return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);
}
catch
{
return false;
}
}
}
测试结果如下