Exemple #1
0
        /// <summary>
        /// Export RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="format"></param>
        /// <param name="usePemFormat"></param>
        /// <returns></returns>
        public static string ExportPublicKey(this MsRSA rsa, RsaKeyFormat format, bool usePemFormat = false)
        {
            var key = format switch
            {
                RsaKeyFormat.XML => rsa.ExportKeyInLvccXml(false),
                RsaKeyFormat.JSON => rsa.ExportKeyInJson(false),
#if NETCOREAPP3_1 || NETSTANDARD2_1
                RsaKeyFormat.Pkcs1 => BaseConv.ToBase64(rsa.ExportRSAPublicKey()),
                RsaKeyFormat.Pkcs8 => BaseConv.ToBase64(rsa.ExportRSAPublicKey()),
#else
                RsaKeyFormat.Pkcs1 => rsa.GetPublicKeyInPkcs1(),
                RsaKeyFormat.Pkcs8 => rsa.GetPublicKeyInPkcs8(),
#endif
                _ => throw new NotSupportedException("Unknown RSA key type.")
            };

            if (usePemFormat)
            {
                key = format switch
                {
                    RsaKeyFormat.XML => key,
                    RsaKeyFormat.JSON => key,
                    RsaKeyFormat.Pkcs1 => key.RemovePkcs1PublicKeyFormat(),
                    RsaKeyFormat.Pkcs8 => key.RemovePkcs8PublicKeyFormat(),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            return(key);
        }
Exemple #2
0
 /// <summary>
 /// Generate RSA key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey Generate(AsymmetricKeyMode mode, int keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false)
 {
     return(keyFormat switch
     {
         RsaKeyFormat.XML => GenerateInXml(mode, keySize),
         RsaKeyFormat.JSON => GenerateInJson(mode, keySize),
         RsaKeyFormat.Pkcs1 => GenerateInPkcs1(mode, keySize, keepingFormat),
         RsaKeyFormat.Pkcs8 => GenerateInPkcs8(mode, keySize, keepingFormat),
         _ => GenerateInXml(mode, keySize)
     });
Exemple #3
0
        private static RsaUtilBase TouchRsaUtilFromPrivateKey(RsaKeyFormat keyFormat, Encoding encoding, string privateKey, int size)
        {
            RsaUtilBase rsa = keyFormat switch
            {
                RsaKeyFormat.XML => new RsaXmlUtil(encoding, null, privateKey, size),
                RsaKeyFormat.JSON => new RsaJsonUtil(encoding, null, privateKey, size),
                RsaKeyFormat.Pkcs1 => new RsaPkcs1Util(encoding, null, privateKey, size),
                RsaKeyFormat.Pkcs8 => new RsaPkcs8Util(encoding, null, privateKey, size),
                _ => throw new NotSupportedException("Unknown RSA key type."),
            };

            return(rsa);
        }
        public void TestRsaEncryptAndDecrypt()
        {
            string _data               = "I am union guy."
            , _dataEncrypted           = ""
            , _dataDecrypted           = "";
            RsaKeyPair   _rsaKeyPair   = new RsaKeyPair();
            RsaKeyFormat _rsaKeyFormat = RsaKeyFormat.PEM;

            _dataEncrypted = RsaHelper.Encrypt(_rsaKeyFormat, _rsaKeyPair.PublicKey_PEM, _data);
            _dataDecrypted = RsaHelper.Decrypt(_rsaKeyFormat, _rsaKeyPair.PrivateKey_PEM, _dataEncrypted);

            Assert.AreEqual(_data, _dataDecrypted, string.Format("Data = {0}, DataEncrypted = {1}, DataDecrypted = {2}", _data, _dataEncrypted, _dataDecrypted));
        }
Exemple #5
0
        /// <summary>
        /// Import RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="format"></param>
        /// <param name="publicKey"></param>
        /// <param name="isPem"></param>
        public static void ImportPublicKey(this MsRSA rsa, RsaKeyFormat format, string publicKey, bool isPem = false)
        {
            if (isPem)
            {
                publicKey = format switch
                {
                    RsaKeyFormat.XML => publicKey,
                    RsaKeyFormat.JSON => publicKey,
                    RsaKeyFormat.Pkcs1 => publicKey.RemovePkcs1PublicKeyFormat(),
                    RsaKeyFormat.Pkcs8 => publicKey.RemovePkcs8PublicKeyFormat(),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            switch (format)
            {
            case RsaKeyFormat.XML:
                rsa.ImportKeyInLvccXml(publicKey);
                break;

            case RsaKeyFormat.JSON:
                rsa.ImportKeyInJson(publicKey);
                break;

            case RsaKeyFormat.Pkcs1:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(BaseConv.FromBase64(publicKey), out _);
#else
                rsa.TouchFromPublicKeyInPkcs1(publicKey, out _);
#endif
                break;

            case RsaKeyFormat.Pkcs8:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(BaseConv.FromBase64(publicKey), out _);
#else
                rsa.TouchFromPublicKeyInPkcs8(publicKey, out _);
#endif
                break;
            }
        }
    }
        public static string Encrypt(RsaKeyFormat rsaKeyFormat, string publicKey, string data)
        {
            KeyWorker _keyWorker;

            switch (rsaKeyFormat)
            {
                case RsaKeyFormat.XML:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.XML);
                    break;
                case RsaKeyFormat.ASN:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.ASN);
                    break;
                case RsaKeyFormat.PEM:
                    _keyWorker = new KeyWorker(publicKey, KeyFormat.PEM);
                    break;
                default:
                    throw new Exception(string.Format("the rsa key format: {0} is not supported.", rsaKeyFormat));
            }

            return _keyWorker.Encrypt(data);
        }
Exemple #7
0
        public static string Decrypt(RsaKeyFormat rsaKeyFormat, string privateKey, string data)
        {
            KeyWorker _keyWorker;

            switch (rsaKeyFormat)
            {
            case RsaKeyFormat.XML:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.XML);
                break;

            case RsaKeyFormat.ASN:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.ASN);
                break;

            case RsaKeyFormat.PEM:
                _keyWorker = new KeyWorker(privateKey, KeyFormat.PEM);
                break;

            default:
                throw new Exception(string.Format("the rsa key format: {0} is not supported.", rsaKeyFormat));
            }

            return(_keyWorker.Decrypt(data));
        }
Exemple #8
0
 /// <summary>
 /// Generate RSA private key.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="keyFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKey(string key, RsaKeyFormat keyFormat = RsaKeyFormat.XML) => RsaKeyGenerator.GeneratePublicKey(key, keyFormat);
Exemple #9
0
 /// <summary>
 /// Generate RSA private key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKey(RsaKeySize keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => RsaKeyGenerator.GeneratePublicKey(keySize, keyFormat, keepingFormat);
Exemple #10
0
 public static IRSA CreateWithPrivateKey(string key, RsaKeyFormat keyFormat) => new RsaFunction(RsaKeyGenerator.GeneratePrivateKey(key, keyFormat));
Exemple #11
0
 /// <summary>
 /// Generate RSA key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GenerateKey(AsymmetricKeyMode mode, RsaKeySize keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => RsaKeyGenerator.Generate(mode, keySize, keyFormat, keepingFormat);
Exemple #12
0
 /// <summary>
 /// Generate RSA private key.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="keyFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKey(string key, RsaKeyFormat keyFormat = RsaKeyFormat.XML) => Factory.GeneratePrivateKey(key, keyFormat);
Exemple #13
0
        public static ICryptoValue DecryptByPublicKey(string cipherText, string publicKey, RsaKeyFormat format, bool fOEAP, Encoding encoding = null)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.DecryptByPublicKey(cipherText, encoding));
        }
Exemple #14
0
        public static ICryptoValue EncryptByPrivateKey(byte[] originalData, string privateKey, RsaKeyFormat format)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.EncryptByPrivateKey(originalData));
        }
Exemple #15
0
 /// <summary>
 /// Generate RSA private key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKey(int keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => Factory.GeneratePrivateKey(keySize, keyFormat, keepingFormat);
Exemple #16
0
        public static ICryptoValue EncryptByPublicKey(string originalText, string publicKey, RsaKeyFormat format, RSAEncryptionPadding padding, Encoding encoding = null)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.EncryptByPublicKey(originalText, padding, encoding));
        }
Exemple #17
0
        public static ISignValue SignByPublicKey(byte[] originalData, string publicKey, RsaKeyFormat format, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.SignByPublicKey(originalData, hashAlgorithmName, padding));
        }
Exemple #18
0
        public static ISignValue SignByPrivateKey(byte[] originalData, string privateKey, RsaKeyFormat format, HashAlgorithmName hashAlgorithmName)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.SignByPrivateKey(originalData, hashAlgorithmName));
        }
Exemple #19
0
        public static ICryptoValue DecryptByPrivateKey(string cipherText, string privateKey, RsaKeyFormat format, RSAEncryptionPadding padding, Encoding encoding = null)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.DecryptByPrivateKey(cipherText, padding, encoding));
        }
Exemple #20
0
        public static ICryptoValue DecryptByPrivateKey(byte[] cipherData, string privateKey, RsaKeyFormat format, RSAEncryptionPadding padding)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.DecryptByPrivateKey(cipherData, padding));
        }
Exemple #21
0
        public static ICryptoValue EncryptByPublicKey(byte[] originalData, string publicKey, RsaKeyFormat format, RSAEncryptionPadding padding)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.EncryptByPublicKey(originalData, padding));
        }
Exemple #22
0
        public static bool VerifyByPrivateKey(byte[] originalData, byte[] signature, string privateKey, RsaKeyFormat format, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.VerifyByPrivateKey(originalData, signature, hashAlgorithmName, padding));
        }
Exemple #23
0
        public static bool VerifyByPrivateKey(string originalText, string signature, string privateKey, RsaKeyFormat format, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding, Encoding encoding = null)
        {
            var key      = RsaKeyGenerator.GeneratePrivateKey(privateKey, format);
            var function = Factory.Create(key);

            return(function.VerifyByPrivateKey(originalText, signature, hashAlgorithmName, padding, encoding));
        }
Exemple #24
0
        public static ISignValue SignByPublicKey(string originalText, string publicKey, RsaKeyFormat format, HashAlgorithmName hashAlgorithmName, RSASignaturePadding padding, Encoding encoding = null)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.SignByPublicKey(originalText, hashAlgorithmName, padding, encoding));
        }
Exemple #25
0
        public static ICryptoValue DecryptByPublicKey(byte[] cipherData, string publicKey, RsaKeyFormat format, bool fOEAP)
        {
            var key      = RsaKeyGenerator.GeneratePublicKey(publicKey, format);
            var function = Factory.Create(key);

            return(function.DecryptByPublicKey(cipherData));
        }