一点PHP博客分享关于PHP在RSA加密中遇到的一些问题,博主在最近一次接入第三方接口时碰到需要做RSA解密,而开发商使用的是.net语言在.net中他们往往习惯使用XML格式的秘钥,但是在PHP中使用解密的函数使用的是PEM格式秘钥,那只有将xml转成pem格式。最后博主查阅了PHP手册以及文档资料并没有找到相关信息,为了节约成本,无奈之下只能选择使用C#解决这个问题,反正语言也只是工具能解决问题就行。
使用C#代码将xml格式秘钥转PEM格式:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using Org.BouncyCastle; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Math; using Org.BouncyCastle.Crypto.Parameters; var rsa = new RSACryptoServiceProvider(); using(var sr = new StreamReader("e:\\key.xml")) { rsa.FromXmlString(sr.ReadToEnd()); } var p = rsa.ExportParameters(true); var key = new RsaPrivateCrtKeyParameters( new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent), new BigInteger(1, p.D), new BigInteger(1, p.P), new BigInteger(1, p.Q), new BigInteger(1, p.DP), new BigInteger(1, p.DQ), new BigInteger(1, p.InverseQ)); using (var sw = new StreamWriter("e:\\key.pem")) { var pemWriter = new Org.BouncyCastle.OpenSsl.PemWriter(sw); pemWriter.WriteObject(key); } |
需要注意的是使用上面代码需要引入BouncyCastle.Crypto.dll扩展,本文会将扩展放入附件中提供下载(回复可见)。
一点php,一点技术分享。
2019年3月27日 下午2:06 沙发
不错不错,来看看。。
2019年3月27日 下午2:08 1层
@妙文屋 互相学习
2019年4月3日 上午12:16 板凳
虽然不知道说的是什么,但看起来好厉害的样子!
2019年4月3日 上午10:53 1层
@你的灵兽看起来很好吃 …
2019年6月21日 下午5:02 地板
不错
2019年6月21日 下午5:03 1层
@肖俊
2019年8月5日 下午10:18 4楼
学习学习
2020年3月17日 下午12:12 5楼
不知道如何实施
2020年3月24日 上午9:03 1层
@1 需要会一点C#的基础就可以了
2020年7月20日 下午2:03 6楼
学习学习
2021年5月27日 上午10:12 7楼
很需要,谢谢
2021年11月16日 上午11:09 8楼
看啊看
2022年1月11日 下午5:35 9楼
很不错
2022年1月11日 下午5:36 10楼
很不错哦
2022年3月23日 下午5:11 11楼
看起来好厉害的样子,虽然看不懂