Java代码
1 public static void main(String[] args) throws Exception { 2 String sign=sign("test",PRIVATEKEY,"UTF-8"); 3 System.out.println(sign); 4 } 5 6 /** 7 * RSA签名 8 * @param content 待签名数据 9 * @param privateKey 支付前置私钥 10 * @param input_charset 编码格式 11 * @return 签名值 12 */ 13 public static String sign(String content, String privateKey, String input_charset) 14 { 15 try 16 { 17 PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec( Base64.decode(privateKey) ); 18 KeyFactory keyf = KeyFactory.getInstance("RSA"); 19 PrivateKey priKey = keyf.generatePrivate(priPKCS8); 20 21 java.security.Signature signature = java.security.Signature 22 .getInstance("SHA1WithRSA"); 23 24 signature.initSign(priKey); 25 signature.update( content.getBytes(input_charset) ); 26 27 byte[] signed = signature.sign(); 28 29 return Base64.encode(signed); 30 } 31 catch (Exception e) 32 { 33 e.printStackTrace(); 34 } 35 36 return null; 37 }View Code
C# 代码
public partial class RSAHelper { #region 私钥加密 /// <summary> /// 基于BouncyCastle的RSA私钥加密 /// </summary> /// <param name="privateKeyJava"></param> /// <param name="data"></param> /// <returns></returns> public static string EncryptPrivateKeyJava(string privateKeyJava, string data, string encoding = "UTF-8") { RsaKeyParameters privateKeyParam = (RsaKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKeyJava)); byte[] cipherbytes = Encoding.GetEncoding(encoding).GetBytes(data); RsaEngine rsa = new RsaEngine(); rsa.Init(true, privateKeyParam);//参数true表示加密/false表示解密。 cipherbytes = rsa.ProcessBlock(cipherbytes, 0, cipherbytes.Length); return Convert.ToBase64String(cipherbytes); } #endregion #region 公钥解密 /// <summary> /// 基于BouncyCastle的RSA公钥解密 /// </summary> /// <param name="publicKeyJava"></param> /// <param name="data"></param> /// <param name="encoding"></param> /// <returns></returns> public static string DecryptPublicKeyJava(string publicKeyJava, string data, string encoding = "UTF-8") { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKeyJava)); byte[] cipherbytes = Convert.FromBase64String(data); RsaEngine rsa = new RsaEngine(); rsa.Init(false, publicKeyParam);//参数true表示加密/false表示解密。 cipherbytes = rsa.ProcessBlock(cipherbytes, 0, cipherbytes.Length); return Encoding.GetEncoding(encoding).GetString(cipherbytes); } #endregion #region 加签 /// <summary> /// 基于BouncyCastle的RSA签名 /// </summary> /// <param name="data"></param> /// <param name="privateKeyJava"></param> /// <param name="hashAlgorithm">JAVA的和.NET的不一样,如:MD5(.NET)等同于MD5withRSA(JAVA)</param> /// <param name="encoding"></param> /// <returns></returns> public static string RSASignJavaBouncyCastle(string data, string privateKeyJava, string hashAlgorithm = "SHA1WITHRSA", string encoding = "UTF-8") { RsaKeyParameters privateKeyParam = (RsaKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKeyJava)); ISigner signer = SignerUtilities.GetSigner(hashAlgorithm); signer.Init(true, privateKeyParam);//参数为true验签,参数为false加签 var dataByte = Encoding.GetEncoding(encoding).GetBytes(data); signer.BlockUpdate(dataByte, 0, dataByte.Length); //return Encoding.GetEncoding(encoding).GetString(signer.GenerateSignature()); //签名结果 非Base64String return Convert.ToBase64String(signer.GenerateSignature()); } #endregion #region 验签 /// <summary> /// 基于BouncyCastle的RSA签名 /// </summary> /// <param name="data">源数据</param> /// <param name="publicKeyJava"></param> /// <param name="signature">base64签名</param> /// <param name="hashAlgorithm">JAVA的和.NET的不一样,如:MD5(.NET)等同于MD5withRSA(JAVA)</param> /// <param name="encoding"></param> /// <returns></returns> public static bool VerifyJavaBouncyCastle(string data, string publicKeyJava, string signature, string hashAlgorithm = "SHA1WITHRSA", string encoding = "UTF-8") { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKeyJava)); ISigner signer = SignerUtilities.GetSigner(hashAlgorithm); signer.Init(false, publicKeyParam); byte[] dataByte = Encoding.GetEncoding(encoding).GetBytes(data); signer.BlockUpdate(dataByte, 0, dataByte.Length); //byte[] signatureByte = Encoding.GetEncoding(encoding).GetBytes(signature);// 非Base64String byte[] signatureByte = Convert.FromBase64String(signature); return signer.VerifySignature(signatureByte); } #endregion
标签:Java,cipherbytes,encoding,C#,RsaKeyParameters,RSA,signer,return,string From: https://www.cnblogs.com/5tomorrow/p/17127008.html