public static bool RSACheckContent(string signContent, string sign, string publicKeyPem, string charset, bool keyFromFile) { try { string sPublicKeyPEM; if (keyFromFile) { sPublicKeyPEM = File.ReadAllText(publicKeyPem); } else { sPublicKeyPEM = "-----BEGIN PUBLIC KEY-----\r\n"; sPublicKeyPEM = sPublicKeyPEM + publicKeyPem; sPublicKeyPEM = sPublicKeyPEM + "-----END PUBLIC KEY-----\r\n\r\n"; } 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 (Exception ex) { string s = ex.Message.ToString(); return(false); } }
public static string RSAEncrypt(string content, string publicKeyPem, string charset) { try { string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.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); } }
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 { string sPublicKeyPEM = File.ReadAllText(publicKeyPem); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.PersistKeyInCsp = false; RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); bool bVerifyResultOriginal = rsa.VerifyData(Encoding.GetEncoding(charset).GetBytes(signContent), sha1, Convert.FromBase64String(sign)); return(bVerifyResultOriginal); } } catch { return(false); } }