public string GenerateSignature(string compactJson, string method, string nonceStr, string privateKey, string requestUrl, string signType, string timestamp, string environment) { string signedData = ""; try { //GenerateSignatureResult result = new GenerateSignatureResult(); string plainText = ""; if (compactJson != "") { string encodedData = Encode.Base64Encode(compactJson); plainText = String.Format("data={0}&method={1}&nonceStr={2}&requestUrl={3}&signType={4}×tamp={5}", encodedData, method, nonceStr, requestUrl, signType, timestamp); } else { plainText = String.Format("method={0}&nonceStr={1}&requestUrl={2}&signType={3}×tamp={4}", method, nonceStr, requestUrl, signType, timestamp); } byte[] plainTextByte = Encoding.UTF8.GetBytes(plainText); RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(privateKey); string prikey = provider.ToXmlString(true); byte[] signedBytes = provider.SignData(plainTextByte, CryptoConfig.MapNameToOID("SHA256")); signedData = Convert.ToBase64String(signedBytes); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(signedData); }
static void Main(string[] args) { RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_pub.pem"); AesCryptoServiceProvider aes = new AesCryptoServiceProvider(); string originFile = @"C:\Users\nmose\Desktop\rsa\info.txt"; string encryFile = @"C:\Users\nmose\Desktop\rsa\info-crypto.bin"; string decrypFile = @"C:\Users\nmose\Desktop\rsa\info-decrypto.txt"; string msg = ""; CryptoFile.GenerateEncryFile(provider, aes, originFile, encryFile, ref msg); RSACryptoServiceProvider providerPrivate = PemKeyUtils.GetRSAProviderFromPemFile(@"C:\Users\nmose\Desktop\rsa\rsa_2048_priv.pem"); CryptoFile.DecrypFile(providerPrivate, encryFile, decrypFile, ref msg); }
public static byte[] RSAEncryptBytes(byte[] dataToEncrypt, string publicKeyPath) { RSACryptoServiceProvider provider = PemKeyUtils.GetRSAProviderFromPemFile(publicKeyPath); return(provider.Encrypt(dataToEncrypt, false)); }