当前位置:编程学习 > 网站相关 >>

.NET下的加密解密大全(3):非对称加密

本博文列出了.NET下常用的非对称加密算法,并将它们制作成小DEMO,希望能对大家有所帮助。

RSA
[csharp]
static string EnRSA(string data,string publickey) 

    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
    byte[] cipherbytes;  
    rsa.FromXmlString(publickey);  
    cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(data), false);  
    return Convert.ToBase64String(cipherbytes); 
     

 
static string DeRSA(string data,string privatekey) 

    
    RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();  
    byte[] cipherbytes; rsa.FromXmlString(privatekey);  
    cipherbytes = rsa.Decrypt(Convert.FromBase64String(data), false);  
    return Encoding.UTF8.GetString(cipherbytes); 

调用代码
[csharp]
  Console.WriteLine("RSA非对称加密"); 
Byte[] iv = CreateKey(16); 
Byte[] key = CreateKey(27); 
string inputRSA_1 = inputString(); 
RSA rsa = RSA.Create(); 
 
string enData = EnRSA(inputRSA_1,rsa.ToXmlString(false)); 
 
 
Console.WriteLine("加密后的数据:{0}", enData); 
Console.WriteLine("解密后的数据:{0}", DeRSA(enData,rsa.ToXmlString(true))); 

 

DSA(数字签名)
[csharp]
 static string EnDSA(string data,string publickey) 
   { 
       DSA dsa = DSA.Create(); 
       Byte[] result; 
       dsa.FromXmlString(publickey); 
       SHA1 sha1 = SHA1.Create(); 
       result = dsa.CreateSignature(sha1.ComputeHash(Convert.FromBase64String(data))); 
       return Convert.ToBase64String(result); 
 
        
   } 
 
   static bool DeDSA(string data,string privatekey,string originalData) 
   { 
       //Byte[] result; 
       DSA dsa = DSA.Create(); 
       dsa.FromXmlString(privatekey); 
       SHA1 sha1 = SHA1.Create(); 
       return dsa.VerifySignature(sha1.ComputeHash(Convert.FromBase64String(originalData)),Convert.FromBase64String(data)); 
 
   } 
调用代码
[csharp]
  Console.WriteLine("DSA数字签名"); 
string inputDSA_1 = inputString(); 
string inputDSA_2 = inputDSA_1; 
DSA dsa = DSA.Create(); 
string enData = EnDSA(inputDSA_1, dsa.ToXmlString(true)); 
 
Console.WriteLine("加密后的数据:{0}", enData); 
Console.WriteLine("解密后的数据:{0}", DeDSA(enData, dsa.ToXmlString(false), inputDSA_2)); 

 

 

ECDsa
[csharp]
 
static string EnECDsa(string data, CngKey key) 
 { 
     ECDsaCng ecdsa = new ECDsaCng(key); 
      
      
     SHA1 sha1 = SHA1.Create(); 
     byte[] result; 
 
     result = ecdsa.SignHash(sha1.ComputeHash(Convert.FromBase64String(data))); 
 
     return Convert.ToBase64String(result); 
 
 } 
 
 static bool DeECDsa(string data, CngKey key,string originalData) 
 { 
     ECDsaCng ecdsa = new ECDsaCng(key); 
     SHA1 sha1 = SHA1.Create(); 
 
     return ecdsa.VerifyHash(sha1.ComputeHash(Convert.FromBase64String(originalData)), Convert.FromBase64String(data)); 
 } 

调用代码
[csharp]
  Console.WriteLine("ECDsa数字签名"); 
string inputDSA_1 = inputString(); 
string inputDSA_2 = inputDSA_1; 
CngKey key = CngKey.Create(CngAlgorithm.ECDsaP256); 
 
string enData = EnECDsa(inputDSA_1, key); 
 
Console.WriteLine("加密后的数据:{0}", enData); 
Console.WriteLine("解密后的数据:{0}", DeECDsa(enData, key, inputDSA_2)); 


摘自  GhostHouse
补充:综合编程 , 安全编程 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,