Exemple #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();
        }
        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();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            //RSA_KEY_Converter();
            //RSA_PEM();
            //RSA_ECB_PKCS1Padding();
            //RSA_NONE_PKCS1Padding();

            //MD5_Sample();
            //SHA256_Sample();

            //HMacSha256_Sample();



            //SHA1WithDSA_Sample();

            //SHA256WithDSA_Sample();

            //SHA256WithECDSA_Sample();

            //SHA256WithRSA_Sample();

            //SM2_Sample();

            //AES_Sample();

            //DES_Sample();

            //TripleDES_Sample();


            //Certificate_Sample();

            var privateKey =
                "MIICWwIBAAKBgQCiivTXjTFqon5L0mljBLFQdf7X5kj/bddcC80XNGEljqkgFAamd3cD+xAZfrknGDD+7g8hvVx2SW92m6v6ky6h3euDXFj82cxn8VfXjQs0/t9wKl0n9+GAsDeo5tlnqyY5h1Dbqe3zyvmkX1hD4GR8gvnH/7dYQdxLTp5vfftbRwIDAQABAoGAOH1Tj3A2GunDO+WyE6QnXZ/MhEs31nHdtVMyoVxmYM+eTpQ3JXaCaeNA2qN0hLY/HPIuVxsA/ekSsGV01R5+x1uvwenhqrwnNTvcF9HP3H93jopgpJXHYaYbcLtYdnrrxd5Pm8mmVTV7r/co0CGEGO+sfQ2uLEZtfxbyvEvaFD0CQQDVx91c0mAZisyR56kKXE1/fKF38mX59F6wB8yx0I911IszhDcol4UgaF5amfXcN8bCDXJGJe9vbSclNRaWdxVzAkEAwqSmg3vtfjrE1FrkSl1pznTRoZhF72Hvpa8q/nJtHM7jMS53QGBuei6Ss+p3u/URj8o1bnNqvLuh7bJ7IM0N3QJAIvZCz5FgQg0fE6WNUbJywizBw3oTD2PVsHg2E8aGD8Eo2s3+r1bIYNpww+R1/wPoL4g/bhV6KQDy6/TYstba3QJAThetTxeLo5eEnQaSjVuJNfIcoU7s0Cxk7/6lq0zRhjtjX7oa0lNeP9srtM+flmOu1hf09AmOi4ZkY2+2guSCaQJAGs+oCQOZxDnc8Uf/xKEcFZet3UxBOoccaNPK6jCOwgGSkQDx3CxmCJd8o8xuTIknS7xLfZk7t9QUl6Q102NbJw==";
            //根据私钥提取公钥
            var publicKey = RSAKeyConverter.GetPublicKeyFromPrivateKeyPkcs1(privateKey);

            //公钥
            //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCiivTXjTFqon5L0mljBLFQdf7X5kj/bddcC80XNGEljqkgFAamd3cD+xAZfrknGDD+7g8hvVx2SW92m6v6ky6h3euDXFj82cxn8VfXjQs0/t9wKl0n9+GAsDeo5tlnqyY5h1Dbqe3zyvmkX1hD4GR8gvnH/7dYQdxLTp5vfftbRwIDAQAB

            var publicParameter = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));

            //公钥中取出模数Modulus转十六进制字符串
            var modulus = Hex.ToHexString(publicParameter.Modulus.ToByteArrayUnsigned()).ToUpper();

            //modulus=A28AF4D78D316AA27E4BD2696304B15075FED7E648FF6DD75C0BCD173461258EA9201406A6777703FB10197EB9271830FEEE0F21BD5C76496F769BABFA932EA1DDEB835C58FCD9CC67F157D78D0B34FEDF702A5D27F7E180B037A8E6D967AB26398750DBA9EDF3CAF9A45F5843E0647C82F9C7FFB75841DC4B4E9E6F7DFB5B47

            //公钥中取出指数Exponent转十六进制字符串
            var exponent = Hex.ToHexString(publicParameter.Exponent.ToByteArrayUnsigned());

            //exponent=010001

            var cipherText =
                "2356b0c72edb4b0340793ae3e39f0ff7f42290e3a1e8cfc14686787a4c923c5677adbbc2c734c2fdfcded2dbfe67044dec2399f0c19e97c3105271a1cdcfd616d9ddc78c387f136a694f4e004a11d51bae7c9eb33c3531cac1b0abc44147c3d9619434d422bc1f5eabf587eadfb1e8714205d0890dbd1295b7609b3fd76c382d";

            var password = RSA.DecryptFromHex(cipherText, AsymmetricKeyUtilities.GetAsymmetricKeyParameterFormPrivateKey(privateKey),
                                              Algorithms.RSA_NONE_PKCS1Padding);

            password = Strings.FromByteArray(Base64.Decode(password));

            //123456

            Console.ReadLine();
        }
Exemple #4
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);
        }