在 PowerShell 版本 2 中将字符串转换为字节数组

2024-02-25

我想做的是使用 SHA1 UTF-8 加密,然后使用 base64 编码和密码字符串值。然而,我需要先进行加密,然后再进行编码,但我却反过来做了。

这是代码:

# Create Input Data 
$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash" 
$data1 = $enc.GetBytes($string1) 

# Create a New SHA1 Crypto Provider 
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 

$# Now hash and display results 
$result1 = $sha.ComputeHash($data1) 

因此,当我进行哈希处理时,我意识到我必须从字符串中获取一个 byte[],但我不知道该怎么做。我认为 .Net 库中有一种简单的方法,但找不到示例。

所以如果我有一个字符串,例如:

$string = "password"

如何将其转换为可以在 :: ComputeHash($string) 上使用的字节数组?

所以我最终必须得到一个加密的 SHA-1 和 Base 64 编码的 UTF-8 密码,上面的代码就是这样做的,但它返回的结果与我在 java 中编码相同的东西时不同,我首先对其进行了加密,然后将该结果转换为 Base 64 编码。

我假设虽然 api 不支持直接加密字符串,但可能有一种解决方法可以让您执行此操作。这就是我正在尝试做的事情。

所以我假设我的代码问题是我必须先对其进行加密,然后对其进行编码以获得正确的值。正确还是我在这里遗漏了一些东西?

这是有效的相关java代码:

//First method call uses a swing component to get the user entered password.
String password = getPassword(); 

//This line is where the work starts with the second and third methods below.
String hashed = byteToBase64(getHash(password));

//The second method call here gets the encryption.
public static byte[] getHash(String password) {
      MessageDigest digest = null;
      byte[] input = null;
      try {
             digest = MessageDigest.getInstance("SHA-1");
      } catch (NoSuchAlgorithmException e1) {
             e1.printStackTrace();
      }
      digest.reset();
      try {
             input = digest.digest(password.getBytes("UTF-8"));
      } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
      }
      return input;
}

//Then the third method call here gets the encoding, FROM THE ENCRYPTED STRING.
public static String byteToBase64(byte[] data){
    return new String(Base64.encodeBase64(data));

当我使用密码字符串“password”运行java代码时,我得到

[91、-86、97、-28、-55、-71、63、63、6、-126、37、11、108、-8、51、27、126、-26、-113、-40] 这就是加密。

然后当我在java中编码时我得到这个: W6ph5Mm5Pz8GgiULbPgzG37mj9g=

但是当我在 PowerShell 中运行它时,我得到了这个,因为它首先被编码为 UTF8:

91 170 97 228 201 185 63 63 6 130 37 11 108 248 51 27 126 230 143 216

然后,当我运行这行代码进行转换时,出现错误:

$base64 = [System.Convert]::FromBase64String($result)

使用“1”个参数调用“FromBase64String”时出现异常:“Base-64 字符数组的长度无效。” 行数:1 字符:45

但是,如果我运行新的代码行以使其从下面变为十六进制,我会得到:

$hexResult = [String]::Join("", ($result | % { "{0:X2}" -f $_}))
PS C:\Program Files (x86)\PowerGUI> Write-Host $hexResult

5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

但我需要最终得到这个值:

W6ph5Mm5Pz8GgiULbPgzG37mj9g=

同样,这甚至可能无法做到,但我正在尝试找到一种解决方法。


您很可能只需要在最后一行之后将哈希值转换为 base64 即可。

$enc = [system.Text.Encoding]::UTF8
$string1 = "This is a string to hash" 
$data1 = $enc.GetBytes($string1) 

# Create a New SHA1 Crypto Provider 
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider 

# Now hash and display results 
$result1 = $sha.ComputeHash($data1)
[System.Convert]::ToBase64String($result1)

文本-​​>字节->加密/哈希->Base64

那是一个very以文本格式发送加密数据的常见模式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 PowerShell 版本 2 中将字符串转换为字节数组 的相关文章

随机推荐