Beispiel #1
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="usePemFormat"></param>
        /// <returns></returns>
        public static string ExportPublicKey(this RSA rsa, RSAKeyTypes type, bool usePemFormat = false)
        {
            var key = type switch
            {
                RSAKeyTypes.XML => rsa.ToLvccXmlString(false),
                RSAKeyTypes.JSON => rsa.ToJsonString(false),
#if NETCOREAPP3_1 || NETSTANDARD2_1
                RSAKeyTypes.Pkcs1 => Base64Conv.ToBase64String(rsa.ExportRSAPublicKey()),
                RSAKeyTypes.Pkcs8 => Base64Conv.ToBase64String(rsa.ExportRSAPublicKey()),
#else
                RSAKeyTypes.Pkcs1 => rsa.ToPkcs1PublicString(),
                RSAKeyTypes.Pkcs8 => rsa.ToPkcs8PublicString(),
#endif
                _ => throw new NotSupportedException("Unknown RSA key type.")
            };

            if (usePemFormat)
            {
                key = type switch
                {
                    RSAKeyTypes.XML => key,
                    RSAKeyTypes.JSON => key,
                    RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormat(key),
                    RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormat(key),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            return(key);
        }
Beispiel #2
0
        /// <summary>
        /// Decrypt byte[] data with xml/json format.
        /// </summary>
        /// <param name="dataBytes">The data to be encrypted.</param>
        /// <param name="privateKey">The private key of xml format.</param>
        /// <param name="fOEAP"></param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns>The decrypted data.</returns>
        public static string DecryptByPrivateKey(
            byte[] dataBytes,
            string privateKey,
            bool fOEAP,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, Encoding.UTF8, privateKey, sizeType);

            return(rsa.DecryptByPrivateKey(dataBytes, fOEAP));
        }
        /// <summary>
        /// Encrypt byte[] data with xml/json format.
        /// </summary>
        /// <param name="dataBytes">The data to be encrypted.</param>
        /// <param name="privateKey">The public key of xml format.</param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <param name="padding"></param>
        /// <returns>The encrypted data.</returns>
        public static string EncryptByPrivateKey(
            byte[] dataBytes,
            string privateKey,
            RSAEncryptionPadding padding,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, Encoding.UTF8, privateKey, sizeType);

            return(rsa.EncryptByPrivateKey(dataBytes, padding));
        }
Beispiel #4
0
        /// <summary>
        /// Signature as byte[]
        /// </summary>
        /// <param name="data"></param>
        /// <param name="privateKey"></param>
        /// <param name="hashAlgorithmName"></param>
        /// <param name="encoding"></param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns></returns>
        public static byte[] SignatureByPrivateKeyAsBytes(
            string data,
            string privateKey,
            HashAlgorithmName hashAlgorithmName,
            Encoding encoding        = null,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType);

            return(rsa.SignDataByPrivateKeyToBytes(data, hashAlgorithmName));
        }
Beispiel #5
0
        /// <summary>
        /// Signature as string
        /// </summary>
        /// <param name="data"></param>
        /// <param name="publicKey"></param>
        /// <param name="hashAlgorithmName"></param>
        /// <param name="encoding"></param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns></returns>
        public static string SignatureByPublicKeyAsString(
            string data,
            string publicKey,
            HashAlgorithmName hashAlgorithmName,
            Encoding encoding        = null,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPublicKey(keyType, encoding, publicKey, sizeType);

            return(rsa.SignDataByPublicKey(data, hashAlgorithmName));
        }
        /// <summary>
        /// Encrypt string data with xml/json format.
        /// </summary>
        /// <param name="data">The data to be encrypted.</param>
        /// <param name="privateKey">The public key of xml format.</param>
        /// <param name="padding"></param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns>The encrypted data.</returns>
        public static string EncryptByPrivateKey(
            string data,
            string privateKey,
            RSAEncryptionPadding padding,
            Encoding encoding        = null,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType);

            return(rsa.EncryptByPrivateKey(data, padding));
        }
Beispiel #7
0
        /// <summary>
        /// Decrypt string data with xml/json format.
        /// </summary>
        /// <param name="data">The data to be encrypted.</param>
        /// <param name="privateKey">The private key of xml format.</param>
        /// <param name="fOEAP"></param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns>The decrypted data.</returns>
        public static string DecryptByPrivateKey(
            string data,
            string privateKey,
            bool fOEAP,
            Encoding encoding        = null,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType);

            return(rsa.DecryptByPrivateKey(data, fOEAP));
        }
Beispiel #8
0
        private static RSABase TouchRsaUtilFromPrivateKey(RSAKeyTypes keyType, Encoding encoding, string privateKey, RSAKeySizeTypes sizeType)
        {
            RSABase rsa = keyType switch
            {
                RSAKeyTypes.XML => new RSAXmlUtil(encoding, null, privateKey, (int)sizeType),
                RSAKeyTypes.JSON => new RSAJsonUtil(encoding, null, privateKey, (int)sizeType),
                RSAKeyTypes.Pkcs1 => new RSAPkcs1Util(encoding, null, privateKey, (int)sizeType),
                RSAKeyTypes.Pkcs8 => new RSAPkcs8Util(encoding, null, privateKey, (int)sizeType),
                _ => throw new NotSupportedException("Unknown RSA key type."),
            };

            return(rsa);
        }
Beispiel #9
0
        /// <summary>
        /// Verify
        /// </summary>
        /// <param name="data"></param>
        /// <param name="privateKey"></param>
        /// <param name="signature"></param>
        /// <param name="hashAlgorithmName"></param>
        /// <param name="encoding"></param>
        /// <param name="sizeType"></param>
        /// <param name="keyType"></param>
        /// <returns></returns>
        public static bool VerifyByPrivateKey(
            string data,
            string privateKey,
            string signature,
            HashAlgorithmName hashAlgorithmName,
            Encoding encoding        = null,
            RSAKeySizeTypes sizeType = RSAKeySizeTypes.R2048,
            RSAKeyTypes keyType      = RSAKeyTypes.XML)
        {
            var rsa = TouchRsaUtilFromPrivateKey(keyType, encoding, privateKey, sizeType);

            return(rsa.VerifyDataByPrivateKey(data, signature, hashAlgorithmName));
        }
Beispiel #10
0
        /// <summary>
        /// Encrypt byte[] data with xml/json format.
        /// </summary>
        /// <param name="dataBytes">The data to be encrypted.</param>
        /// <param name="publicKey">The public key of xml format.</param>
        /// <param name="keyType"></param>
        /// <returns>The encrypted data.</returns>
        public static string Encrypt(byte[] dataBytes, string publicKey, RSAKeyTypes keyType = RSAKeyTypes.XML)
        {
            using (var rsa = new RSACryptoServiceProvider()) {
                if (keyType == RSAKeyTypes.XML)
                {
                    rsa.FromLvccXmlString(publicKey);
                }
                else
                {
                    rsa.FromJsonString(publicKey);
                }

                return(Convert.ToBase64String(rsa.Encrypt(dataBytes, false)));
            }
        }
Beispiel #11
0
        /// <summary>
        /// Import RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="type"></param>
        /// <param name="publicKey"></param>
        /// <param name="isPem"></param>
        public static void ImportPublicKey(this RSA rsa, RSAKeyTypes type, string publicKey, bool isPem = false)
        {
            if (isPem)
            {
                publicKey = type switch
                {
                    RSAKeyTypes.XML => publicKey,
                    RSAKeyTypes.JSON => publicKey,
                    RSAKeyTypes.Pkcs1 => RSAPemFormatHelper.Pkcs1PublicKeyFormatRemove(publicKey),
                    RSAKeyTypes.Pkcs8 => RSAPemFormatHelper.Pkcs8PublicKeyFormatRemove(publicKey),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            switch (type)
            {
            case RSAKeyTypes.XML:
                rsa.FromLvccXmlString(publicKey);
                break;

            case RSAKeyTypes.JSON:
                rsa.FromJsonString(publicKey);
                break;

            case RSAKeyTypes.Pkcs1:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
                break;
#else
                rsa.FromPkcs1PublicString(publicKey, out _);
                break;
#endif

            case RSAKeyTypes.Pkcs8:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(Convert.FromBase64String(publicKey), out _);
                break;
#else
                rsa.FromPkcs8PublicString(publicKey, out _);
                break;
#endif
            }
        }
Beispiel #12
0
        public static RSAKey CreateKey(RSAKeySizeTypes size = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML)
        {
            using (var rsa = new RSACryptoServiceProvider((int)size)) {
                var publicKey = keyType == RSAKeyTypes.JSON
                    ? rsa.ToJsonString(false)
                    : rsa.ToLvccXmlString(false);

                var privateKey = keyType == RSAKeyTypes.JSON
                    ? rsa.ToJsonString(true)
                    : rsa.ToLvccXmlString(true);

                return(new RSAKey {
                    PublicKey = publicKey,
                    PrivateKey = privateKey,
                    Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
                    Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
                });
            }
        }
Beispiel #13
0
        /// <summary>
        /// Rsa from xml or json string
        /// </summary>
        /// <param name="rsaKey"></param>
        /// <param name="keyType"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static RSA RSAFromString(string rsaKey, RSAKeyTypes keyType = RSAKeyTypes.XML)
        {
            if (string.IsNullOrEmpty(rsaKey))
            {
                throw new ArgumentNullException(nameof(rsaKey));
            }

            var rsa = RSA.Create();

            if (keyType == RSAKeyTypes.XML)
            {
                rsa.FromLvccXmlString(rsaKey);
            }
            else
            {
                rsa.FromJsonString(rsaKey);
            }

            return(rsa);
        }
Beispiel #14
0
        /// <summary>
        /// Create a new <see cref="RSAKey"/>
        /// </summary>
        /// <param name="size"></param>
        /// <param name="keyType"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static RSAKey CreateKey(RSAKeySizeTypes size = RSAKeySizeTypes.R2048, RSAKeyTypes keyType = RSAKeyTypes.XML, bool format = false)
        {
            switch (keyType)
            {
            case RSAKeyTypes.Pkcs1:
                return(Core.RSAKeyGenerator.Pkcs1Key((int)size, format));

            case RSAKeyTypes.Pkcs8:
                return(Core.RSAKeyGenerator.Pkcs8Key((int)size, format));

            case RSAKeyTypes.XML:
                return(Core.RSAKeyGenerator.XmlKey((int)size));

            case RSAKeyTypes.JSON:
                return(Core.RSAKeyGenerator.JsonKey((int)size));

            default:
                return(Core.RSAKeyGenerator.XmlKey((int)size));
            }
        }
Beispiel #15
0
        /// <summary>
        /// Decrypt byte[] data with xml/json format.
        /// </summary>
        /// <param name="dataBytes">The data to be encrypted.</param>
        /// <param name="privateKey">The private key of xml format.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="keyType"></param>
        /// <returns>The decrypted data.</returns>
        public static string Decrypt(byte[] dataBytes, string privateKey, Encoding encoding = null, RSAKeyTypes keyType = RSAKeyTypes.XML)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            using (var rsa = new RSACryptoServiceProvider()) {
                if (keyType == RSAKeyTypes.XML)
                {
                    rsa.FromLvccXmlString(privateKey);
                }
                else
                {
                    rsa.FromJsonString(privateKey);
                }

                return(encoding.GetString(rsa.Decrypt(dataBytes, false)));
            }
        }
Beispiel #16
0
 /// <summary>
 /// Decrypt string data with xml/json format.
 /// </summary>
 /// <param name="data">The data to be encrypted.</param>
 /// <param name="privateKey">The private key of xml format.</param>
 /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
 /// <param name="keyType"></param>
 /// <returns>The decrypted data.</returns>
 public static string Decrypt(string data, string privateKey, Encoding encoding = null, RSAKeyTypes keyType = RSAKeyTypes.XML)
 {
     return(Decrypt(Convert.FromBase64String(data), privateKey, encoding, keyType));
 }
Beispiel #17
0
        /// <summary>
        /// Encrypt string data with xml/json format.
        /// </summary>
        /// <param name="data">The data to be encrypted.</param>
        /// <param name="publicKey">The public key of xml format.</param>
        /// <param name="encoding">The <see cref="T:System.Text.Encoding"/>,default is Encoding.UTF8.</param>
        /// <param name="keyType"></param>
        /// <returns>The encrypted data.</returns>
        public static string Encrypt(string data, string publicKey, Encoding encoding = null, RSAKeyTypes keyType = RSAKeyTypes.XML)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }

            return(Encrypt(encoding.GetBytes(data), publicKey, keyType));
        }