Exemple #1
0
        public string setRsa()      //初始化,生成PKCS1密钥对,将私钥保存在session中用于解密接收到的密码密文,将公钥传到前端用于密码加密
        {
            RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
            //添加参数
            RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(BigInteger.ValueOf(3), new SecureRandom(), 1024, 25);

            //初始化构造器
            keyGenerator.Init(param);
            //生成密钥对
            AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
            TextWriter textWriter           = new StringWriter();
            PemWriter  pemWriter            = new PemWriter(textWriter);

            pemWriter.WriteObject(keyPair.Private); //获取密钥对的私钥
            pemWriter.Writer.Flush();               //清空缓存区,防止脏写
            string priKey = textWriter.ToString();

            priKey             = RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(priKey);
            Session["Private"] = priKey;    //将私钥保存在session中
            TextWriter textWriter1 = new StringWriter();
            PemWriter  pemWriter1  = new PemWriter(textWriter1);

            pemWriter1.WriteObject(keyPair.Public); //获取密钥对的公钥
            pemWriter1.Writer.Flush();              //清空缓存区,防止脏写
            string pubKey = textWriter1.ToString();

            Session["pub"] = pubKey;    //将私钥保存在session中

            //将公钥存入cookie方便读取
            Response.Cookies["Key"].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies["Key"].Value   = pubKey;
            Response.Cookies["Key"].Expires = DateTime.Now.AddDays(1);
            return(pubKey);  //将公钥传到前端
        }
Exemple #2
0
 /// <summary>
 /// Pkcs1-> Pkcs8
 /// </summary>
 /// <param name="privateKeyXml">私钥</param>
 /// <returns></returns>
 public static string ConvertPrivateKeyPkcs1ToPkcs8(string privateKeyXml)
 {
     if (privateKeyXml is null)
     {
         return("null");
     }
     try
     {
         return(RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(privateKeyXml));
     }
     catch (Exception ex)
     {
         return(ex.Message);
     }
 }
Exemple #3
0
 //封装方法,实现委托
 public static void ConversionRSAKeyFunc(string privateKey, string publicKey, EnumConversionType enumConversionType)
 {
     if (enumConversionType == EnumConversionType.XML转化为Pkcs1)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyXmlToPkcs1(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyXmlToPem(privateKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.XML转化为Pkcs8)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyXmlToPkcs8(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyXmlToPem(privateKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs1转化为XML)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs1ToXml(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyPemToXml(publicKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs1转化为Pkcs8)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("No conversion required");
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else if (enumConversionType == EnumConversionType.Pkcs8转化为XML)
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs8ToXml(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine(publicKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PublicKeyPemToXml(publicKey));
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
     else
     {
         if (!string.IsNullOrWhiteSpace(privateKey))
         {
             Console.WriteLine(privateKey);
             Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
             Console.WriteLine(RsaKeyConvert.PrivateKeyPkcs8ToPkcs1(privateKey));
         }
         if (!string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("No conversion required");
         }
         if (string.IsNullOrWhiteSpace(privateKey) && string.IsNullOrWhiteSpace(publicKey))
         {
             Console.WriteLine("请输入正确的公私钥");
         }
     }
 }
Exemple #4
0
 /// <summary>
 /// Pkcs1-> Pkcs8
 /// </summary>
 public void Pkcs1ToPkcs8()
 {
     PrivateKey = RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(PrivateKey);
 }
Exemple #5
0
        private int OnExecute(CommandLineApplication app)
        {
            if (app.Options.Count == 1 && app.Options[0].ShortName == "h")
            {
                app.ShowHelp();
                return(0);
            }

            if (KeyType == "pub" && ((SourceFormat == "pkcs1" && TargetFormat == "pkcs8") ||
                                     (SourceFormat == "pkcs8" && TargetFormat == "pkcs1")))
            {
                app.Out.WriteLine("This public key does not need to be converted.");
                return(1);
            }

            if (SourceFormat == TargetFormat)
            {
                app.Out.WriteLine("Target format can not equal Source format.");
                return(1);
            }

            try
            {
                string result     = string.Empty;
                string keyContent = File.ReadAllText(KeyFilePath);

                if (KeyType == "pri")
                {
                    switch ($"{SourceFormat}->{TargetFormat}")
                    {
                    case "xml->pkcs1":
                        result = RsaKeyConvert.PrivateKeyXmlToPkcs1(keyContent);
                        break;

                    case "xml->pkcs8":
                        result = RsaKeyConvert.PrivateKeyXmlToPkcs8(keyContent);
                        break;

                    case "pkcs1->xml":
                        result = RsaKeyConvert.PrivateKeyPkcs1ToXml(keyContent);
                        break;

                    case "pkcs1->pkcs8":
                        result = RsaKeyConvert.PrivateKeyPkcs1ToPkcs8(keyContent);
                        break;

                    case "pkcs8->xml":
                        result = RsaKeyConvert.PrivateKeyPkcs8ToXml(keyContent);
                        break;

                    case "pkcs8->pkcs1":
                        result = RsaKeyConvert.PrivateKeyPkcs8ToPkcs1(keyContent);
                        break;
                    }
                }
                else
                {
                    result = SourceFormat == "xml" ? RsaKeyConvert.PublicKeyXmlToPem(keyContent) : RsaKeyConvert.PublicKeyPemToXml(keyContent);
                }

                if (!Directory.Exists(Output))
                {
                    Directory.CreateDirectory(Output);
                }
                string fileName = $"{new FileInfo(KeyFilePath).Name}.new.key";
                string savePath = Path.Combine(Output, fileName);
                File.WriteAllText(savePath, result);
                app.Out.WriteLine($"Process success.File saved in {savePath}.");
            }
            catch (Exception e)
            {
                app.Out.WriteLine($"Process error.Detail message:{e.Message}");
                return(1);
            }

            return(0);
        }