首页 > 代码库 > RSA的密钥把JAVA格式转换成C#的格式(2)

RSA的密钥把JAVA格式转换成C#的格式(2)

把C#格式转换成Java:RSA的密钥把JAVA格式转换成C#的格式(1)

我已经在第一篇介绍过如何把C#格式转换成Java,现在来看看如何把Java格式转换成C#。

 /// <summary>        /// RSA加密        /// </summary>        /// <param name="publickey"></param>        /// <param name="content"></param>        /// <returns></returns>        public string RSAEncrypt(string publickey, string content)        {            publickey = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/M9kJVwi+sO672NBm5fAEbUEl2PqRFuYZVB5ZrKah7eJPP0pKBe/eoA6pOMsjnKFoZGVizz7oCt+TJ+1fpuScn7vAtIFSjn1jgPANE+IcVdk4QKDFKu0LQdiurPQKLpq8Q3wJDAW7FxB2MHw/4ca/9w0N5VL0hQegZ4YhxDss6QIDAQABprivateKey";            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();            byte[] cipherbytes;            rsa.FromXmlString(publickey);            cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);            return Convert.ToBase64String(cipherbytes);        }        /// <summary>        /// RSA解密        /// </summary>        /// <param name="privatekey"></param>        /// <param name="content"></param>        /// <returns></returns>        public string RSADecrypt(string content)        {            //私钥            string NET_PRIVATE_KEY = @"<RSAKeyValue><Modulus>vzPZCVcIvrDuu9jQZuXwBG1BJdj6kRbmGVQeWaymoe3iTz9KSgXv3qAOqTjLI5yhaGRlYs8+6ArfkyftX6bknJ+7wLSBUo59Y4DwDRPiHFXZOECgxSrtC0HYrqz0Ci6avEN8CQwFuxcQdjB8P+HGv/cNDeVS9IUHoGeGIcQ7LOk=</Modulus><Exponent>AQAB</Exponent><P>81Hqu5RXCBOLloURr3lRYzQBrkrFvqvRjsDu7UuWMLgCENjPl4GBkRaPMTAulf/Un6ibZklNc5O9qTJWLnyKhw==</P><Q>ySqkcKbWuobq5I9KSQISq2qCuGKtj/iUFho4PCD1YxhnQ7gcHA4OpS1dRFjtXJYQPTX9be+mmypsCFIyofE5Dw==</Q><DP>8ZnbTBqFOH2/0tuarfP2flx7dsbEBwmwcODd3T16ijGLy5KcvkFXl+sgj6TZ/RMj2syeUThj8Wk3n7Ndo5cCLQ==</DP><DQ>rNwKn46Nt4yVrT7/y0qPtmJ4325TzAIDZhazud9sXPGsUoGTDfx6Uy9vFHsBw+xjGeYZ1mz3EiBa4q+XOJbquw==</DQ><InverseQ>i90WdnZVPxtSTqkkLYbnh4Ro2WhdwRjkfyBxBZZx8hfaM6MfLPi3A0rw9DPOSB4M/BMchtEh3bXuI7bue2tG+A==</InverseQ><D>ol/9qRjorEjF9XEjSr9rHddKxEGIST8RGeF+BNnCiTHkRziQdlykYIO876jzmsKhsG3STB+EZLsXM3ls9RZefcsPF5mLOCSOCow3DikfCtAy4hntsU9JwpuYE0V4A+Sgfd24fatqbu+JxE2nvpSbAPczDOgBFPNfYBkhMiuZ/iE=</D></RSAKeyValue>";            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();            RSA.FromXmlString(NET_PRIVATE_KEY);            //要解密的密文            //string pwv = @"c98qyUw0tWHzYuCpOY2X1pxMdtj5Sd11Xdrct+TS5fflMApfj+a2vXPnRSs6VDzdX+YSVVkV5cgd8/xINEtzBib8G8H/D669SgPNWdqOnIX+Xrebettbx7q9c7wcUYjFqC+6S+W58yDvR3y6S8FkGfbeBmCJcQHyw7Mmt0MSwXU=";            //string pwv1 = @"fhTK2Z8meUzRm2NVEZlbH2Q6NDrIdOpgxdJ25FuqVOwlMo34z2dvsK2bo5a79u6vousPZJ1sEQ3MIJ/dVf7uiHhvBoxX9ortBGOp07gMk1zWynpagSPQwGFB99jZSegKYAQTijkrpw+WYwew+v8kHkK3RaoyrnUGk9waGjAez2g=";            //解密            byte[] pwb1 = Convert.FromBase64String(content);            byte[] cpw1 = RSA.Decrypt(pwb1, false);            string bc = Encoding.UTF8.GetString(cpw1);            return bc;        }        /// <summary>        /// RSA验签        /// </summary>        /// <param name="privatekey"></param>        /// <param name="content"></param>        /// <returns></returns>        public string RSACheckKey(string account, string key)        {            string NET_PRIVATE_KEY = @"<RSAKeyValue><Modulus>vzPZCVcIvrDuu9jQZuXwBG1BJdj6kRbmGVQeWaymoe3iTz9KSgXv3qAOqTjLI5yhaGRlYs8+6ArfkyftX6bknJ+7wLSBUo59Y4DwDRPiHFXZOECgxSrtC0HYrqz0Ci6avEN8CQwFuxcQdjB8P+HGv/cNDeVS9IUHoGeGIcQ7LOk=</Modulus><Exponent>AQAB</Exponent><P>81Hqu5RXCBOLloURr3lRYzQBrkrFvqvRjsDu7UuWMLgCENjPl4GBkRaPMTAulf/Un6ibZklNc5O9qTJWLnyKhw==</P><Q>ySqkcKbWuobq5I9KSQISq2qCuGKtj/iUFho4PCD1YxhnQ7gcHA4OpS1dRFjtXJYQPTX9be+mmypsCFIyofE5Dw==</Q><DP>8ZnbTBqFOH2/0tuarfP2flx7dsbEBwmwcODd3T16ijGLy5KcvkFXl+sgj6TZ/RMj2syeUThj8Wk3n7Ndo5cCLQ==</DP><DQ>rNwKn46Nt4yVrT7/y0qPtmJ4325TzAIDZhazud9sXPGsUoGTDfx6Uy9vFHsBw+xjGeYZ1mz3EiBa4q+XOJbquw==</DQ><InverseQ>i90WdnZVPxtSTqkkLYbnh4Ro2WhdwRjkfyBxBZZx8hfaM6MfLPi3A0rw9DPOSB4M/BMchtEh3bXuI7bue2tG+A==</InverseQ><D>ol/9qRjorEjF9XEjSr9rHddKxEGIST8RGeF+BNnCiTHkRziQdlykYIO876jzmsKhsG3STB+EZLsXM3ls9RZefcsPF5mLOCSOCow3DikfCtAy4hntsU9JwpuYE0V4A+Sgfd24fatqbu+JxE2nvpSbAPczDOgBFPNfYBkhMiuZ/iE=</D></RSAKeyValue>";            //string PUB_KEY_MODULES = @"<RSAKeyValue><Modulus>vzPZCVcIvrDuu9jQZuXwBG1BJdj6kRbmGVQeWaymoe3iTz9KSgXv3qAOqTjLI5yhaGRlYs8+6ArfkyftX6bknJ+7wLSBUo59Y4DwDRPiHFXZOECgxSrtC0HYrqz0Ci6avEN8CQwFuxcQdjB8P+HGv/cNDeVS9IUHoGeGIcQ7LOk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();            key = key.Replace("\\", "");            key = key.Replace(\",  );            key = key.Replace(" ", "");            byte[] e = Encoding.UTF8.GetBytes(account);            byte[] v = Convert.FromBase64String(key);            RSA.FromXmlString(NET_PRIVATE_KEY);            try            {                bool result4 = RSA.VerifyData(e, "MD5", v);                if (result4)                {                    return "1";                }                else                {                    return "验签不通过!";                }            }            catch            {                return "程序出错!";            }        }

 

RSA的密钥把JAVA格式转换成C#的格式(2)