示例#1
0
        /// <summary>
        /// RSA encrypt
        /// </summary>
        /// <param name="publicKey">public key</param>
        /// <param name="srcString">src string</param>
        /// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
        /// <param name="isPemKey">set key is pem format,default is false</param>
        /// <returns>encrypted string</returns>
        public static string RSADecrypt(string privateKey, string srcString, RSAEncryptionPadding padding, bool isPemKey = false)
        {
            Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
            Check.Argument.IsNotEmpty(srcString, nameof(srcString));
            Check.Argument.IsNotNull(padding, nameof(padding));

            RSA rsa;

            if (isPemKey)
            {
                rsa = RsaProvider.FromPem(privateKey);
            }
            else
            {
                rsa = RSA.Create();
                rsa.FromJsonString(privateKey);
            }

            using (rsa)
            {
                byte[] srcBytes     = srcString.ToBytes();
                byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
                return(Encoding.UTF8.GetString(decryptBytes));
            }
        }
示例#2
0
        /// <summary>
        /// RSA encrypt
        /// </summary>
        /// <param name="publicKey">public key</param>
        /// <param name="srcString">src string</param>
        /// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
        /// <param name="isPemKey">set key is pem format,default is false</param>
        /// <returns>encrypted string</returns>
        public static string RSAEncrypt(string publicKey, string srcString, RSAEncryptionPadding padding, bool isPemKey = false)
        {
            Check.Argument.IsNotEmpty(publicKey, nameof(publicKey));
            Check.Argument.IsNotEmpty(srcString, nameof(srcString));
            Check.Argument.IsNotNull(padding, nameof(padding));

            RSA rsa;

            if (isPemKey)
            {
                rsa = RsaProvider.FromPem(publicKey);
            }
            else
            {
                rsa = RSA.Create();
                rsa.FromJsonString(publicKey);
            }

            using (rsa)
            {
                var maxLength = GetMaxRsaEncryptLength(rsa, padding);
                var rawBytes  = Encoding.UTF8.GetBytes(srcString);

                if (rawBytes.Length > maxLength)
                {
                    throw new OutofMaxlengthException($"'{srcString}' is out of max encrypt length {maxLength}", maxLength, rsa.KeySize, padding);
                }

                byte[] encryptBytes = rsa.Encrypt(rawBytes, padding);
                return(encryptBytes.ToHexString());
            }
        }
示例#3
0
        /// <summary>
        /// RSA encrypt
        /// </summary>
        /// <param name="publicKey">public key</param>
        /// <param name="data">src string</param>
        /// <param name="padding">rsa encryptPadding <see cref="RSAEncryptionPadding"/> RSAEncryptionPadding.Pkcs1 for linux/mac openssl </param>
        /// <param name="isPemKey">set key is pem format,default is false</param>
        /// <returns>encrypted string</returns>
        public static byte[] RSADecrypt(string privateKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false)
        {
            Check.Argument.IsNotEmpty(privateKey, nameof(privateKey));
            Check.Argument.IsNotNull(data, nameof(data));
            Check.Argument.IsNotNull(padding, nameof(padding));

            RSA rsa;

            if (isPemKey)
            {
                rsa = RsaProvider.FromPem(privateKey);
            }
            else
            {
                rsa = RSA.Create();
                rsa.FromJsonString(privateKey);
            }

            using (rsa)
            {
                byte[] srcBytes     = data;
                byte[] decryptBytes = rsa.Decrypt(srcBytes, padding);
                return(decryptBytes);
            }
        }
示例#4
0
 /// <summary>
 /// RSA From pem
 /// </summary>
 /// <param name="pem"></param>
 /// <returns></returns>
 public static RSA RSAFromPem(string pem)
 {
     Check.Argument.IsNotEmpty(pem, nameof(pem));
     return(RsaProvider.FromPem(pem));
 }
示例#5
0
 /// <summary>
 /// RSA From pem
 /// </summary>
 /// <param name="pem"></param>
 /// <returns></returns>
 public static System.Security.Cryptography.RSA RSAFromPem(string pem)
 {
     Check.Argument.IsNotEmpty(pem, nameof(pem));
     return(RsaProvider.FromPem(pem));
 }