在PowerShell版本2中将字符串转换为字节数组 [英] Convert a string to a byte array in PowerShell version 2

查看:261
本文介绍了在PowerShell版本2中将字符串转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是使用SHA1 UTF-8加密,然后使用base64编码和密码字符串值。然而,我需要先进行加密,然后再进行编码,然后再进行编码。但是我以其他方式做了。

What I'm trying to do is use SHA1 UTF-8 encryption and then base64 encoding and on a password string value. However, I needed to do the encryption first, then the encoding, but I did it the other way around.

这里是代码:

# 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) 

所以,当我去做哈希,我意识到我不得不从字符串一个字节[],我不知道如何做到这一点。我认为有一个简单的方法从.Net库,但找不到一个例子。

So, when I went to do the hashing I realized I had to have a byte[] from the string and I'm not sure how to do that. I'm thinking there is a simple way from the .Net libraries, but couldn't find an example.

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

So if I have a string, like:

$string = "password"

如何将它转换成我可以使用的字节数组:: ComputeHash($ string)?

How do I convert that into a byte array that I can use on :: ComputeHash($string)?

所以我必须结束它是一个加密的SHA-1和基本64编码的UTF-8密码,上面的代码是,但是它回到不同于当我在java中编码这个相同的东西时,我先加密它,然后将其转换为基础64编码。

So what I have to end up with is an encrypted SHA-1 and base 64 encoded UTF-8 password, which the code above does, but it's coming back different than when I coded this same thing in java, where I encrypted it first, then converted that result to base 64 encoding.

我假设在api不支持直接加密字符串的情况下,可能会有一些解决方法,可以让您做这个。这是我试图做的。

所以我假设我的问题的代码是,我必须先加密,然后编码它得到正确的价值。

So I'm assuming my issue with the code is that I had to encrypt it first and then encode it to get the correct value. Correct or am I missing something here?

以下是相关的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代码时,我得到

When I run the java code with the password string of "password" I get

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

[91, -86, 97, -28, -55, -71, 63, 63, 6, -126, 37, 11, 108, -8, 51, 27, 126, -26, -113, -40] which is the encryption.

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

Then I when the encoding in java I get this: W6ph5Mm5Pz8GgiULbPgzG37mj9g=

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

but when I run it in PowerShell I get this because it's encoded first for UTF8:

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

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

然后,当我运行这行代码来转换它,我得到一个错误:

Then when I run this line of code to convert it I get an error:

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

异常调用FromBase64String与1参数:Base-64字符数组的长度无效。
在行:1 char:45

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

However, if I run the new line of code to make it hex from below I get:

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

5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

但我需要得到这个值:

W6ph5Mm5Pz8GgiULbPgzG37mj9g =

,这可能甚至不可能做,但我正在努力寻找一个解决方案。

Again, this may not even be possible to do, but I'm trying to find a work-around to see.

推荐答案

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

You most likely just need to convert your hash to base64 after the last line.

$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

Text->Bytes->Encrypt/Hash->Base64

这是一个非常的普通模式,用于发送e以文本格式加密的数据。

That's a very common pattern for sending encrypted data in a text format.

这篇关于在PowerShell版本2中将字符串转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆