/// <summary>Extracts the binary data from a PEM file.</summary> internal static byte[] GetDERFromPEM(string sPEM) { UInt32 dwSkip, dwFlags; UInt32 dwBinarySize = 0; if (!RSACryptoServiceProviderExtension.CryptStringToBinary(sPEM, (UInt32)sPEM.Length, CRYPT_STRING_FLAGS.CRYPT_STRING_BASE64HEADER, null, ref dwBinarySize, out dwSkip, out dwFlags)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } byte[] decodedData = new byte[dwBinarySize]; if (!RSACryptoServiceProviderExtension.CryptStringToBinary(sPEM, (UInt32)sPEM.Length, CRYPT_STRING_FLAGS.CRYPT_STRING_BASE64HEADER, decodedData, ref dwBinarySize, out dwSkip, out dwFlags)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(decodedData); }
public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, string signType) { try { if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } if ("RSA2".Equals(signType)) { string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign)); return(bVerifyResultOriginal); } else { bool bVerifyResultOriginal = false; try { string sPublicKeyPEM = publicKeyPem;// File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign)); } catch (Exception ex) { } return(bVerifyResultOriginal); } } catch { return(false); } }
public static string RSAEncrypt(string content, string publicKeyPem, string charset) { try { string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider { PersistKeyInCsp = false }; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } byte[] data = Encoding.GetEncoding(charset).GetBytes(content); int maxBlockSize = rsa.KeySize / 8 - 11; //加密块最大长度限制 if (data.Length <= maxBlockSize) { byte[] cipherbytes = rsa.Encrypt(data, false); return(Convert.ToBase64String(cipherbytes)); } MemoryStream plaiStream = new MemoryStream(data); MemoryStream crypStream = new MemoryStream(); Byte[] buffer = new Byte[maxBlockSize]; int blockSize = plaiStream.Read(buffer, 0, maxBlockSize); while (blockSize > 0) { Byte[] toEncrypt = new Byte[blockSize]; Array.Copy(buffer, 0, toEncrypt, 0, blockSize); Byte[] cryptograph = rsa.Encrypt(toEncrypt, false); crypStream.Write(cryptograph, 0, cryptograph.Length); blockSize = plaiStream.Read(buffer, 0, maxBlockSize); } return(Convert.ToBase64String(crypStream.ToArray(), Base64FormattingOptions.None)); } catch (Exception ex) { throw new AopException("EncryptContent = " + content + ",charset = " + charset, ex); } }
/// <summary>Converts DER binary format to a CAPI CERT_PUBLIC_KEY_INFO structure containing an RSA key.</summary> internal static byte[] GetRSAFromDER(byte[] DERData) { byte[] data = null; byte[] publicKey = null; CERT_PUBLIC_KEY_INFO info; UInt32 dwCertPublicKeyInfoSize = 0; IntPtr pCertPublicKeyInfo = IntPtr.Zero; if (RSACryptoServiceProviderExtension.CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.X509_PUBLIC_KEY_INFO), DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyInfoSize)) { data = new byte[dwCertPublicKeyInfoSize]; if (RSACryptoServiceProviderExtension.CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.X509_PUBLIC_KEY_INFO), DERData, (UInt32)DERData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyInfoSize)) { GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned); try { info = (CERT_PUBLIC_KEY_INFO)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(CERT_PUBLIC_KEY_INFO)); publicKey = new byte[info.PublicKey.cbData]; Marshal.Copy(info.PublicKey.pbData, publicKey, 0, publicKey.Length); } finally { handle.Free(); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(publicKey); }
/// <summary>Returns a public key blob from an RSA public key.</summary> internal static byte[] GetPublicKeyBlobFromRSA(byte[] RSAData) { byte[] data = null; UInt32 dwCertPublicKeyBlobSize = 0; if (RSACryptoServiceProviderExtension.CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.RSA_CSP_PUBLICKEYBLOB), RSAData, (UInt32)RSAData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyBlobSize)) { data = new byte[dwCertPublicKeyBlobSize]; if (!RSACryptoServiceProviderExtension.CryptDecodeObject(CRYPT_ENCODING_FLAGS.X509_ASN_ENCODING | CRYPT_ENCODING_FLAGS.PKCS_7_ASN_ENCODING, new IntPtr((int)CRYPT_OUTPUT_TYPES.RSA_CSP_PUBLICKEYBLOB), RSAData, (UInt32)RSAData.Length, CRYPT_DECODE_FLAGS.NONE, data, ref dwCertPublicKeyBlobSize)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(data); }
public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset) { try { //string sPublicKeyPEM = File.ReadAllText(publicKeyPem); publicKeyPem = string.Format("-----BEGIN PUBLIC KEY-----{0}-----END PUBLIC KEY-----", publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; //RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, publicKeyPem); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign)); return(bVerifyResultOriginal); } catch { return(false); } }
public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, string signType, bool keyFromFile) { try { if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } string sPEM; if (keyFromFile) { sPEM = File.ReadAllText(publicKeyPem); } else { sPEM = "-----BEGIN PUBLIC KEY-----\r\n"; sPEM += publicKeyPem; sPEM += "-----END PUBLIC KEY-----\r\n\r\n"; } if ("RSA2".Equals(signType)) { RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(); rSACryptoServiceProvider.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rSACryptoServiceProvider, sPEM); return(rSACryptoServiceProvider.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), "SHA256", Convert.FromBase64String(sign))); } RSACryptoServiceProvider rSACryptoServiceProvider2 = new RSACryptoServiceProvider(); rSACryptoServiceProvider2.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rSACryptoServiceProvider2, sPEM); SHA1CryptoServiceProvider halg = new SHA1CryptoServiceProvider(); return(rSACryptoServiceProvider2.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), halg, Convert.FromBase64String(sign))); } catch { return(false); } }
public static string RSAEncrypt(string content, string publicKeyPem, string charset) { try { string sPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rSACryptoServiceProvider = new RSACryptoServiceProvider(); rSACryptoServiceProvider.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rSACryptoServiceProvider, sPEM); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } byte[] bytes = Encoding.GetEncoding(charset).GetBytes(content); int num = rSACryptoServiceProvider.KeySize / 8 - 11; if (bytes.Length <= num) { byte[] inArray = rSACryptoServiceProvider.Encrypt(bytes, fOAEP: false); return(Convert.ToBase64String(inArray)); } MemoryStream memoryStream = new MemoryStream(bytes); MemoryStream memoryStream2 = new MemoryStream(); byte[] array = new byte[num]; for (int num2 = memoryStream.Read(array, 0, num); num2 > 0; num2 = memoryStream.Read(array, 0, num)) { byte[] array2 = new byte[num2]; Array.Copy(array, 0, array2, 0, num2); byte[] array3 = rSACryptoServiceProvider.Encrypt(array2, fOAEP: false); memoryStream2.Write(array3, 0, array3.Length); } return(Convert.ToBase64String(memoryStream2.ToArray(), Base64FormattingOptions.None)); } catch (Exception innerException) { throw new AopException("EncryptContent = " + content + ",charset = " + charset, innerException); } }
public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset) { try { // === AlexModified for hide KeyFile ================ //string sPublicKeyPEM = File.ReadAllText(publicKeyPem); string sPublicKeyPEM; string fileName = Path.GetFileNameWithoutExtension(publicKeyPem); if (fileName == "alipay_rsa_public_key") { sPublicKeyPEM = Encoding.UTF8.GetString(global::VoucherExpense.Properties.Resources.alipay_rsa_public_key); } else if (fileName == "rsa_public_key") { sPublicKeyPEM = Encoding.ASCII.GetString(global::VoucherExpense.Properties.Resources.rsa_public_key); } else { sPublicKeyPEM = File.ReadAllText(publicKeyPem); } // ================================================== RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); if (string.IsNullOrEmpty(charset)) { charset = DEFAULT_CHARSET; } bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign)); return(bVerifyResultOriginal); } catch { return(false); } }
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a PEM public key string.</summary> public static void LoadPublicKeyPEM(RSACryptoServiceProvider provider, string sPEM) { byte[] DERData = RSACryptoServiceProviderExtension.GetDERFromPEM(sPEM); RSACryptoServiceProviderExtension.LoadPublicKeyDER(provider, DERData); }
/// <summary>Extension method which initializes an RSACryptoServiceProvider from a DER public key blob.</summary> public static void LoadPublicKeyDER(RSACryptoServiceProvider provider, byte[] DERData) { byte[] RSAData = RSACryptoServiceProviderExtension.GetRSAFromDER(DERData); byte[] publicKeyBlob = RSACryptoServiceProviderExtension.GetPublicKeyBlobFromRSA(RSAData); provider.ImportCspBlob(publicKeyBlob); }