string publicRsaKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6UCsvO/M/Q51ydlxvuvwy8W9yXkAWLLiG5eA2Duha9cr/XoWrQcETf7PRMNYaQs616PGhj0tnPfxfDltmeqTJQf8rHNxevIZ9nQ0N0/8PIlVszubpMyAHnEis2OLMnLENGyepAlabFGDMDPzRpf0ibL0Q/R15BuTzII5gunzAbwIDAQAB";标签:Convert,JAVA,string,buffer,RSA,value,加密 From: https://www.cnblogs.com/SmallChen/p/17566255.html
string RsaEncrypt(string value, bool usePkcs8 = false) { if (string.IsNullOrEmpty(value)) return value; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); rsa.FromXmlString(RSAPublicKeyJava2DotNet(publicRsaKey));//将公钥导入到RSA对象中,准备加密; var buffer = Encoding.UTF8.GetBytes(value); buffer = rsa.Encrypt(buffer, false); return Convert.ToBase64String(buffer); } /***************以下是java提供的公私钥转成.net公私钥XML的方式*************************************/ /// <summary> /// RSA公钥格式转换,java->.net /// </summary> /// <param name="publicKey">java生成的公钥</param> /// <returns></returns> string RSAPublicKeyJava2DotNet(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); }