Example #1
0
        public void Rsa_Encrypt_LengthLimit_Test(RsaSize size)
        {
            var rsaKey = EncryptProvider.CreateRsaKey(size);

            var publicKey  = rsaKey.PublicKey;
            var privateKey = rsaKey.PrivateKey;

            //Act
            var rawStr = "eyJNb2R1bHVzIjoidHVSL1V1dFVSV0RSVElDYTFFRDcraUF2MUVnQUl0dC9oNkhHc0x6SG80QXAyVVdqWGtvRkp4T1NuRmdhY3d4cWM0WUg5UDdRaVIxQ1lCK3lvMnJUbkhZbVIrYWs2V3RJRU1YNWtmTTJrWHBNUVY2aFBrd0FxRTFpU1pWRUM2eXlmeTNGZUJTVmNnVlUwMFpJMGozbzhqT3ZMOXhneGhmT1J1eTcwM1RUbXdFPSIsIkV4cG9uZW50IjoiQVFBQiIsIlAiOiI3MVNIYVRnK2JvOXhzRnEzSXlrcHRFUXVHUXZTNDNEUDFoM04xcVlBN1E1VHpoS0IydEc1RWxvamtYTkF4d0VVVStxSnZMWDBxTHdzd09zRkhaL3lydz09IiwiUSI6Inc2R2ltem84a0lUL0xuS2U0Sk5QTUt2YTlVVzBSZUZlVzA5U1ZtVnFVWS9VeHl2eU9kemowd3JzTTZib1ZCU1JnZi9SbUZwRUZ1bUZTVW9yVWkxNVR3PT0iLCJEUCI6Im9yNXpPaXloMzZLeFozKzRhek54aFlDYmJES3JIRGc1VEZ1Ri9rRngvY0V4WWI4YUNFZDJ0ekVPWUxqandxOU1PR2dUYzN5enV3NEN6TWpEK01vc1J3PT0iLCJEUSI6InMvNGhhQVM2K0pVRlhDemxkT2JVTTRuTEdXUWFxempoNGMwbmlvb2d1ZzVGelVMbnlNa3RiRjFlV1YrMTNyWlY4bS8yM2VBZlNaMXRuckw1RE5EK0RRPT0iLCJJbnZlcnNlUSI6IlBPSkRGUk03MmVxd0R3TytldDFpTzIwTWlQcFVEUS93N1hEMHBMLzJWYTE4OEgrRGlaK0NuZDJRdnFYZyt4NFdNZSsrVlVNYXo2bWM3V1g4WnBaWW9RPT0iLCJEIjoiWE1QUEZPYktDcHFON21pNG4zb0tsSmFveTlwdFAwRG9FWXBydGc4NmoyS2RWMWZzQWhJM1JOZTNvRmRMcXhrY0VWWmxTTTNLUmhHeUxnRkY0WDk0cnVIYjBQeC9LZVQxMW1BeDNvQ2NCRVlWelhabXlIUHQzWCs2dlBMZzdmYUhtRmlxK3N0Y2NMTlBNSEdna2lkWTF6NGtiTXZwZnBlOWxhN0VMWUdKM21VPSJ9";

            //RSAEncryptionPaddingMode is Pkcs1
            var padding   = RSAEncryptionPadding.Pkcs1;
            var maxLength = ((int)size - 384) / 8 + 37;
            var rawData   = rawStr.Substring(0, maxLength);

            var encryptedStr = EncryptProvider.RSAEncrypt(publicKey, rawData, padding);
            var decryptedStr = EncryptProvider.RSADecrypt(privateKey, encryptedStr, padding);

            //RSAEncryptionPaddingMode is Oaep
            padding = RSAEncryptionPadding.OaepSHA1;

            var sha1   = "oaep".SHA1();
            var length = sha1.Length;

            maxLength = (int)size / 8 - 42;   //214 //40
            rawData   = rawStr.Substring(0, maxLength);

            encryptedStr = EncryptProvider.RSAEncrypt(publicKey, rawData, padding);
            decryptedStr = EncryptProvider.RSADecrypt(privateKey, encryptedStr, padding);
            Assert.Equal(decryptedStr, rawData);


            padding = RSAEncryptionPadding.OaepSHA256;

            maxLength = (int)size / 8 - 66;   //190   //64
            rawData   = rawStr.Substring(0, maxLength);

            encryptedStr = EncryptProvider.RSAEncrypt(publicKey, rawData, padding);
            decryptedStr = EncryptProvider.RSADecrypt(privateKey, encryptedStr, padding);

            Assert.Equal(decryptedStr, rawData);

            padding   = RSAEncryptionPadding.OaepSHA384;
            maxLength = (int)size / 8 - 98;  //158  //96
            rawData   = rawStr.Substring(0, maxLength);

            encryptedStr = EncryptProvider.RSAEncrypt(publicKey, rawData, padding);
            decryptedStr = EncryptProvider.RSADecrypt(privateKey, encryptedStr, padding);

            Assert.Equal(decryptedStr, rawData);

            padding   = RSAEncryptionPadding.OaepSHA512;
            maxLength = (int)size / 8 - 130; //126  // 128
            rawData   = rawStr.Substring(0, maxLength);

            encryptedStr = EncryptProvider.RSAEncrypt(publicKey, rawData, padding);
            decryptedStr = EncryptProvider.RSADecrypt(privateKey, encryptedStr, padding);

            Assert.Equal(decryptedStr, rawData);
        }
Example #2
0
        /// <summary>
        /// 创建Xml格式的RSA密钥对,默认【密钥长度】为2048
        /// </summary>
        /// <param name="size">密钥长度</param>
        /// <returns></returns>
        public (string publicKey, string privateKey) CreateXmlKey(RsaSize size = RsaSize.R2048)
        {
            using RSA rsa = RSA.Create();
            rsa.KeySize   = (int)size;
            var te = rsa.ToXmlString(false);

            return(rsa.ToXmlString(false), rsa.ToXmlString(true));
        }
Example #3
0
        public void Rsa_Encrypt_WithPadding_Test(RsaSize size)
        {
            var rsaKey    = EncryptProvider.CreateRsaKey(size);
            var srcString = "rsa encrypt";

            //Act
            var encrypted = EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString, RSAEncryptionPadding.Pkcs1);

            //Assert
            Assert.NotEmpty(encrypted);
        }
Example #4
0
        public void Rsa_Encrypt_Success_Test(RsaSize size)
        {
            var rsaKey    = EncryptProvider.CreateRsaKey(size);
            var srcString = "rsa encrypt";

            //Act
            var encrypted = EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString);

            //Assert
            Assert.NotEmpty(encrypted);
        }
Example #5
0
        public void Create_RSAKey_Test(RsaSize size)
        {
            //Act
            var rsaKey = EncryptProvider.CreateRsaKey(size);

            //Assert
            Assert.NotNull(rsaKey);
            Assert.NotEmpty(rsaKey.PublicKey);
            Assert.NotEmpty(rsaKey.PrivateKey);
            Assert.NotEmpty(rsaKey.Exponent);
            Assert.NotEmpty(rsaKey.Modulus);
        }
Example #6
0
        public void Rsa_Decrypt_Success_Test(RsaSize size)
        {
            var rsaKey    = EncryptProvider.CreateRsaKey(size);
            var srcString = "rsa decrypt";

            //Act
            var encrypted = EncryptProvider.RSAEncrypt(rsaKey.PublicKey, srcString);
            var decrypted = EncryptProvider.RSADecrypt(rsaKey.PrivateKey, encrypted);

            //Assert
            Assert.NotEmpty(encrypted);
            Assert.NotEmpty(decrypted);
            Assert.Equal(srcString, decrypted);
        }
#pragma warning restore CS1591


        /// <summary>
        /// 生成 RSA 公钥和私钥
        /// </summary>
        /// <param name="rsaSize">密钥长度</param>
        public static RSAKey GenerateRSAKeys(RsaSize rsaSize = RsaSize.R2048)
        {
#if NET461
            using (var rsa = new RSACryptoServiceProvider())
                return new RSAKey()
                       {
                           PublicKey  = rsa.ToXmlString(false),
                           PrivateKey = rsa.ToXmlString(true),
                           Exponent   = rsa.ExportParameters(false).Exponent.ToHexString(),
                           Modulus    = rsa.ExportParameters(false).Modulus.ToHexString()
                       };
#else
            return((RSAKey)EncryptProvider.CreateRsaKey((NETCore.Encrypt.RsaSize)rsaSize));
#endif
        }
Example #8
0
        /// <summary>
        /// Create an RSA key
        /// </summary>
        /// <param name="keySizeInBits">the default size is 2048</param>
        /// <returns></returns>
        public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
        {
            using (RSA rsa = RSA.Create((int)rsaSize))
            {
                string publicKey  = rsa.ToJsonString(false);
                string privateKey = rsa.ToJsonString(true);

                return(new RSAKey()
                {
                    PublicKey = publicKey,
                    PrivateKey = privateKey,
                    Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
                    Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
                });
            }
        }
        public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048)
        {
            using (System.Security.Cryptography.RSA rsa = System.Security.Cryptography.RSA.Create())
            {
                rsa.KeySize = (int)rsaSize;



                string publicKey  = rsa.ToJsonString(false);
                string privateKey = rsa.ToJsonString(true);

                return(new RSAKey()
                {
                    PublicKey = publicKey,
                    PrivateKey = privateKey,
                    Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
                    Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
                });
            }
        }
Example #10
0
        /// <summary>
        /// Create an RSA key
        /// </summary>
        /// <param name="rsaSize">the default size is 2048</param>
        /// <param name="rsaKeyType">key serialization format</param>
        /// <returns></returns>
        public static RSAKey CreateRsaKey(RsaSize rsaSize = RsaSize.R2048, RsaKeyType rsaKeyType = RsaKeyType.JSON)
        {
            //#if NET461
            using (var rsa = new RSACryptoServiceProvider((int)rsaSize))
            //#else // only netcoreapp2.0
            //using (RSA rsa = RSA.Create((int)rsaSize))
            //#endif
            {
                //string publicKey = rsa.ToJsonString(false);
                //string privateKey = rsa.ToJsonString(true);
                string publicKey = string.Empty, privateKey = string.Empty;
                switch (rsaKeyType)
                {
                case RsaKeyType.JSON:
                    publicKey  = rsa.ToJsonString(false);
                    privateKey = rsa.ToJsonString(true);
                    break;

                case RsaKeyType.XML:
#if NET461
                    publicKey  = rsa.ToXmlString(false);
                    privateKey = rsa.ToXmlString(true);
#else
                    publicKey  = rsa.ToLvccXmlString(false);
                    privateKey = rsa.ToLvccXmlString(true);
#endif
                    break;
                }

                return(new RSAKey()
                {
                    PublicKey = publicKey,
                    PrivateKey = privateKey,
                    Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
                    Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
                });
            }
        }
Example #11
0
        /// <summary>
        /// 生成 RSA 公钥和私钥
        /// </summary>
        /// <param name="publicKey">公钥</param>
        /// <param name="privateKey">私钥</param>
        /// <param name="rsaSize">加密长度</param>
        /// <param name="rsaKeyType">密钥序列化格式(默认Json)</param>
        public static void GenerateRSAKeys(out string publicKey, out string privateKey, RsaSize rsaSize = RsaSize.R2048, RsaKeyType rsaKeyType = RsaKeyType.JSON)
        {
            //using (var rsa = new RSACryptoServiceProvider())
            //{
            //    publicKey = rsa.ToXmlString(false);
            //    privateKey = rsa.ToXmlString(true);
            //}
            var key = EncryptProvider.CreateRsaKey(rsaSize, rsaKeyType);

            publicKey  = key.PublicKey;
            privateKey = key.PrivateKey;
        }
Example #12
0
 /// <summary>
 /// 创建PEM格式的RSA密钥对,默认填充为【PKCS#8】,默认【密钥长度】为2048
 /// <para>【isPKCS8】为真时返回PKCS#8格式,否则返回PKCS#1格式,默认为【PKCS#8】</para>
 /// </summary>
 /// <param name="isPKCS8">是否为【PKCS#8】填充方式</param>
 /// <param name="size">密钥长度</param>
 /// <returns></returns>
 public (string publicKey, string privateKey) CreatePemKey(bool isPKCS8 = true, RsaSize size = RsaSize.R2048)
 {
     using RSA rsa = RSA.Create();
     rsa.KeySize   = (int)size;
     return(RsaToPEM(rsa, false, isPKCS8), RsaToPEM(rsa, true, isPKCS8));
 }
Example #13
0
 /// <summary>
 /// 创建密钥对,Xml格式的公钥和私钥
 /// </summary>
 /// <param name="xmlPrivateKey">公钥</param>
 /// <param name="xmlPublicKey">私钥</param>
 /// <param name="size">生成秘钥位数</param>
 public static void CreateKey(out string xmlPrivateKey, out string xmlPublicKey, RsaSize size = RsaSize.R1024)
 {
     CreateKey(out xmlPrivateKey, out xmlPublicKey, size.Value());
 }