Coder Social home page Coder Social logo

xiangyuecn / rsa-csharp Goto Github PK

View Code? Open in Web Editor NEW
226.0 10.0 93.0 235 KB

c# dotnet .NET Framework/Standard/Core RSA Provides: Encrypt,Decrypt,Signature,Verify; RSA key format: PEM (PKCS#1 PKCS#8), XML, Public Private Key, Import Export Convert; Padding support: NoPadding,PKCS1,OAEP,PSS, & MD5,SHA1,SHA256,SHA512,SHA3-256,SHA3-512 etc. Code with zero dependencies. RSA公钥私钥导入导出转换、加密解密、签名验证,多种填充方式支持。

License: MIT License

C# 83.22% Batchfile 11.14% Shell 5.64%
rsa-pem rsa-xml pem-xml pkcs1 pkcs8 sha1withrsa sha256withrsa oaeppadding oaepwithsha-1andmgf1padding oaepwithsha-256andmgf1padding

rsa-csharp's People

Contributors

xiangyuecn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rsa-csharp's Issues

rsa工具

方便提供一下最后的RSA工具的下载地址吗?谢谢

C#.net 6.0的警告

2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_Modulus”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_Exponent”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_D”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_P”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_Q”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DP”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(51,11,51,18): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_InverseQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(60,18,60,31): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(61,19,61,33): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(64,13,64,20): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(66,13,66,20): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(67,13,67,20): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(68,14,68,22): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(69,14,69,22): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(70,20,70,34): warning CS8601: 可能的 null 引用赋值。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_Modulus”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_Exponent”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_D”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_P”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_Q”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DP”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(56,10,56,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_InverseQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(67,12,67,16): warning CS8603: 可能返回 null 引用。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(69,18,69,22): warning CS8600: 将 null 文本或可能的 null 值转换为不可为 null 类型。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(72,12,72,16): warning CS8603: 可能返回 null 引用。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(76,12,76,16): warning CS8603: 可能返回 null 引用。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Key_D”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_P”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_Q”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DP”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_DQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(97,10,97,17): warning CS8618: 在退出构造函数时,不可为 null 的 字段“Val_InverseQ”必须包含非 null 值。请考虑将 字段 声明为可以为 null。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(108,12,108,16): warning CS8603: 可能返回 null 引用。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(127,18,127,22): warning CS8600: 将 null 文本或可能的 null 值转换为不可为 null 类型。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(254,18,254,22): warning CS8600: 将 null 文本或可能的 null 值转换为不可为 null 类型。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(189,10,189,49): warning CA1416: 可在所有平台上访问此调用站点。"RSACryptoServiceProvider" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(187,20,187,39): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(188,4,188,19): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters.Flags" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(180,4,180,19): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters.Flags" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(179,20,179,39): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA.cs(181,10,181,58): warning CA1416: 可在所有平台上访问此调用站点。"RSACryptoServiceProvider" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(148,20,148,39): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(149,4,149,19): warning CA1416: 可在所有平台上访问此调用站点。"CspParameters.Flags" 在 'windows' 上受支持。
2>D:\Projects\Trade\Net60\Infrastructure\Cryptography\RSA_PEM.cs(150,14,150,53): warning CA1416: 可在所有平台上访问此调用站点。"RSACryptoServiceProvider" 在 'windows' 上受支持。

RSACryptoServiceProvider应该是最大的问题,.net core上不兼容。

为什么会报“PEM未能提取到数据”?

在试图读取以下文件内容时:

-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgiMUviC3wiXZITKuC
dZ3EOjolfC2Buep2zac1orHq/JmhRANCAAQcSjQ/I/Ngs9d/ARFwDH/PJM4bCCvc
3A6T2/8Cuz2mBQ96otBiwCJcbnV/bQ/B2LcKOhLe4g042x/OAeTLpW4W
-----END PRIVATE KEY-----

Operation is not supported on this platform, .Net Core

Operation is not supported on this platform.

          var provider = RSA_PEM.FromPEM(publicKey);
            var xml = provider.ToXmlString(false);
   at System.Security.Cryptography.RSA.ToXmlString(Boolean includePrivateParameters)
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\xxx\Documents\Code\ConsoleApp1\Program.cs:line 18

ToXmlString, seem to not support on .Net Core Plateform anymore.
We should implement the methods, FromXmlString and ToXmlString , I dont know why MS is not support the two methods. :={

/// <summary>Initializes an <see cref="T:System.Security.Cryptography.RSA" /> object from the key information from an XML string.</summary>
   /// <param name="xmlString">The XML string containing <see cref="T:System.Security.Cryptography.RSA" /> key information. </param>
   /// <exception cref="T:System.ArgumentNullException">The <paramref name="xmlString" /> parameter is null. </exception>
   /// <exception cref="T:System.Security.Cryptography.CryptographicException">The format of the <paramref name="xmlString" /> parameter is not valid. </exception>
   public override void FromXmlString(string xmlString)
   {
     if (xmlString == null)
       throw new ArgumentNullException(nameof (xmlString));
     RSAParameters parameters = new RSAParameters();
     SecurityElement topElement = new Parser(xmlString).GetTopElement();
     string inputBuffer1 = topElement.SearchForTextOfLocalName("Modulus");
     if (inputBuffer1 == null)
       throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object) nameof (RSA), (object) "Modulus"));
     parameters.Modulus = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer1));
     string inputBuffer2 = topElement.SearchForTextOfLocalName("Exponent");
     if (inputBuffer2 == null)
       throw new CryptographicException(Environment.GetResourceString("Cryptography_InvalidFromXmlString", (object) nameof (RSA), (object) "Exponent"));
     parameters.Exponent = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer2));
     string inputBuffer3 = topElement.SearchForTextOfLocalName("P");
     if (inputBuffer3 != null)
       parameters.P = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer3));
     string inputBuffer4 = topElement.SearchForTextOfLocalName("Q");
     if (inputBuffer4 != null)
       parameters.Q = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer4));
     string inputBuffer5 = topElement.SearchForTextOfLocalName("DP");
     if (inputBuffer5 != null)
       parameters.DP = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer5));
     string inputBuffer6 = topElement.SearchForTextOfLocalName("DQ");
     if (inputBuffer6 != null)
       parameters.DQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer6));
     string inputBuffer7 = topElement.SearchForTextOfLocalName("InverseQ");
     if (inputBuffer7 != null)
       parameters.InverseQ = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer7));
     string inputBuffer8 = topElement.SearchForTextOfLocalName("D");
     if (inputBuffer8 != null)
       parameters.D = Convert.FromBase64String(Utils.DiscardWhiteSpaces(inputBuffer8));
     this.ImportParameters(parameters);
   }

   /// <summary>Creates and returns an XML string containing the key of the current <see cref="T:System.Security.Cryptography.RSA" /> object.</summary>
   /// <returns>An XML string containing the key of the current <see cref="T:System.Security.Cryptography.RSA" /> object.</returns>
   /// <param name="includePrivateParameters">true to include a public and private RSA key; false to include only the public key. </param>
   public override string ToXmlString(bool includePrivateParameters)
   {
     RSAParameters rsaParameters = this.ExportParameters(includePrivateParameters);
     StringBuilder stringBuilder = new StringBuilder();
     stringBuilder.Append("<RSAKeyValue>");
     stringBuilder.Append("<Modulus>" + Convert.ToBase64String(rsaParameters.Modulus) + "</Modulus>");
     stringBuilder.Append("<Exponent>" + Convert.ToBase64String(rsaParameters.Exponent) + "</Exponent>");
     if (includePrivateParameters)
     {
       stringBuilder.Append("<P>" + Convert.ToBase64String(rsaParameters.P) + "</P>");
       stringBuilder.Append("<Q>" + Convert.ToBase64String(rsaParameters.Q) + "</Q>");
       stringBuilder.Append("<DP>" + Convert.ToBase64String(rsaParameters.DP) + "</DP>");
       stringBuilder.Append("<DQ>" + Convert.ToBase64String(rsaParameters.DQ) + "</DQ>");
       stringBuilder.Append("<InverseQ>" + Convert.ToBase64String(rsaParameters.InverseQ) + "</InverseQ>");
       stringBuilder.Append("<D>" + Convert.ToBase64String(rsaParameters.D) + "</D>");
     }
     stringBuilder.Append("</RSAKeyValue>");
     return stringBuilder.ToString();
   }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.