对称加密DES和非对称加密RSA的实现

被刻印的时光 ゝ 提交于 2020-03-28 02:24:03
        Shared strKey As String = "12345678"
        Shared strIV As String = "12345678"

        Private Shared Key() As Byte = Encoding.UTF8.GetBytes(strKey.Substring(0, 8))
        Private Shared IV() As Byte = Encoding.UTF8.GetBytes(strIV.Substring(0, 8))
        ‘Key = New Byte() {11, 12, 13, 14, 15, 16, 17, 18}
        ‘IV = New Byte() {11, 12, 13, 14, 15, 16, 17, 18}

       Public Shared Function Encrypt(ByVal strText) As String
            Try
                Dim inputByteArray() As Byte = Encoding.UTF8.GetBytes(strText)
                Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
                Dim ms As New MemoryStream
                Dim cs As New CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
                cs.Write(inputByteArray, 0, inputByteArray.Length)
                cs.FlushFinalBlock()
                Return Convert.ToBase64String(ms.ToArray())
            Catch ex As Exception
                Return ex.Message
            End Try
        End Function

        Public Shared Function Decrypt(ByVal strText) As String
            Dim inputByteArray() As Byte
            'inputByteArray = New Byte(strText.Length)

            Try
                inputByteArray = Convert.FromBase64String(strText)
                Dim des As New DESCryptoServiceProvider
                Dim ms As New MemoryStream
                Dim cs As New CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write)
                cs.Write(inputByteArray, 0, inputByteArray.Length)
                cs.FlushFinalBlock()
                Dim encoding As encoding = encoding.UTF8
                Return encoding.GetString(ms.ToArray())
            Catch ex As Exception
                Return ex.Message
            End Try
        End Function

C#实现DES
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Configuration;

public static class EncryptHelper
{
    
static String strKey = System.Configuration.ConfigurationManager.AppSettings["EncryptKey"];
    
static String strIV = strKey;

    
private static Byte[] Key = Encoding.UTF8.GetBytes(strKey.Substring(08));
    
private static Byte[] IV = Encoding.UTF8.GetBytes(strIV.Substring(08));

    
public static string Encrypt(string strText)
    {
        
try
        {
            Byte[] inputByteArray 
= Encoding.UTF8.GetBytes(strText);
            DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
            System.IO.MemoryStream ms 
= new MemoryStream();
            CryptoStream cs 
= new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 
0, inputByteArray.Length);
            cs.FlushFinalBlock();
            
return Convert.ToBase64String(ms.ToArray());

        }
        
catch (Exception ex)
        {

            
return ex.Message;
        }
    }

    
public static string Decrypt(string strText)
    {

        Byte[] inputByteArray;

        
try
        {
            inputByteArray 
= Convert.FromBase64String(strText);

            DESCryptoServiceProvider des 
= new DESCryptoServiceProvider();
            System.IO.MemoryStream ms 
= new MemoryStream();
            CryptoStream cs 
= new CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);

            cs.Write(inputByteArray, 
0, inputByteArray.Length);
            cs.FlushFinalBlock();
            
return Encoding.UTF8.GetString(ms.ToArray());
        }
        
catch (Exception ex)
        {

            
return ex.Message;
        }
    }

}



非对称RSA
using System;
using System.Security.Cryptography;
using System.Text;
 
class RSACSPSample
{
     
public void MainTest()
     {
         
try
         {
              
//为了实现字节数组到字符窜的转换创建一个UnicodeEncoder
              UnicodeEncoding ByteConverter = new UnicodeEncoding();
 
              
//创建一个字节数组保留原始的,加密的以及解密的数据
              
//byte[] dataToEncrypt = ByteConverter.GetBytes("需要加密的数据");
 
              
byte[] dataToEncrypt = ByteConverter.GetBytes("需要加密的数据");
              
byte[] encryptedData;
              
byte[] decryptedData;
 
              
//创建一个 RSACryptoServiceProvider 的实例来产生公共和私有密钥数据
              RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
 
              
//通过这个数据去加密,公共密钥信息
              
//使用RSACryptoServiceProvider.ExportParameters(false),
              
//和一个 boolean 变量标明是否用 OAEP 填充.
              encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false),  false);
 
              Console.WriteLine(
"encrypted plaintext: {0}", Convert.ToBase64String(encryptedData));
 
              
//通过这个数据去解密, 私有密钥使用
              
//RSACryptoServiceProvider.ExportParameters(true),
              
//和 一个 boolean变量标明是否用 OAEP 填充.
              decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);
 
              
//显示解密的信息到控制台. 
              Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
              Console.Read(); 
         }
         
catch(ArgumentNullException)
         {
              Console.WriteLine(
"Encryption failed.");
         }
     }
 
     
public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
     {
         
try
         {
              RSACryptoServiceProvider RSA 
= new RSACryptoServiceProvider();
 
              
//输入 RSA Key 信息. 包括 public key 信息.
              RSA.ImportParameters(RSAKeyInfo);
 
              
//加密这个字节数组和是否用 OAEP 来填充
              
//OAEP 填充仅仅在Microsoft Windows XP 或者
              
//以后的版本中有用
              return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
         }
         
catch(CryptographicException e)
         {
              Console.WriteLine(e.Message);
              
return null;
         }
     }
 
     
public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
     {
         
try
         {
              RSACryptoServiceProvider RSA 
= new RSACryptoServiceProvider();
 
              
//输入 RSA Key 信息. 这个需要包括
              
// private key 信息
              RSA.ImportParameters(RSAKeyInfo);
 
              
//解密这个字节数组和是否用 OAEP 填充.  
              return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
         }
         
catch(CryptographicException e)
         {
              Console.WriteLine(e.ToString());
              
return null;
         }
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!