许可优化
产品
解决方案
服务支持
关于
软件库
当前位置:服务支持 >  软件文章 >  RSA系列第3篇:Unity3d 使用 RSA 和 DES 加密网络数据包

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

阅读数 4
点赞 0
article_banner

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();
    }
}
c#
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
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);
    }
}
c#
运行
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
源码分享

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

image-20220811202401117


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删
相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空