diff --git a/src/NETCore.Encrypt/EncryptProvider.cs b/src/NETCore.Encrypt/EncryptProvider.cs index c6ded6c..dba6aa6 100644 --- a/src/NETCore.Encrypt/EncryptProvider.cs +++ b/src/NETCore.Encrypt/EncryptProvider.cs @@ -328,7 +328,7 @@ public static void RijndaelEncrypt(string data, string key) rijndael.Mode = CipherMode.ECB; rijndael.Padding = PaddingMode.PKCS7; rijndael.KeySize = 256; - + } @@ -690,6 +690,8 @@ public static string RSAEncrypt(string publicKey, string srcString) return encryptStr; } + + /// /// RSA encrypt with pem key /// @@ -702,6 +704,8 @@ public static string RSAEncryptWithPem(string publicKey, string srcString) return encryptStr; } + + /// /// RSA encrypt /// @@ -742,6 +746,70 @@ public static string RSAEncrypt(string publicKey, string srcString, RSAEncryptio } } + /// + /// RSA encrypt + /// + /// public key + /// data byte[] + /// encrypted byte[] + public static byte[] RSAEncrypt(string publicKey, byte[] data) + { + byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.OaepSHA512); + return encryptBytes; + } + + /// + /// RSA encrypt with pem key + /// + /// pem public key + /// data byte[] + /// + public static byte[] RSAEncryptWithPem(string publicKey, byte[] data) + { + byte[] encryptBytes = RSAEncrypt(publicKey, data, RSAEncryptionPadding.Pkcs1, true); + return encryptBytes; + } + + /// + /// RSA encrypt + /// + /// public key + /// data byte[] + /// rsa encryptPadding RSAEncryptionPadding.Pkcs1 for linux/mac openssl + /// set key is pem format,default is false + /// encrypted byte[] + public static byte[] RSAEncrypt(string publicKey, byte[] data, RSAEncryptionPadding padding, bool isPemKey = false) + { + Check.Argument.IsNotEmpty(publicKey, nameof(publicKey)); + Check.Argument.IsNotNull(data, nameof(data)); + 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 = data; + + if (rawBytes.Length > maxLength) + { + throw new OutofMaxlengthException($"data is out of max encrypt length {maxLength}", maxLength, rsa.KeySize, padding); + } + + byte[] encryptBytes = rsa.Encrypt(rawBytes, padding); + return encryptBytes; + } + } + /// /// RSA decrypt /// @@ -799,6 +867,63 @@ public static string RSADecrypt(string privateKey, string srcString, RSAEncrypti } } + /// + /// RSA decrypt + /// + /// private key + /// encrypted byte[] + /// Decrypted string + public static byte[] RSADecrypt(string privateKey, byte[] data) + { + byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.OaepSHA512); + return decryptBytes; + } + + /// + /// RSA decrypt with pem key + /// + /// pem private key + /// encrypted byte[] + /// + public static byte[] RSADecryptWithPem(string privateKey, byte[] data) + { + byte[] decryptBytes = RSADecrypt(privateKey, data, RSAEncryptionPadding.Pkcs1, true); + return decryptBytes; + } + + /// + /// RSA encrypt + /// + /// public key + /// src string + /// rsa encryptPadding RSAEncryptionPadding.Pkcs1 for linux/mac openssl + /// set key is pem format,default is false + /// encrypted string + 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; + } + } + /// /// RSA from json string ///