des和RSA两种加密方式的例子

本文提供了RSA非对称加密及解密的具体实现代码,并展示了如何使用DES进行数据加密与解密的过程。通过这些示例代码,读者可以了解加密算法的工作原理及其在实际应用中的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 static public string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
    {
        str_Public_Key = "";
        str_Private_Key = "";
        UnicodeEncoding ByteConverter = new UnicodeEncoding();
        byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
        try
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));

            //OAEP padding is only available on Microsoft Windows XP or later. 
            byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
            str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
            str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
            string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
            return str_Cypher_Text;
        }
        catch (CryptographicException e)
        {

            return null;
        }
    }
    //RSA解密
    static public string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
    {
        byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
        try
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
            //RSA.ImportParameters(RSAKeyInfo);
            byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
            RSA.ImportCspBlob(bytes_Public_Key);

            //OAEP padding is only available on Microsoft Windows XP or later. 
            byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
            UnicodeEncoding ByteConverter = new UnicodeEncoding();
            string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
            return str_Plain_Text;
        }
        catch (CryptographicException e)
        {
            throw e;
            return null;
        }
    }

 

 

 

 

 

 

 

 

 

  //加密
    public static String Encrypt(String str)
    {
        String encryptKey = "Aast";
        DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
        byte[] key = Encoding.Unicode.GetBytes(encryptKey);
        byte[] data = Encoding.Unicode.GetBytes(str);
        System.IO.MemoryStream MStream = new System.IO.MemoryStream();
        CryptoStream CStream = new CryptoStream(MStream, descsp.CreateEncryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock();
        return Convert.ToBase64String(MStream.ToArray());
    }
    //解密
    public static String Decryption(String str)
    {
        String encryptKey = "Aast";
        DESCryptoServiceProvider descsp = new DESCryptoServiceProvider();
        byte[] key = Encoding.Unicode.GetBytes(encryptKey);
        byte[] data = Convert.FromBase64String(str);
        System.IO.MemoryStream MStream = new System.IO.MemoryStream();
        CryptoStream CStream = new CryptoStream(MStream, descsp.CreateDecryptor(key, key), CryptoStreamMode.Write);
        CStream.Write(data, 0, data.Length);
        CStream.FlushFinalBlock();
        return Encoding.Unicode.GetString(MStream.ToArray());
    }

 

 

### 关于 DES RSA 加密算法的源代码 对于加密技术中的数据加密标准(DES)以及公钥基础设施下的RSA加密算法,在多种编程语言中有不同的实现方式。 #### 数据加密标准(DES) 在Python中,可以利用`pycryptodome`库来实现DES加密: ```python from Crypto.Cipher import DES from Crypto.Random import get_random_bytes key = b'SecretK' # 密钥长度需为8字节 cipher = DES.new(key, DES.MODE_ECB) plaintext = b'This is a test' ciphertext = cipher.encrypt(plaintext + (8 - len(plaintext) % 8) * b'\0') # 补齐至8字节倍数 print(ciphertext.hex()) ``` 这段代码展示了如何创建一个基于给定密钥的新DES对象,并对明文消息进行填充后执行加密操作[^1]。 #### RSA加密算法 同样是在Python环境下,使用相同的库也可以轻松构建RSA加解密功能: ```python from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import binascii keyPair = RSA.generate(bits=1024) pubKey = keyPair.publickey() encryptor = PKCS1_OAEP.new(pubKey) encrypted = encryptor.encrypt(b'Message for encryption') print("Encrypted:", binascii.hexlify(encrypted)) ``` 此部分说明了生成一对新的RSA密钥对的过程,接着采用PKCS#1 OAEP作为填充方案完成了一次完整的加密流程。 值得注意的是,上述例子仅用于学习目的;实际应用时应考虑更安全的做法,比如选择更强力的哈希函数或增加随机盐值等措施。此外,还有许多其他资源提供了关于这两种经典密码学方法的具体描述更多样化的编码实例[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值