zoukankan      html  css  js  c++  java
  • C#数据Encrypt加密Encrypt解密的算法使用--非对称算法RSACryptoServiceProvider

    C#数据加密解密的非对称算法使用---RSACryptoServiceProvider   Asymmetric algorithms--Encrypt Encrypt  

    C#数据Encrypt加密Encrypt解密的相关算法可以参考System.Security.Cryptography,这个类库中包含MD5,SHA1,SHA256,SHA384,SHA512

    MD5 and SHA256 are two of the HashAlgorithm subtypes provided by the .NET Framework. Here are all the major algorithms, in ascending order of security (and hash length, in bytes):
    MD5(16) → SHA1(20) → SHA256(32) → SHA384(48) → SHA512(64)
    The shorter the algorithm, the faster it executes. MD5 is more than 20 times faster than SHA512 and is well suited to calculating file checksums. You can hash hundreds
    of megabytes per second with MD5 , and then store its result in a Guid . (A Guid happens to be exactly 16 bytes long, and as a value type it is more tractable than a byte array;
    you can meaningfully compare Guid s with the simple equality operator, for instance.)
    However, shorter hashes increase the possibility of collision (two distinct files yielding the same hash).
    Use at least SHA256 when hashing passwords or other securitysensitive data. MD5 and SHA1 are considered insecure for this
    purpose, and are suitable to protect only against accidental corruption, not deliberate tampering.
    SHA384 is no faster than SHA512 , so if you want more security than SHA256 , you may as well use SHA512

    引用using System.Security.Cryptography;

    代码如下:

     static void Main(string[] args)
            {
                log4net.Config.XmlConfigurator.Configure();
                Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    
                TestEncryptgrapg();
    
                Console.ReadLine();
            }
    
            public static void TestEncryptgrapg()
            {
                using var rsa = new RSACryptoServiceProvider();
                File.WriteAllText("PublicKeyOnly.xml", rsa.ToXmlString(false));
                File.WriteAllText("PublicPrivate.xml", rsa.ToXmlString(true));
    
                byte[] data = Encoding.UTF8.GetBytes("Message to encrypt");
                string publicKeyOnly = File.ReadAllText("PublicKeyOnly.xml");
                string publicPrivate = File.ReadAllText("PublicPrivate.xml");
                byte[] encrypted, decrypted;
                using (var rsaPublicOnly = new RSACryptoServiceProvider())
                {
                    rsaPublicOnly.FromXmlString(publicKeyOnly);
                    encrypted = rsaPublicOnly.Encrypt(data, true);
                    // 下面的这解密就会报错,因为需要私钥解密
                    // decrypted = rsaPublicOnly.Decrypt (encrypted, true);
                }
                using (var rsaPublicPrivate = new RSACryptoServiceProvider())
                {
                    // With the private key we can successfully decrypt:
                    rsaPublicPrivate.FromXmlString(publicPrivate);
                    decrypted = rsaPublicPrivate.Decrypt(encrypted, true);
                    string ss = Encoding.UTF8.GetString(decrypted);
                    WriteLog(ss);
                }
            }
  • 相关阅读:
    Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 's'
    抓报错SQL
    10.3.4 direct path read and direct path read temp
    武道与健身——北漂18年(80)
    jquery选择器 之 获取父级元素、同级元素、子元素
    JQuery Show()的几种效果 总有一种是你需要的
    Add method not implemented
    MySQL server version for the right syntax to use near 'info where info.stu_id = 1' at line 1
    Mapped Statements collection does not contain value for WaitMissionMapper.updateWait
    Cause: org.xml.sax.SAXParseException; lineNumber: 32; columnNumber: 14; 注释中不允许出现字符串 "--"
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/12171890.html
Copyright © 2011-2022 走看看