将 Salt 值表示为字符串

发布于 2024-10-27 02:38:16 字数 476 浏览 6 评论 0原文

我正在使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凉世弥音 2024-11-03 02:38:16

最简单的方法是使用 Convert.ToBase64String 将字节数组转换为字符串的方法和 Convert.FromBase64String 反之亦然,例如,

var salt = new byte[] { 1, 2, 4, 8, 16, 32, 64, 128, 255 };
var s = Convert.ToBase64String(salt);
var b = Convert.FromBase64String(s);

在此示例中,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 and Convert.FromBase64String for vise versa, e.g.

var salt = new byte[] { 1, 2, 4, 8, 16, 32, 64, 128, 255 };
var s = Convert.ToBase64String(salt);
var b = Convert.FromBase64String(s);

in this example s variable holds value AQIECBAgQID/, 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 the salt array are differs. This is because BASE64 is used here (it has 64 symbol alphabet, although byte 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#?.

桃酥萝莉 2024-11-03 02:38:16

也许System.Convert.ToBase64String是一个可能的选择。 System.Convert.FromBase64String 将编码后的字符串转换回来。

Maybe System.Convert.ToBase64String is a possible option. System.Convert.FromBase64String converts the encoded string back.

小嗲 2024-11-03 02:38:16

可能会有所帮助:

public static string CreateRandomPassword(int PasswordLength)
{
  String _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789";
  Byte[] randomBytes = new Byte[PasswordLength];
  RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  rng.GetBytes(randomBytes);
  char[] chars = new char[PasswordLength];
  int allowedCharCount = _allowedChars.Length;

  for(int i = 0;i<PasswordLength;i++)
  {
    chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
  }

  return new string(chars);
}

This may help:

public static string CreateRandomPassword(int PasswordLength)
{
  String _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ23456789";
  Byte[] randomBytes = new Byte[PasswordLength];
  RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  rng.GetBytes(randomBytes);
  char[] chars = new char[PasswordLength];
  int allowedCharCount = _allowedChars.Length;

  for(int i = 0;i<PasswordLength;i++)
  {
    chars[i] = _allowedChars[(int)randomBytes[i] % allowedCharCount];
  }

  return new string(chars);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文