RSA系列第3篇:Unity3d 使用 RSA 和 DES 加密网络数据包

Unity3d 使用 RSA 和 DES 加密网络数据包

Unity_RSA_DES :Unity3d 使用 RSA 和 DES 加密网络数据包

在网络通讯中,如果数据不进行加密,那么这些数据都是透明的 。
就相当于你去寄信,但是这封信居然没有用信封装起来,这样邮局的任何一个人都可以拿过来看信的内容,毫无安全性可言。
电脑中发送出去的每个数据包,都要在交换机 路由器上通过。
如果你发送一个登陆网站的请求,而这个请求没有加密,那么在路由器上能明明白白的看到你的帐号密码。
最简单的测试方式:在电脑上安装 WireShark,然后让手机连上电脑发出去的 Wifi,在 WireShark 中能看到手机发送的所有数据。

现有的加密分为 对称加密和非堆成加密。
对称加密:客户端和服务端 使用相同的密钥,加密速度很快,比如 DES 。
非对称加密:客户端和服务端使用 不相同的密钥,加密速度非常慢。比如 RSA

一般项目中的加密流程如下

C# 库 中为我们提供了 RSA 和 DES 加密的 API,下面分别来看。
using System.IO;
using System.Security.Cryptography;

public class UEncrypt
{
    /// <summary>
    /// 生成RSA私钥 公钥
    /// </summary>
    /// <param name="privateKey"></param>
    /// <param name="publicKey"></param>
    public static void RSAGenerateKey(ref string privateKey, ref string publicKey)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        privateKey = rsa.ToXmlString(true);
        publicKey = rsa.ToXmlString(false);
    }
    
    /// <summary>
    /// 用RSA公钥 加密
    /// </summary>
    /// <param name="data"></param>
    /// <param name="publicKey"></param>
    /// <returns></returns>
    public static byte[] RSAEncrypt(byte[] data, string publicKey)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(publicKey);
        byte[] encryptData = rsa.Encrypt(data, false);
        return encryptData;
    }
    
    /// <summary>
    /// 用RSA私钥 解密
    /// </summary>
    /// <param name="data"></param>
    /// <param name="privateKey"></param>
    /// <returns></returns>
    public static byte[] RSADecrypt(byte[] data, string privateKey)
    {
        RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
        rsa.FromXmlString(privateKey);
        byte[] decryptData = rsa.Decrypt(data, false);
        return decryptData;
    }
    
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="data">源数据</param>
    /// <param name="desrgbKey"></param>
    /// <param name="desrgbIV"></param>
    /// <returns></returns>
    public static byte[] DESEncrypt(byte[] data, byte[] desrgbKey, byte[] desrgbIV)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, 
            des.CreateEncryptor(desrgbKey, desrgbIV), CryptoStreamMode.Write);
        cs.Write(data, 0, data.Length);
        cs.FlushFinalBlock();
        return ms.ToArray();
    }

    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="data">源数据</param>
    /// <param name="desrgbKey"></param>
    /// <param name="desrgbIV"></param>
    /// <returns></returns>
    public static byte[] DESDecrypt(byte[] data, byte[] desrgbKey, byte[] desrgbIV)
    {
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms,
            des.CreateDecryptor(desrgbKey, desrgbIV), CryptoStreamMode.Write);
        cs.Write(data, 0, data.Length);
        cs.FlushFinalBlock();
        return ms.ToArray();
    }
}
using System.Text;
using UnityEngine;

public class Test : MonoBehaviour
{
    public string message = "Hello World";
    private string privateKey = string.Empty;
    private string publicKey = string.Empty;
    
    void Start()
    {
        Debug.Log("src:" + message);
        byte[] data = Encoding.Default.GetBytes(message);   //网络数据包
        
        //1、客户端 -- 生成RSA公钥 私钥,并把公钥发给服务器
        UEncrypt.RSAGenerateKey(ref privateKey, ref publicKey);
        
        //2、服务端 -- 生成DES密钥
        byte[] serverDesKey = new byte[] {1, 2, 3, 4, 8, 7, 6, 5};
        
        //3、服务端 -- 用RSA公钥加密 DES密钥,并发给客户端
        byte[] serverDesKeyEncrypt = UEncrypt.RSAEncrypt(serverDesKey, publicKey);
        
        //4、客户端 -- 用RSA私钥解密 DES密钥
        byte[] clientRsaDecryptDesKey = UEncrypt.RSADecrypt(serverDesKeyEncrypt, privateKey);
        
        //5、客户端 -- 用 DES密钥加密网络包
        byte[] clientDesEncryptData = UEncrypt.DESEncrypt(data, clientRsaDecryptDesKey, clientRsaDecryptDesKey);
        
        //6、服务端 -- 用 DES密钥解密网络包
        byte[] serverDesDecryptData = UEncrypt.DESDecrypt(clientDesEncryptData, serverDesKey, serverDesKey);
        
        message = Encoding.Default.GetString(serverDesDecryptData);
        Debug.Log("des:" + message);
    }
}
源码分享

getker/Unity_RSA_DES: Unity_RSA_DES-Unity使用RSA和DES加密网络数据包 (github.com)

image-20220811202401117

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kerven_HKW

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值