【发布时间】:2012-06-28 21:54:42
【问题描述】:
一直在尝试将 xml 文件加密为字符串,以便我可以通过服务传输它。使用编译到代码中的对称密钥从服务器到服务器进行传输。
我一直在使用来自MSDN 的 AES 样本,然后将字节数组与字符串进行转换,如下所示:
' Encrypt the string to an array of bytes.
Dim encrypted As Byte() = crypto.EncryptString(original, _key, _iv)
Dim encrypStr As String = Encoding.Unicode.GetString(encrypted)
'''' >>> Transmit...
Dim postTrans As Byte() = Encoding.Unicode.GetBytes(encrypStr)
' Decrypt the bytes to a string.
Dim roundtrip As String = crypto.DecryptString(postTrans, _key, _iv)
如果没有中间两行,加密/解密工作正常,包括中间两行我要么收到格式错误的无法解析的 xml 文档,要么收到“填充无效且无法删除”错误。
这不是字符串加密的好方法吗?它可以完美地工作,无需将 byte() 转换为字符串广告。
【问题讨论】:
-
不要使用 Encoding.GetString(),不会一直有效。使用 ToBase64()
-
@HenkHolterman 完美!今晚让我很头疼,感谢您的解决方案。
-
@HenkHolterman 您可能想将其发布为答案...
标签: .net xml vb.net encryption aes