public void FromXmlKeyTest() { string xmlKey = "<RSAKeyValue><Modulus>woDaokr9YMTGnCd3voqEdFQgVYTnc9ZXdjpamdFvsuqHldBOiqBanhJiMlVLFKXoAQPGl50tGpyp5vVI6GmTy//aX80UrHAJ3muOgi+jXpxuqfS8rp9Ld7gmHgvvFXT1bcJsrgMZJgAF6N31rpo1OcTzq1IDgEtIo2ca0UYMh5k=</Modulus><Exponent>AQAB</Exponent><P>/6UsMm9IbQhziMxMkw9GlEJ1mApmmv/vv0WUEzD+lPrOrTfXPGKIx9o9heMJaS6HYwQHYcjJ+4vrNzMUyfOihw==</P><Q>wsX1YB6m5AbBVZim6dyqDZ2X/wYBQMKO1l+ref7/LhOj/S5CdO/363bTKfHeGmuH19QvCJAmTA7mvuV/rvds3w==</Q><DP>6tmkZpjQ4kl3Sd7VIexQnHcJbKlxZUdlykNzKsZBuj9Git7XvkOPAHo/NrZBuPzjp1TWK0C4PXxmk4k7ueM2FQ==</DP><DQ>TRuDhbb8yP8noNRbF/BE3jkKiGp/Kn0bgSDEyPo7Walq3Xtn0dWCZ5RWCp/3FqyFTdtxd0lh7/BiRyaiAslvLw==</DQ><InverseQ>/lCyqC6dzJKUCkUxMMMwn+MVx5rxpUG7WZxWeUkkV1gfgye9QBbdPLvIbcrr8Q7xdmcY58hKFV2C76wB2uCWoA==</InverseQ><D>WDgTcjrrE6IMpzTrhfvTueOSSteeFxcn0lMDVvL8Y70mRBgYF55Fm56g9U1kYXgic5tfztKWa5SVJ1EngWqpgrg8pY/sHwlXDeId8EA1HGfhmSCPjAUSMlWo98GrMLcbGdY63bj9jWnuOIMrkME7HZ9IdszAutNAWleUeTldLeU=</D></RSAKeyValue>"; //string xmlKey = "<RSAKeyValue><Modulus>q+NfMvfOxSbRZU2esnFZ17icIts9Yv1teeFD7sSSKzQ8xaCKVRUbYZngsjisQXcFXpd2Z2FFZ+XFaMMb/NVrmPXkSC+b38DzLGYPpLXVNUWibzv0uO0EN4DrP59xxtx3NTGkUP7ADH9gPnZNQna9Wfar/pLKG+sUDKIdDssy6DE=</Modulus><Exponent>AQAB</Exponent><P>zHClmRF/Gr7W4fDDk+GMqKr68/qu+GoS+uxGDSRmC2sBqgrI/G37nNXsGVFzcNxnm0LxFAJZsAgOU7SM/MVh9w==</P><Q>1z0QoW1/8n27uUYq9TesmitLRE2gXMtY8Fa31LafvDbnm5uZwRg7zNszkqzMXqLYHOHmNGt5k1mAjVFOUAr9Fw==</Q><DP>F3u5CeQgnYneVQW68XmvFpDNUskw4AgPNhN92HSd5CHehxHGFHjttg48mIvqnsQygnsmBg5fDwFd4++RYlep8Q==</DP><DQ>sKigI76aC0+Danfa/lVpx0fNiQwlmMQWBX1HeMFDrdlqk19M/R2Ex4kKdVJ6kxoZQutuZIvpxzTovBSFsFC0lQ==</DQ><InverseQ>kxBQQtQ5wVvB+JOAzOuBjBOPBpNegKi+c970APIrRk7JaPzVhbxL2mvHSzF19WnD2mLzzuqB/knLfbZ0reH6vQ==</InverseQ><D>ObVtudvvBMwCk2Na+4gI5N8rNJys4HY7cicKOriuZmj0WlmK+APNHJigEtXqJ1SBbf+mFhFXTwmanJCc7ebtZ3GHtrs3hc1wafPrjEY6muKJjt+X3B/Gox1Mzz2Kxqpq7kF+EKa/duX/1HOR0U87D26cy7kwgIgk6gmQ843kIS0=</D></RSAKeyValue>"; var helper = RsaKeyHelper.FromXmlKey(xmlKey); string pemPub = helper.ToXmlString(true); Assert.IsNotNull(pemPub); }
public void ToXmlStringTest() { string privateKey = "<RSAKeyValue><Modulus>woDaokr9YMTGnCd3voqEdFQgVYTnc9ZXdjpamdFvsuqHldBOiqBanhJiMlVLFKXoAQPGl50tGpyp5vVI6GmTy//aX80UrHAJ3muOgi+jXpxuqfS8rp9Ld7gmHgvvFXT1bcJsrgMZJgAF6N31rpo1OcTzq1IDgEtIo2ca0UYMh5k=</Modulus><Exponent>AQAB</Exponent><P>/6UsMm9IbQhziMxMkw9GlEJ1mApmmv/vv0WUEzD+lPrOrTfXPGKIx9o9heMJaS6HYwQHYcjJ+4vrNzMUyfOihw==</P><Q>wsX1YB6m5AbBVZim6dyqDZ2X/wYBQMKO1l+ref7/LhOj/S5CdO/363bTKfHeGmuH19QvCJAmTA7mvuV/rvds3w==</Q><DP>6tmkZpjQ4kl3Sd7VIexQnHcJbKlxZUdlykNzKsZBuj9Git7XvkOPAHo/NrZBuPzjp1TWK0C4PXxmk4k7ueM2FQ==</DP><DQ>TRuDhbb8yP8noNRbF/BE3jkKiGp/Kn0bgSDEyPo7Walq3Xtn0dWCZ5RWCp/3FqyFTdtxd0lh7/BiRyaiAslvLw==</DQ><InverseQ>/lCyqC6dzJKUCkUxMMMwn+MVx5rxpUG7WZxWeUkkV1gfgye9QBbdPLvIbcrr8Q7xdmcY58hKFV2C76wB2uCWoA==</InverseQ><D>WDgTcjrrE6IMpzTrhfvTueOSSteeFxcn0lMDVvL8Y70mRBgYF55Fm56g9U1kYXgic5tfztKWa5SVJ1EngWqpgrg8pY/sHwlXDeId8EA1HGfhmSCPjAUSMlWo98GrMLcbGdY63bj9jWnuOIMrkME7HZ9IdszAutNAWleUeTldLeU=</D></RSAKeyValue>"; string publicKey = "<RSAKeyValue><Modulus>woDaokr9YMTGnCd3voqEdFQgVYTnc9ZXdjpamdFvsuqHldBOiqBanhJiMlVLFKXoAQPGl50tGpyp5vVI6GmTy//aX80UrHAJ3muOgi+jXpxuqfS8rp9Ld7gmHgvvFXT1bcJsrgMZJgAF6N31rpo1OcTzq1IDgEtIo2ca0UYMh5k=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"; var helper = RsaKeyHelper.FromXmlKey(privateKey); string xmlPvt = helper.ToXmlString(true); string xmlPub = helper.ToXmlString(false); Assert.AreEqual(privateKey, helper.ToXmlString(true)); Assert.AreEqual(publicKey, xmlPub); }
/// <summary> /// 转换密钥格式 /// </summary> /// <param name="keyString"></param> /// <param name="isPrivate"></param> /// <param name="toFormat"></param> /// <returns></returns> private static string TransformKeyFormat(string keyString, bool isPrivate, string toFormat) { string fromFormat = DetectKeyStringFormat(keyString, isPrivate); if (fromFormat == toFormat) { return(keyString); } RsaKeyHelper helper = null; if (fromFormat == "xml") { helper = RsaKeyHelper.FromXmlKey(keyString); } else if (fromFormat == "pkcs1") { keyString = isPrivate ? RsaKeyHelper.FormatPrivateKey(keyString, RsaKeyHelper.KeyFormat.pkcs1) : RsaKeyHelper.FormatPublicKey(keyString); helper = RsaKeyHelper.FromPemKeyString(keyString); } else if (fromFormat == "pkcs8") { keyString = isPrivate ? RsaKeyHelper.FormatPrivateKey(keyString, RsaKeyHelper.KeyFormat.pkcs8) : RsaKeyHelper.FormatPublicKey(keyString); helper = RsaKeyHelper.FromPemKeyString(keyString); } if (helper == null) { return(keyString); } string res = keyString; if (toFormat == "xml") { res = helper.ToXmlString(isPrivate); } else if (toFormat == "pkcs1") { helper.Format = RsaKeyHelper.KeyFormat.pkcs1; res = helper.GetKeyString(isPrivate); } else if (toFormat == "pkcs8") { helper.Format = RsaKeyHelper.KeyFormat.pkcs8; res = helper.GetKeyString(isPrivate); } return(res); }