将 Salt 值表示为字符串
我正在使用 .NET 生成加密消息。作为此过程的一部分,我生成一个盐值,该盐值用于加密过程,并传输给加密消息的接收者,以便他们可以解密该消息。我想将盐作为文本字符串传输,但是当我尝试将盐字节数组转换为字符串时,每个字节都用“破折号”分隔。例如..
45-A1-99-0C-C0-0C-C2-C2
这是生成盐的方法..
Public Shared Function GenerateSalt() As String
Dim rng As New RNGCryptoServiceProvider()
Dim buffer As Byte() = New Byte(7) {}
rng.GetBytes(buffer)
Return BitConverter.ToString(buffer)
End Function
我的问题是这样的。如何将盐表示为接收者可以在解密过程中使用的字符串?
I am generating an encrypted message using .NET. As part of this process I am generating a salt value that is used in the encryption process and also transmitted to the recipient of the encrypted message so that they may decrypt the message. I want to transmit the salt as a text string but when I try to convert the salt byte array to a String I get each byte separated by a "dash". For example ..
45-A1-99-0C-C0-0C-C2-C2
Here is the method that generates the salt ..
Public Shared Function GenerateSalt() As String
Dim rng As New RNGCryptoServiceProvider()
Dim buffer As Byte() = New Byte(7) {}
rng.GetBytes(buffer)
Return BitConverter.ToString(buffer)
End Function
My question is this. How do I represent the salt as a string that the recipient can use in their decryption process?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的方法是使用
Convert.ToBase64String
将字节数组转换为字符串的方法和Convert.FromBase64String
反之亦然,例如,在此示例中,
s
变量保存值AQIECBAgQID/
,b
保存字节array{ 1, 2, 4, 8, 16, 32, 64, 128, 255 }
,所以什么也没有丢失。是的,
s
字符串的长度和salt
数组的 length 不同。这是因为这里使用了BASE64
(它有 64 个符号字母表,尽管byte
可以容纳 256 个不同的值)。但是,如果您想将字节数组转换为十六进制字符串,反之亦然,您可能会发现这个问题很有帮助: 如何在 C# 中将字节数组转换为十六进制字符串,反之亦然?。
The easiest way is to use
Convert.ToBase64String
method for converting a byte array to string andConvert.FromBase64String
for vise versa, e.g.in this example
s
variable holds valueAQIECBAgQID/
,b
holds byte array{ 1, 2, 4, 8, 16, 32, 64, 128, 255 }
, so nothing was lost.Yes, the length of the
s
string and the length of thesalt
array are differs. This is becauseBASE64
is used here (it has 64 symbol alphabet, althoughbyte
may hold 256 different values).But if you want to convert your byte array to a hexadecimal string and vise versa you could find this question helpful: How do you convert Byte Array to Hexadecimal String, and vice versa, in C#?.
也许
System.Convert.ToBase64String
是一个可能的选择。System.Convert.FromBase64String
将编码后的字符串转换回来。Maybe
System.Convert.ToBase64String
is a possible option.System.Convert.FromBase64String
converts the encoded string back.这可能会有所帮助:
This may help: