RSAKeyGenerator.cs 916 B

1234567891011121314151617181920212223242526272829
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MeterVision.Util
  8. {
  9. //我们需要生成一对 RSA 密钥(公钥和私钥)。你可以使用以下代码生成密钥对:
  10. public class RSAKeyGenerator
  11. {
  12. public static Tuple<string, string> GenerateKeys()
  13. {
  14. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048)) // 2048位密钥长度
  15. {
  16. // 生成私钥(包括公钥信息)
  17. string privateKey = rsa.ToXmlString(true); // true 表示包含私钥
  18. // 生成公钥
  19. string publicKey = rsa.ToXmlString(false); // false 表示只包含公钥
  20. return Tuple.Create(publicKey, privateKey);
  21. }
  22. }
  23. }
  24. //--------------------
  25. }