|
|
@@ -0,0 +1,290 @@
|
|
|
+using Cn.Ubingo.Security.RSA.Core;
|
|
|
+using Cn.Ubingo.Security.RSA.Data;
|
|
|
+using Cn.Ubingo.Security.RSA.Key;
|
|
|
+using System;
|
|
|
+using System.Security.Cryptography;
|
|
|
+using System.Text;
|
|
|
+
|
|
|
+namespace AuthorizationSign
|
|
|
+{
|
|
|
+ /// <summary>
|
|
|
+ /// RSA加解密
|
|
|
+ /// </summary>
|
|
|
+ public class RsaHelper
|
|
|
+ {
|
|
|
+ #region Const
|
|
|
+ /// <summary>
|
|
|
+ /// ASN_PUBLIC_KEY
|
|
|
+ /// </summary>
|
|
|
+ public const string ASN_PUBLIC_KEY = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCMivG439/MBZoeZlWDo5So0Y5lMcw7QOPkvNqtVMhuThVtlLKjmg5AdT2oZejAQojlq5kHEhhRSFAsALzEaRCLJmf+8JwmvIBg8n/Vr8z4ALyfsrpSrU/3J/USC+K2f8OAFutj7WD0H21xY8KcvTTQmJ+hy6VKb8C6Ma324vmxOwIDAQAB";
|
|
|
+ /// <summary>
|
|
|
+ /// ASN_PRIVATE_KEY
|
|
|
+ /// </summary>
|
|
|
+ public const string ASN_PRIVATE_KEY = @"MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANeVbJM1ehuc5+6uGuzSSiSkRic6LF6jitWKotykPi8w0jozsn/RwGANIs74CAp5qbe/utFdlDVMh3errfp35n39dkyDDl6RNtMJ2Kh2UQiqhPcxG5k+ZRnZhMf4Gle+lmgO7tbeVXiYinXzcVD/qdpRUSBppWLHy5z2wSJZ1gDJAgMBAAECgYA6m+P1rW8x/JlNWUX2pkED7yknVvhjDkEbRuJ566kAOVyd+BoKB+Mt+cRB0kqVVngefWBztOYrC1A055aX5mrhy6qff5CNL0INmK50RwU3U0LAod1a7oa+YPQaA97fc+D+soduFXCq7tGFeLrweDy/gEITH/0PZuMUhv39FO8hdQJBAPxwxAyVDwaYgDpeWSYojRUERi7ZG6GARjfI4dk9i58fFsEHfVM1yUu/3aOUYISe3jTJm0I/E/Boi7R7nnvXObcCQQDan53IW6wpivUEnGu2Yani3Ep3v98TIqBIqHHWmx1+gYZx87KdSfNxG6DFcoKwnoIGHvTd1M8X84GIH3Aj/pl/AkBsdwtVmKpHHDlkWuuVILFVyWtab8CWiOYiy/Z2gCJAsXv2XoBzw1WFbaAOzA8YjKZPKhzCvxWTI2h7mO4EqfpLAkEAwBmU2OLEalRxazXO55j1YCPVQ1fTOcspL+7i6IGv7uL9Mc/C2LaMNIzkPdqav3roIVy+KNW/ivoqjWOic95ZHQJBAOhM84QH3QFx/g+7+2VsO1agYN2lt1fSSOKmmNnMn8tnB/z/1IGKmFWLJYlNVfSqQGvxYEDywqOOyhUAXu6FHy0=";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// XML_PUBLIC_KEY
|
|
|
+ /// </summary>
|
|
|
+ public const string XML_PUBLIC_KEY = @"<RSAKeyValue><Modulus>xF/Qn+vx7g5FzsbFPgx1rWwdqOge8VyXJBE90qmJBzn2E5qvb0OhLdk0+xpodopHQ50/W1sD5cw/qNXyrf+lEmpV9QjR1naCDcJAQN+n88eXMXTqdqs/K6ddDEfFGiXcS20rd8sBv3KdnafD5DUJqQcW8a1P8s2OohfX/q24oOU=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
|
|
+ /// <summary>
|
|
|
+ /// XML_PRIVATE_KEY
|
|
|
+ /// </summary>
|
|
|
+ public const string XML_PRIVATE_KEY = @"<RSAKeyValue><Modulus>xF/Qn+vx7g5FzsbFPgx1rWwdqOge8VyXJBE90qmJBzn2E5qvb0OhLdk0+xpodopHQ50/W1sD5cw/qNXyrf+lEmpV9QjR1naCDcJAQN+n88eXMXTqdqs/K6ddDEfFGiXcS20rd8sBv3KdnafD5DUJqQcW8a1P8s2OohfX/q24oOU=</Modulus><Exponent>AQAB</Exponent><P>xM7T6yXOAtjhVWZDsYGixdLUNFPfNjs3WOhYWhn5BUnI2YETHhQ7xKkTDOG8zZyzrUZr932e30eNMxepv64rUw==</P><Q>/2+ZTP9N3WBvTIH2iPByRZLqo7fpj8eHGlbTv0ZmQlxiqzRIZ4lhWOe32wdrAM+ATPVBtIYtVOA56YmVl5oz5w==</Q><DP>DBcQma922mHsZcykd/EYyMJM2vIUEkbM/Z84XkCeNZ70wbfWLtDbYxdn3jC34oGv4AMlesyyx9CjcfIQMYBHlQ==</DP><DQ>y8ID8a2+ORdR8R/Cv12+ac1WKp7sSusI1HPqmSvZKppzgspD74NhCgBdYvS54H2lhL4nWY+m3Yu35pj/oV06hQ==</DQ><InverseQ>pMOYsFMg5383XWj3FajSngFQdaa5LP3GLDdo4UPOqOM1Do6QpCFoadqvqwdg3eHxSWma3yS8UppjisEtlQG48Q==</InverseQ><D>ZLoA+Qa9ga4wgG/UnI8njVz3mbv2ozcrD4UIgRYVA2/nmQavFhNKiYnwcxyImMU06Kmv5dw5k5XA0YlsgXOv52pKh4+yCcMsbFY60K3NhAJkGrX8QpZi+GDTMjqZ0IBKovNKIhY11qZpfXCLcjfwbCm8DpXbybDF4W0aF+ruicE=</D></RSAKeyValue>";
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// PEM_PUBLIC_KEY
|
|
|
+ /// </summary>
|
|
|
+ public const string PEM_PUBLIC_KEY = @"-----BEGIN PUBLIC KEY-----
|
|
|
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDV8XjtTJrZGvYB2th3ZJTnatT4
|
|
|
+ YDFcXim7tTUFQYP+6DSEy06jdt+RkFKjYMrFCj8CuwA0Lm3/QNYK6gdoXwMjMazE
|
|
|
+ QEj3GInTUfbEMQjUS9oXq20QdxDVTyvldHFMYUP9RbPr9velfoSglaz7ZKLO6JFk
|
|
|
+ VAkOw2MkDiGZWkKZnQIDAQAB
|
|
|
+ ----END PUBLIC KEY-----
|
|
|
+ ";
|
|
|
+ /// <summary>
|
|
|
+ /// P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VATE KEY-----
|
|
|
+ ";
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 生成公/私密钥
|
|
|
+ /// <summary>
|
|
|
+ /// 生成公/私密钥
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="rsaKeyTypes">密钥格式</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string[] CreateRSAKey(RSAKeyTypes rsaKeyTypes)
|
|
|
+ {
|
|
|
+ KeyPair _keyPair = KeyGenerator.GenerateKeyPair();
|
|
|
+ string[] _ary = new string[2];
|
|
|
+ KeyPair _kp = _keyPair.ToXMLKeyPair();
|
|
|
+ if (rsaKeyTypes == RSAKeyTypes.ASNKey)
|
|
|
+ {
|
|
|
+ _kp = _keyPair.ToASNKeyPair();
|
|
|
+ }
|
|
|
+ else if (rsaKeyTypes == RSAKeyTypes.PEMKey)
|
|
|
+ {
|
|
|
+ _kp = _keyPair.ToPEMKeyPair();
|
|
|
+ }
|
|
|
+ _ary[0] = _kp.PublicKey;
|
|
|
+ _ary[1] = _kp.PrivateKey;
|
|
|
+ return _ary;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// RSA加密
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="rsaKeyTypes">密钥格式</param>
|
|
|
+ /// <param name="encryptString">需要加密的字符串</param>
|
|
|
+ /// <param name="publicKey">RSA公钥xml字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string EncryptRSA(RSAKeyTypes rsaKeyTypes, string encryptString, string publicKey)
|
|
|
+ {
|
|
|
+ KeyFormat _kf = KeyFormat.XML;
|
|
|
+ if (rsaKeyTypes == RSAKeyTypes.ASNKey)
|
|
|
+ {
|
|
|
+ _kf = KeyFormat.ASN;
|
|
|
+ }
|
|
|
+ else if (rsaKeyTypes == RSAKeyTypes.PEMKey)
|
|
|
+ {
|
|
|
+ _kf = KeyFormat.PEM;
|
|
|
+ }
|
|
|
+ KeyWorker _privateWorker = new KeyWorker(publicKey, _kf);
|
|
|
+ return _privateWorker.Encrypt(encryptString);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// RSA解密
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="rsaKeyTypes">密钥格式</param>
|
|
|
+ /// <param name="decryptString">需要解密的字符串</param>
|
|
|
+ /// <param name="privateKey">RSA私钥xml字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string DecryptRSA(RSAKeyTypes rsaKeyTypes, string decryptString, string privateKey)
|
|
|
+ {
|
|
|
+ KeyFormat _kf = KeyFormat.XML;
|
|
|
+ if (rsaKeyTypes == RSAKeyTypes.ASNKey)
|
|
|
+ {
|
|
|
+ _kf = KeyFormat.ASN;
|
|
|
+ }
|
|
|
+ else if (rsaKeyTypes == RSAKeyTypes.PEMKey)
|
|
|
+ {
|
|
|
+ _kf = KeyFormat.PEM;
|
|
|
+ }
|
|
|
+ KeyWorker _privateWorker = new KeyWorker(privateKey, _kf);
|
|
|
+ return _privateWorker.Decrypt(decryptString);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// RSA加密
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="encryptString">需要解密的字符串</param>
|
|
|
+ /// <param name="xmlPublicKey">RSA公钥xml字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string EncryptRSA(string encryptString, string xmlPublicKey)
|
|
|
+ {
|
|
|
+ CspParameters RSAParams = new CspParameters();
|
|
|
+ RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
|
|
|
+ RSACryptoServiceProvider _rsacsp = new RSACryptoServiceProvider(1024, RSAParams);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ byte[] rsaBts;
|
|
|
+ string encryptResult = "";
|
|
|
+ byte[] tempBts = new byte[117];
|
|
|
+ byte[] unicodeBts = Encoding.UTF8.GetBytes(encryptString);
|
|
|
+
|
|
|
+ _rsacsp.FromXmlString(xmlPublicKey);
|
|
|
+ double len = unicodeBts.Length;
|
|
|
+ if (len < 117)//加密文本少于117字节
|
|
|
+ {
|
|
|
+ rsaBts = _rsacsp.Encrypt(unicodeBts, false);
|
|
|
+ //encryptResult = Convert.ToBase64String(rsaBts);
|
|
|
+ encryptResult = BitConverter.ToString(rsaBts).Replace("-", string.Empty);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ int blocks = (int)Math.Ceiling(len / 117);
|
|
|
+ rsaBts = new byte[blocks * 128];
|
|
|
+ for (int i = 0; i < blocks; i++)
|
|
|
+ {
|
|
|
+ if (i < blocks - 1)
|
|
|
+ {
|
|
|
+ Array.Copy(unicodeBts, i * 117, tempBts, 0, 117);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ tempBts = new byte[(unicodeBts.Length - i * 117)];
|
|
|
+ Array.Copy(unicodeBts, i * 117, tempBts, 0, (unicodeBts.Length - i * 117));
|
|
|
+ }
|
|
|
+ Array.Copy(_rsacsp.Encrypt(tempBts, false), 0, rsaBts, i * 128, 128);
|
|
|
+ }
|
|
|
+ //encryptResult = Convert.ToBase64String(rsaBts);
|
|
|
+ encryptResult = BitConverter.ToString(rsaBts).Replace("-", string.Empty);
|
|
|
+ }
|
|
|
+
|
|
|
+ return encryptResult;
|
|
|
+ }
|
|
|
+ catch { return encryptString; }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ _rsacsp.Clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// RSA解密
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="decryptString">需要解密的字符串</param>
|
|
|
+ /// <param name="xmlPrivateKey">RSA私钥xml字符串</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static string DecryptRSA(string decryptString, string xmlPrivateKey)
|
|
|
+ {
|
|
|
+ CspParameters RSAParams = new CspParameters();
|
|
|
+ RSAParams.Flags = CspProviderFlags.UseMachineKeyStore;
|
|
|
+ RSACryptoServiceProvider _rsacsp = new RSACryptoServiceProvider(1024, RSAParams);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ string decryptResult = "";
|
|
|
+ byte[] rsaBts = new byte[decryptString.Length / 2];
|
|
|
+ int j = 0;
|
|
|
+ for (int i = 0; i < decryptString.Length / 2; i++)
|
|
|
+ {
|
|
|
+ rsaBts[i] = Byte.Parse(decryptString[j].ToString() + decryptString[j + 1].ToString(), System.Globalization.NumberStyles.HexNumber);
|
|
|
+ j += 2;
|
|
|
+ }
|
|
|
+ _rsacsp.FromXmlString(xmlPrivateKey);
|
|
|
+
|
|
|
+ if (rsaBts.Length < 129)
|
|
|
+ {
|
|
|
+ byte[] unicodeBts = _rsacsp.Decrypt(rsaBts, false);
|
|
|
+ decryptResult = Encoding.UTF8.GetString(unicodeBts);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ byte[] tempBts = new byte[128];
|
|
|
+ int blocks = rsaBts.Length / 128;
|
|
|
+ for (int i = 0; i < blocks; i++)
|
|
|
+ {
|
|
|
+ Array.Copy(rsaBts, i * 128, tempBts, 0, 128);
|
|
|
+ decryptResult += Encoding.UTF8.GetString(_rsacsp.Decrypt(tempBts, false));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return decryptResult;
|
|
|
+ }
|
|
|
+ catch { return decryptString; }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ _rsacsp.Clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 加密信息
|
|
|
+ /// </summary>
|
|
|
+ public static string EncryptData(RSAKeyTypes rsaKeyTypes, string encryptString, string publicKey = "")
|
|
|
+ {
|
|
|
+ if ((rsaKeyTypes == RSAKeyTypes.XMLKey) && (!string.IsNullOrEmpty(publicKey)))
|
|
|
+ {
|
|
|
+ return EncryptRSA(encryptString, publicKey);
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(publicKey))
|
|
|
+ {
|
|
|
+ publicKey = rsaKeyTypes == RSAKeyTypes.ASNKey ? ASN_PUBLIC_KEY : (rsaKeyTypes == RSAKeyTypes.PEMKey ? PEM_PUBLIC_KEY : XML_PUBLIC_KEY);
|
|
|
+ }
|
|
|
+ return EncryptRSA(rsaKeyTypes, encryptString, publicKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 解密信息
|
|
|
+ /// </summary>
|
|
|
+ public static string DecryptData(RSAKeyTypes rsaKeyTypes, string decryptString, string privateKey = "")
|
|
|
+ {
|
|
|
+ if (rsaKeyTypes == RSAKeyTypes.XMLKey)
|
|
|
+ {
|
|
|
+ return DecryptRSA(decryptString, XML_PRIVATE_KEY);
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(privateKey))
|
|
|
+ {
|
|
|
+ privateKey = rsaKeyTypes == RSAKeyTypes.ASNKey ? ASN_PRIVATE_KEY : (rsaKeyTypes == RSAKeyTypes.PEMKey ? PEM_PRIVATE_KEY : XML_PRIVATE_KEY);
|
|
|
+ }
|
|
|
+ return DecryptRSA(rsaKeyTypes, decryptString, privateKey);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 密钥格式
|
|
|
+ /// </summary>
|
|
|
+ public enum RSAKeyTypes
|
|
|
+ {
|
|
|
+ /// <summary>
|
|
|
+ /// ASNKey
|
|
|
+ /// </summary>
|
|
|
+ ASNKey = 1,
|
|
|
+ /// <summary>
|
|
|
+ /// XMLKey
|
|
|
+ /// </summary>
|
|
|
+ XMLKey = 2,
|
|
|
+ /// <summary>
|
|
|
+ /// PEMKey
|
|
|
+ /// </summary>
|
|
|
+ PEMKey = 3
|
|
|
+ }
|
|
|
+}
|