示例#1
0
        private static void RSA_NONE_PKCS1Padding()
        {
            var data = "RSA_NONE_PKCS1Padding";

            //rsa pkcs1 private key encrypt
            var encryptdata1 = RSA.EncryptToBase64(data, AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPrivateKey(pkcs1_1024_private_key),
                                                   Algorithms.RSA_NONE_PKCS1Padding);

            Console.WriteLine(encryptdata1);

            //rsa pkcs1 private key encrypt
            //algorithm  rsa/none/pkcs1padding
            var encryptdata2 = RSA.EncryptToBase64(data,
                                                   AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormAsn1PrivateKey(pkcs8_1024_private_key),
                                                   Algorithms.RSA_NONE_PKCS1Padding);

            Console.WriteLine(encryptdata2);

            Console.WriteLine(encryptdata1.Equals(encryptdata2));

            var _1024_public_key = RSAKeyConverter.GetPublicKeyFromPrivateKeyPkcs1(pkcs1_1024_private_key);

            //rsa public key decrypt
            //algorithm  rsa/none/pkcs1padding
            Console.WriteLine(RSA.DecryptFromBase64(encryptdata2, AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPublicKey(_1024_public_key), Algorithms.RSA_NONE_PKCS1Padding));

            Console.WriteLine();
        }
示例#2
0
        private static void RSA_ECB_PKCS1Padding()
        {
            var data = "hello rsa";

            Console.WriteLine($"加密原文:{data}");

            // rsa pkcs8 private key encrypt
            //algorithm  rsa/ecb/pkcs1padding
            var pkcs8data = RSA.EncryptToBase64(data, RSAUtilities.GetAsymmetricKeyParameterFormAsn1PrivateKey(pkcs8_1024_private_key), CipherAlgorithms.RSA_ECB_PKCS1Padding);

            Console.WriteLine("密钥格式:pkcs8,密文算法:rsa/ecb/pkcs1padding,加密结果");
            Console.WriteLine(pkcs8data);

            //rsa pkcs1 private key encrypt
            //algorithm  rsa/ecb/pkcs1padding
            var pkcs1data = RSA.EncryptToBase64(data, RSAUtilities.GetAsymmetricKeyParameterFormPrivateKey(pkcs1_1024_private_key), CipherAlgorithms.RSA_ECB_PKCS1Padding);

            Console.WriteLine($"密钥格式:pkcs1,密文算法:rsa/ecb/pkcs1padding");
            Console.WriteLine(pkcs1data);

            Console.WriteLine($"加密结果比对是否一致:{pkcs8data.Equals(pkcs1data)}");

            var _1024_public_key = RSAKeyConverter.GetPublicKeyFromPrivateKeyPkcs1(pkcs1_1024_private_key);

            Console.WriteLine($"从pkcs1私钥中提取公钥:");
            Console.WriteLine(_1024_public_key);

            Console.WriteLine("使用公钥解密数据:");
            //rsa public key decrypt
            //algorithm  rsa/ecb/pkcs1padding
            Console.WriteLine(RSA.DecryptFromBase64(pkcs1data, RSAUtilities.GetAsymmetricKeyParameterFormPublicKey(_1024_public_key), CipherAlgorithms.RSA_ECB_PKCS1Padding));

            Console.WriteLine();
        }
示例#3
0
        private static void Certificate_Sample()
        {
            var algorithm = "RSA";
            var keySize   = 2048;

            //颁发者DN
            var issuer = new X509Name(new ArrayList
            {
                X509Name.C,
                X509Name.O,
                X509Name.OU,
                X509Name.L,
                X509Name.ST
            }, new Hashtable
            {
                [X509Name.C]  = "CN",
                [X509Name.O]  = "Fulu Newwork",
                [X509Name.OU] = "Fulu RSA CA 2020",
                [X509Name.L]  = "Wuhan",
                [X509Name.ST] = "Hubei",
            });
            //使用者DN
            var subject = new X509Name(new ArrayList
            {
                X509Name.C,
                X509Name.O,
                X509Name.CN
            }, new Hashtable
            {
                [X509Name.C]  = "CN",
                [X509Name.O]  = "ICH",
                [X509Name.CN] = "*.fulu.com"
            });

            CertificateGenerator.GenerateCertificate(new GenerateCertificateOptions {
                Path = "mypfx.pfx", Issuer = issuer, Subject = subject
            });

            var password           = "******";        //证书密码
            var signatureAlgorithm = "SHA256WITHRSA"; //签名算法

            //var keyP = RSAKeyGenerator.Pkcs1();

            //var pK = AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPrivateKey(keyP.PrivateKey);

            //CertificateGenerator.GenerateCertificate(new GenerateCertificateOptions{ Path = "mypfx.pfx", Issuer = issuer, Subject = subject });

            //生成证书
            // CertificateGenerator.X509V3(algorithm, keySize, password, signatureAlgorithm, DateTime.Now.AddDays(-1),DateTime.Now.AddDays(2), issuer, subject, "mycert.cert", "mypfx.pfx");

            var pfx = new X509Certificate2("mypfx.pfx", password, X509KeyStorageFlags.Exportable);



            var keyPair2 = DotNetUtilities.GetKeyPair(pfx.PrivateKey);

            var subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair2.Public);
            var privateKeyInfo       = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair2.Private);

            var privateKey = Base64.ToBase64String(privateKeyInfo.ParsePrivateKey().GetEncoded());
            var publicKey  = Base64.ToBase64String(subjectPublicKeyInfo.GetEncoded());

            //var cert = new X509Certificate2("mycert.cert", string.Empty, X509KeyStorageFlags.Exportable);

            //var publicKey2 = Base64.ToBase64String(SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(DotNetUtilities.FromX509Certificate(cert).GetPublicKey()).GetEncoded());

            Console.ForegroundColor = ConsoleColor.DarkYellow;

            Console.WriteLine("Pfx证书私钥:");
            Console.WriteLine(privateKey);

            Console.WriteLine("Pfx证书公钥:");
            Console.WriteLine(publicKey);

            //Console.WriteLine("Cert证书公钥:");
            //Console.WriteLine(publicKey2);

            var data = "hello rsa";

            Console.WriteLine($"加密原文:{data}");

            var pkcs1data = RSA.EncryptToBase64(data, AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPublicKey(publicKey), Algorithms.RSA_ECB_PKCS1Padding);

            Console.WriteLine("加密结果:");
            Console.WriteLine(pkcs1data);

            //pkcs1data =
            //    "KGbgP3Ns6kFyjJ7tbepdZ3X8zssoHKWyVzVesghWg8fFP0ZMVumf+iXJ93LBu3xqKWE/5JTr1qFc5u0Cm3BUPnusMjBTgMrQk3zopVOELpChFbkeTR2YHsdDZdBzaJVN4SQQwHMkp2w8Pyb9x1NjsFoHHQEskBUNnOEuGkEFZdg=";

            Console.WriteLine("解密结果:");
            var datares = RSA.DecryptFromBase64(pkcs1data,
                                                AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPrivateKey(privateKey), Algorithms.RSA_ECB_PKCS1Padding);

            Console.WriteLine(datares);
        }