请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册
搜索

华为快应用中的 密码算法 在ASP.NET中的实现RSA加密解密

0
回复
598
查看
[复制链接]

1

主题

1

帖子

10

积分

 楼主| 2020-7-30 11:02:47 显示全部楼层 |阅读模式

华为快应用将会成为未来5G的重要APP模式,也可能是鸿蒙系统HMS的重要软件提组成。 快应用目前可以兼容手机、平板、汽车等移动载体,不久的未来还会支持手表等更多移动端。 因而现在的程序猿人们有时间可以看起来未来的快应用开发平台了。

言归正传, 华为快应用目前提供了基于RSA Cipher的非对称加密解密API安全接口,可用于与服务端的安全加密数据交互。但是,ASP.Net没有原生的RSA Cipher 组件对应华为的API安全加密解密,因而笔者经过了一周时间的苦心研究,终于找到了解决方案,在此和各位伙伴分享。 希望大家可以在此内容上节约一些时间。

解决方案是使用基于第三方组件 BouncyCastle.Crypto.dll 对RSA Cipher数据进行解密或明文进行加密。

代码如下:

  1. Imports Org.BouncyCastle.Asn1.Pkcs
  2. Imports Org.BouncyCastle.Asn1.X509
  3. Imports Org.BouncyCastle.Crypto.Generators
  4. Imports Org.BouncyCastle.Crypto.Parameters
  5. Imports Org.BouncyCastle.Math
  6. Imports Org.BouncyCastle.Pkcs
  7. Imports Org.BouncyCastle.Security
  8. Imports Org.BouncyCastle.Crypto.Engines
  9. Imports Org.BouncyCastle.X509
  10. Imports Org.BouncyCastle.Crypto
  11. Imports Org.BouncyCastle.Asn1
  12. Imports Org.BouncyCastle.Crypto.Encodings
  13. Public Class RSACipher
  14. Public Sub New(Optional ByVal _Transformation As String = "")
  15. If _Transformation <> "" Then Transformation = _Transformation
  16. End Sub
  17. ''' <summary>
  18. ''' KEY 结构体
  19. ''' </summary>
  20. Public Structure RSAKEY
  21. ''' <summary>
  22. ''' 公钥
  23. ''' </summary>
  24. Public Property PublicKey() As String
  25. ''' <summary>
  26. ''' 私钥
  27. ''' </summary>
  28. Public Property PrivateKey() As String
  29. End Structure
  30. Public Transformation As String = "RSA/None/OAEPWithSHA256AndMGF1Padding"
  31. Public Function GetKey() As RSAKEY
  32. 'RSA密钥对的构造器
  33. Dim keyGenerator As New RsaKeyPairGenerator()
  34. 'RSA密钥构造器的参数
  35. Dim param As New RsaKeyGenerationParameters(Org.BouncyCastle.Math.BigInteger.ValueOf(3), New Org.BouncyCastle.Security.SecureRandom(), 1024, 25) '密钥长度
  36. '用参数初始化密钥构造器
  37. keyGenerator.Init(param)
  38. '产生密钥对
  39. Dim keyPair As AsymmetricCipherKeyPair = keyGenerator.GenerateKeyPair()
  40. '获取公钥和密钥
  41. Dim publicKey As AsymmetricKeyParameter = keyPair.Public
  42. Dim privateKey As AsymmetricKeyParameter = keyPair.Private
  43. Dim subjectPublicKeyInfo As SubjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey)
  44. Dim privateKeyInfo As PrivateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey)
  45. Dim asn1ObjectPublic As Asn1Object = subjectPublicKeyInfo.ToAsn1Object()
  46. Dim publicInfoByte() As Byte = asn1ObjectPublic.GetEncoded("UTF-8")
  47. Dim asn1ObjectPrivate As Asn1Object = privateKeyInfo.ToAsn1Object()
  48. Dim privateInfoByte() As Byte = asn1ObjectPrivate.GetEncoded("UTF-8")
  49. Dim item As New RSAKEY() With {.PublicKey = Convert.ToBase64String(publicInfoByte), .PrivateKey = Convert.ToBase64String(privateInfoByte)}
  50. Return item
  51. End Function
  52. Private Function GetPublicKeyParameter(ByVal s As String) As AsymmetricKeyParameter
  53. s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
  54. Dim publicInfoByte() As Byte = Convert.FromBase64String(s)
  55. Dim pubKeyObj As Asn1Object = Asn1Object.FromByteArray(publicInfoByte) '这里也可以从流中读取,从本地导入
  56. Dim pubKey As AsymmetricKeyParameter = PublicKeyFactory.CreateKey(publicInfoByte)
  57. Return pubKey
  58. End Function
  59. Private Function GetPrivateKeyParameter(ByVal s As String) As AsymmetricKeyParameter
  60. s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
  61. Dim privateInfoByte() As Byte = Convert.FromBase64String(s)
  62. ' Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
  63. ' PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  64. Dim priKey As AsymmetricKeyParameter = PrivateKeyFactory.CreateKey(privateInfoByte)
  65. Return priKey
  66. End Function
  67. '加密
  68. Public Function EncryptByPublicKey(ByVal s As String, ByVal key As String) As String
  69. '非对称加密算法,加解密用
  70. Dim engine As IBufferedCipher = CipherUtilities.GetCipher(Transformation)
  71. '加密
  72. Try
  73. engine.Init(True, GetPublicKeyParameter(key))
  74. Dim byteData() As Byte = System.Text.Encoding.UTF8.GetBytes(s)
  75. Dim ResultData = engine.DoFinal(byteData)
  76. Return Convert.ToBase64String(ResultData)
  77. 'Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
  78. Catch ex As Exception
  79. Return ex.Message
  80. End Try
  81. End Function
  82. '解密
  83. Public Function DecryptByPrivateKey(ByVal s As String, ByVal key As String) As String
  84. s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
  85. '非对称加密算法,加解密用
  86. 'Dim engine As IAsymmetricBlockCipher = New Pkcs1Encoding(New RsaEngine( ))
  87. Dim engine As IBufferedCipher = CipherUtilities.GetCipher(Transformation)
  88. '解密
  89. Try
  90. engine.Init(False, GetPrivateKeyParameter(key))
  91. Dim byteData() As Byte = Convert.FromBase64String(s)
  92. Dim ResultData = engine.DoFinal(byteData)
  93. Return System.Text.Encoding.UTF8.GetString(ResultData)
  94. Catch ex As Exception
  95. Return ex.Message
  96. End Try
  97. End Function
  98. End Class
复制代码

这里需要特别注意的是 Public Transformation As String = "RSA/None/OAEPWithSHA256AndMGF1Padding" 必须和华为官方文档中的内容一致,否则无法正确解密。

此类需要使用到的插件: bouncycastle.crypto.dll 可以在以下官网下载,

http://www.bouncycastle.org/

希望对各位快应用开发者有帮助。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册