关于JAVA和C#加解密通讯的话,可以用这个BouncyCastle插件,会帮助你解决很多问题
//c#使用java给的公钥进行rsa加密 public static byte[] RSAEncrypt(string publickey, string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] publicKeyBytes = Convert.FromBase64String(publickey); AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes); RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); rsa.ImportParameters(rsaParameters); return rsa.Encrypt(Encoding.GetEncoding("gbk").GetBytes(content), false); }
使用方法:
string tempStr=Convert.ToBase64String(RSACoder.RSAEncrypt("公钥", "需要加密码的字符"))
普通C#自身调用加解密:
RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider(); string privatekey = oRSA.ToXmlString(true);//私钥 string publickey = oRSA.ToXmlString(false);//公钥 //这两个密钥需要保存下来 byte[] messagebytes = Encoding.UTF8.GetBytes("luo罗"); //需要加密的数据 //公钥加密 RSACryptoServiceProvider oRSA1 = new RSACryptoServiceProvider(); oRSA1.FromXmlString(publickey); //加密要用到公钥所以导入公钥 byte[] AOutput = oRSA1.Encrypt(messagebytes, false); //AOutput 加密以后的数据 //私钥解密 RSACryptoServiceProvider oRSA2 = new RSACryptoServiceProvider(); oRSA2.FromXmlString(privatekey); byte[] AInput = oRSA2.Decrypt(AOutput, false); string reslut = Encoding.UTF8.GetString(AInput);