Skip to main content
 首页 » 编程设计

c#之RSA客户端服务器加密解密

2024年02月13日11fff_TT

这是RSA加密/解密的简单代码。

如何让它适用于不同的应用程序?在下面的示例中,相同的 RSA 对象用于加密和解密。我怎样才能让它适用于 2 个独立的应用程序。这样在一个应用程序中加密的数据可以被另一个应用程序解密。

using System; 
using System.Security.Cryptography; 
using System.Text; 
 
class RSACSPSample 
{ 
 
    static void Main() 
    { 
        try 
        { 
            //Create a UnicodeEncoder to convert between byte array and string. 
            UnicodeEncoding ByteConverter = new UnicodeEncoding(); 
 
            //Create byte arrays to hold original, encrypted, and decrypted data.  
            byte[] dataToEncrypt = ByteConverter.GetBytes("Data to Encrypt"); 
            byte[] encryptedData; 
            byte[] decryptedData; 
 
            //Create a new instance of RSACryptoServiceProvider to generate  
            //public and private key data.  
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) 
            { 
 
                //Pass the data to ENCRYPT, the public key information   
                //(using RSACryptoServiceProvider.ExportParameters(false),  
                //and a boolean flag specifying no OAEP padding. 
                encryptedData = RSAEncrypt(dataToEncrypt, RSA.ExportParameters(false), false); 
 
                //Pass the data to DECRYPT, the private key information   
                //(using RSACryptoServiceProvider.ExportParameters(true),  
                //and a boolean flag specifying no OAEP padding. 
                decryptedData = RSADecrypt(encryptedData, RSA.ExportParameters(true), false); 
 
                //Display the decrypted plaintext to the console.  
                Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData)); 
            } 
        } 
        catch (ArgumentNullException) 
        { 
            //Catch this exception in case the encryption did  
            //not succeed. 
            Console.WriteLine("Encryption failed."); 
 
        } 
    } 
 
    static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) 
    { 
        try 
        { 
            byte[] encryptedData; 
            //Create a new instance of RSACryptoServiceProvider.  
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) 
            { 
 
                //Import the RSA Key information. This only needs  
                //toinclude the public key information. 
                RSA.ImportParameters(RSAKeyInfo); 
 
                //Encrypt the passed byte array and specify OAEP padding.    
                //OAEP padding is only available on Microsoft Windows XP or  
                //later.   
                encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding); 
            } 
            return encryptedData; 
        } 
        //Catch and display a CryptographicException    
        //to the console.  
        catch (CryptographicException e) 
        { 
            Console.WriteLine(e.Message); 
 
            return null; 
        } 
 
    } 
 
    static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding) 
    { 
        try 
        { 
            byte[] decryptedData; 
            //Create a new instance of RSACryptoServiceProvider.  
            using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) 
            { 
                //Import the RSA Key information. This needs  
                //to include the private key information. 
                RSA.ImportParameters(RSAKeyInfo); 
 
                //Decrypt the passed byte array and specify OAEP padding.    
                //OAEP padding is only available on Microsoft Windows XP or  
                //later.   
                decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding); 
            } 
            return decryptedData; 
        } 
        //Catch and display a CryptographicException    
        //to the console.  
        catch (CryptographicException e) 
        { 
            Console.WriteLine(e.ToString()); 
 
            return null; 
        } 
 
    } 
} 

请您参考如下方法:

当你打电话时

//Create a new instance of RSACryptoServiceProvider to generate  
//public and private key data.  
using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) 
.... 

新的 RSA key 对是按照您在代码上方的注释中所述生成的。您需要共享此 key 对。你可以做

RSA.ExportParameters(true) 

或

RSA.ToXmlString(true) 

然后您可以将此输出包含在两个应用程序的配置中,并且在执行开始时您必须调用 RSA.ImportParameters 或 RSA.FromXmlString。

请注意,上面的代码提取了 key 对的私有(private)部分。如果您只想在应用程序 A 中加密,而只在应用程序 B 中解密,那么应用程序 A 只需要 key 对的公共(public)部分,而应用程序 B 需要私有(private)部分。

另请注意,不建议使用非对称密码术对大数据进行加密(出于性能原因),您应该考虑使用对称密码术(即 AES256)。