示例#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_KEY_Converter()
        {
            // pkcs8>>pkcs1
            Console.WriteLine("pkcs8 >> pkcs1");
            Console.WriteLine(RSAKeyConverter.PrivateKeyPkcs8ToPkcs1(pkcs8_1024_private_key));
            // pkcs1>>pkcs8
            Console.WriteLine("pkcs1 >> pkcs8");
            Console.WriteLine(RSAKeyConverter.PrivateKeyPkcs1ToPkcs8(pkcs1_1024_private_key));

            // pkcs8>>pkcs1 pem
            Console.WriteLine("pkcs8 >> pkcs1 pem");
            Console.WriteLine(RSAKeyConverter.PrivateKeyPkcs8ToPkcs1(pkcs8_1024_private_key, true));

            // pkcs1>>pkcs8 pem
            Console.WriteLine("pkcs1 >> pkcs8 pem");
            Console.WriteLine(RSAKeyConverter.PrivateKeyPkcs1ToPkcs8(pkcs1_1024_private_key, true));

            // private key pkcs1 >> public key
            Console.WriteLine(" private key pkcs1 >> public key");
            Console.WriteLine(RSAKeyConverter.GetPublicKeyFromPrivateKeyPkcs1(pkcs1_1024_private_key));

            // private key pkcs8 >> public key
            Console.WriteLine(" private key pkcs8 >> public key");
            Console.WriteLine(RSAKeyConverter.GetPublicKeyFromPrivateKeyPkcs8(pkcs8_1024_private_key));

            Console.WriteLine();
        }
示例#3
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();
        }
示例#4
0
 void DisplayRSAKeyInXml(RSASecretKey key, bool showPublicKey = true, bool showPrivateKey = true)
 {
     if (showPrivateKey)
     {
         Write(ConsoleColor.Green, "PrivateKey:");
         Console.WriteLine(FormatXml(RSAKeyConverter.ToXmlPrivateKey(key.PrivateKey)));
     }
     if (showPublicKey)
     {
         Write(ConsoleColor.Green, "PublicKey:");
         Console.WriteLine(FormatXml(RSAKeyConverter.ToXmlPublicKey(key.PublicKey)));
     }
 }
示例#5
0
        /// <summary>
        /// generate RSA secret key
        /// </summary>
        /// <param name="keySize">the size of the key,must from 384 bits to 16384 bits in increments of 8 </param>
        /// <returns></returns>
        RSASecretKey GenerateRSASecretKey(int keySize)
        {
            if (keySize % 8 != 0 || keySize < 384 || keySize > 16384)
            {
                throw new ArgumentOutOfRangeException(nameof(keySize), "The range of KeySize must from 384 bits to 16384 bits in increments of 8");
            }
            RSASecretKey rsaKey = new RSASecretKey();

            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(keySize))
            {
                rsaKey.PrivateKey = rsa.ToXmlString(true);
                rsaKey.PublicKey  = rsa.ToXmlString(false);
            }
            rsaKey.PrivateKey = RSAKeyConverter.FromXmlPrivateKey(rsaKey.PrivateKey);
            rsaKey.PublicKey  = RSAKeyConverter.FromXmlPublicKey(rsaKey.PublicKey);
            return(rsaKey);
        }
示例#6
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();
        }
示例#7
0
        void ShowDecryptPage()
        {
            Console.Clear();
            Console.WriteLine("----- C# RSA 操作示例 -----");
            int i = 0;

            foreach (RSASecretKey key in _generatedRSAKey)
            {
                Console.WriteLine("{0}:{1}", ++i, key.PrivateKey);
            }
            Console.Write("选择密钥:");
            int ch = int.Parse(Console.ReadLine());

            Console.Write("\r\n要解密的内容:");
            string content          = Console.ReadLine();
            string decryptedContent = RSADecrypt(/*_generatedRSAKey[ch - 1].PrivateKey*/ RSAKeyConverter.ToXmlPrivateKey(_generatedRSAKey[ch - 1].PrivateKey), content);

            Console.WriteLine("Result: {0}", decryptedContent);
            Console.ReadKey();
        }