zoukankan      html  css  js  c++  java
  • .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)); 

  • 相关阅读:
    ffmpeg full help
    docker 服务命令
    php 查看swoole版本
    vue/cli 的启动
    TP框架的使用,不需要阿帕奇
    mysql 的文件恢复
    mac下使用iterm实现自动登陆
    跨库怎样查询
    swoole和websocket的关系
    mac上mysql的安装和使用
  • 原文地址:https://www.cnblogs.com/gjhjoy/p/3501666.html
Copyright © 2011-2022 走看看