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

Jam*_*ard 21 powershell encoding sha1 bytearray utf-8

我要做的是使用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) 
Run Code Online (Sandbox Code Playgroud)

所以,当我去做哈希时,我意识到我必须从字符串中得到一个byte [],我不知道该怎么做.我想有一个简单的方法来自.Net库,但找不到一个例子.

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

$string = "password"
Run Code Online (Sandbox Code Playgroud)

如何将其转换为我可以在:: 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));
Run Code Online (Sandbox Code Playgroud)

当我运行密码字符串为"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)
Run Code Online (Sandbox Code Playgroud)

使用"1"参数调用"FromBase64String"的异常:"Base-64 char数组的长度无效." 在线:1字符:45

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

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

5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

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

W6ph5Mm5Pz8GgiULbPgzG37mj9g =

再说一次,这甚至可能无法做到,但我正试图找到一个可以解决的问题.

Eri*_*son 27

您最有可能只需要在最后一行之后将哈希值转换为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)
Run Code Online (Sandbox Code Playgroud)

文本 - > Bytes->加密/哈希的>的Base64

这是以文本格式发送加密数据的一种非常常见的模式.