赞
踩
华为快应用将会成为未来5G的重要APP模式,也可能是鸿蒙系统HMS的重要软件提组成。 快应用目前可以兼容手机、平板、汽车等移动载体,不久的未来还会支持手表等更多移动端。 因而现在的程序猿人们有时间可以看起来未来的快应用开发平台了。
言归正传, 华为快应用目前提供了基于RSA Cipher的非对称加密解密API安全接口,可用于与服务端的安全加密数据交互。但是,ASP.Net没有原生的RSA Cipher 组件对应华为的API安全加密解密,因而笔者经过了一周时间的苦心研究,终于找到了解决方案,在此和各位伙伴分享。 希望大家可以在此内容上节约一些时间。
解决方案是使用基于第三方组件 BouncyCastle.Crypto.dll 对RSA Cipher数据进行解密或明文进行加密。
代码如下:
-
- Imports Org.BouncyCastle.Asn1.Pkcs
- Imports Org.BouncyCastle.Asn1.X509
- Imports Org.BouncyCastle.Crypto.Generators
- Imports Org.BouncyCastle.Crypto.Parameters
- Imports Org.BouncyCastle.Math
- Imports Org.BouncyCastle.Pkcs
- Imports Org.BouncyCastle.Security
- Imports Org.BouncyCastle.Crypto.Engines
- Imports Org.BouncyCastle.X509
- Imports Org.BouncyCastle.Crypto
- Imports Org.BouncyCastle.Asn1
- Imports Org.BouncyCastle.Crypto.Encodings
-
-
- Public Class RSACipher
-
- Public Sub New(Optional ByVal _Transformation As String = "")
- If _Transformation <> "" Then Transformation = _Transformation
- End Sub
-
-
- ''' <summary>
- ''' KEY 结构体
- ''' </summary>
- Public Structure RSAKEY
- ''' <summary>
- ''' 公钥
- ''' </summary>
- Public Property PublicKey() As String
- ''' <summary>
- ''' 私钥
- ''' </summary>
- Public Property PrivateKey() As String
- End Structure
- Public Transformation As String = "RSA/None/OAEPWithSHA256AndMGF1Padding"
- Public Function GetKey() As RSAKEY
- 'RSA密钥对的构造器
- Dim keyGenerator As New RsaKeyPairGenerator()
-
- 'RSA密钥构造器的参数
- Dim param As New RsaKeyGenerationParameters(Org.BouncyCastle.Math.BigInteger.ValueOf(3), New Org.BouncyCastle.Security.SecureRandom(), 1024, 25) '密钥长度
- '用参数初始化密钥构造器
- keyGenerator.Init(param)
- '产生密钥对
- Dim keyPair As AsymmetricCipherKeyPair = keyGenerator.GenerateKeyPair()
- '获取公钥和密钥
- Dim publicKey As AsymmetricKeyParameter = keyPair.Public
- Dim privateKey As AsymmetricKeyParameter = keyPair.Private
- Dim subjectPublicKeyInfo As SubjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey)
- Dim privateKeyInfo As PrivateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey)
- Dim asn1ObjectPublic As Asn1Object = subjectPublicKeyInfo.ToAsn1Object()
- Dim publicInfoByte() As Byte = asn1ObjectPublic.GetEncoded("UTF-8")
- Dim asn1ObjectPrivate As Asn1Object = privateKeyInfo.ToAsn1Object()
- Dim privateInfoByte() As Byte = asn1ObjectPrivate.GetEncoded("UTF-8")
- Dim item As New RSAKEY() With {.PublicKey = Convert.ToBase64String(publicInfoByte), .PrivateKey = Convert.ToBase64String(privateInfoByte)}
- Return item
- End Function
- Private Function GetPublicKeyParameter(ByVal s As String) As AsymmetricKeyParameter
- s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
- Dim publicInfoByte() As Byte = Convert.FromBase64String(s)
- Dim pubKeyObj As Asn1Object = Asn1Object.FromByteArray(publicInfoByte) '这里也可以从流中读取,从本地导入
- Dim pubKey As AsymmetricKeyParameter = PublicKeyFactory.CreateKey(publicInfoByte)
- Return pubKey
- End Function
-
- Private Function GetPrivateKeyParameter(ByVal s As String) As AsymmetricKeyParameter
- s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
- Dim privateInfoByte() As Byte = Convert.FromBase64String(s)
- ' Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
- ' PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
- Dim priKey As AsymmetricKeyParameter = PrivateKeyFactory.CreateKey(privateInfoByte)
-
- Return priKey
- End Function
-
- '加密
- Public Function EncryptByPublicKey(ByVal s As String, ByVal key As String) As String
- '非对称加密算法,加解密用
- Dim engine As IBufferedCipher = CipherUtilities.GetCipher(Transformation)
-
-
- '加密
- Try
- engine.Init(True, GetPublicKeyParameter(key))
- Dim byteData() As Byte = System.Text.Encoding.UTF8.GetBytes(s)
- Dim ResultData = engine.DoFinal(byteData)
- Return Convert.ToBase64String(ResultData)
- 'Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
- Catch ex As Exception
- Return ex.Message
-
- End Try
- End Function
-
- '解密
- Public Function DecryptByPrivateKey(ByVal s As String, ByVal key As String) As String
- s = s.Replace(vbCr, "").Replace(vbLf, "").Replace(" ", "")
- '非对称加密算法,加解密用
- 'Dim engine As IAsymmetricBlockCipher = New Pkcs1Encoding(New RsaEngine( ))
- Dim engine As IBufferedCipher = CipherUtilities.GetCipher(Transformation)
- '解密
-
- Try
- engine.Init(False, GetPrivateKeyParameter(key))
- Dim byteData() As Byte = Convert.FromBase64String(s)
- Dim ResultData = engine.DoFinal(byteData)
- Return System.Text.Encoding.UTF8.GetString(ResultData)
-
- Catch ex As Exception
- Return ex.Message
-
- End Try
- End Function
- End Class
这里需要特别主义的是 Public Transformation As String = "RSA/None/OAEPWithSHA256AndMGF1Padding" 必须和华为官方文档中的内容一致,否则无法正确解密。
此类需要使用到的插件: bouncycastle.crypto.dll 可以在以下官网下载,
希望对各位快应用开发者有帮助。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。