Arrays 从字符串创建字节数组vs从文件创建字节数组

Arrays 从字符串创建字节数组vs从文件创建字节数组,arrays,powershell,encryption,encoding,rsa,Arrays,Powershell,Encryption,Encoding,Rsa,我正试图解密一些文本。 我能够从文件中解密文本,但是当我复制该文件的内容并将其直接放入字符串中时,它不起作用,因为字节数组略有不同 如何从字符串中获取字节数组,该字符串与从包含该字符串的文件中读取时的字节数组相同 这是我的密码: $privateKeyFile = [System.IO.FileInfo]'D:\Avanade\CBA\Scripts\Encryption\Keys\cba.private.xml' $privateKey = [System.IO.File]::OpenText

我正试图解密一些文本。 我能够从文件中解密文本,但是当我复制该文件的内容并将其直接放入字符串中时,它不起作用,因为字节数组略有不同

如何从字符串中获取字节数组,该字符串与从包含该字符串的文件中读取时的字节数组相同

这是我的密码:

$privateKeyFile = [System.IO.FileInfo]'D:\Avanade\CBA\Scripts\Encryption\Keys\cba.private.xml'
$privateKey = [System.IO.File]::OpenText($($privateKeyFile.FullName)).ReadToEnd() 

$rsaProvider = New-Object System.Security.Cryptography.RSACryptoServiceProvider
$rsaProvider.FromXmlString($privateKey)

$encryptedData = 'ꨢﻥ睚紫震እ�풽꞊偓䷨頽ױ㻮앚튛堏焞娌젣래核儝쪅元㝂㢚覰齉c㑥㰺ᨅ㵉ァ镮邹꽋荺眢ꢈ쑷絓�ꮹ栊ハ垅懻惜䡠덟蓩瘫㙉ਧ騰י聗�၁틽ᮿ싓㈧ハ腰瑦ꊕ媘겻辖庖甏ܫ桑敘옐餈꿎請쌝⢸蒺銟஦ᩅ캼Շ疑ꊽ�䐼ꀑ醾耣咞䏎帾힆纄܏㎡㨇괎ꆠ䵢싐쇢绽굈ữ禘'
$encryptedDataAsByteArray1 = [System.Text.Encoding]::UniCode.GetBytes($encryptedData) #this byte array does NOT work

$FileToDecrypt = [System.IO.FileInfo]'D:\Avanade\CBA\Scripts\Encryption\d.txt.encrypted' #this has the same text as $encryptedData
$encryptedFile = [System.IO.File]::OpenRead($($FileToDecrypt.FullName))
$encryptedDataAsByteArray = New-Object System.Byte[] $encryptedFile.Length #This byte array works
$encryptedFile.Read($encryptedDataAsByteArray, 0, $encryptedFile.Length) 
$encryptedFile.Close()

for ($i = 0; $i -lt $encryptedDataAsByteArray1.Count; $i++)
{
    if ($encryptedDataAsByteArray1[$i] -ne $encryptedDataAsByteArray[$i])
    {
        Write-Host "Byte $i is not the same"
        Write-Host "$($encryptedDataAsByteArray1[$i]) and $($encryptedDataAsByteArray[$i])"
    }
}

$decryptedDataAsByteArray = $rsaProvider.Decrypt($encryptedDataAsByteArray, $false)

<#
Comparison of the two byte arrays:
Byte 12 is not the same
253 and 47
Byte 13 is not the same
255 and 223
Byte 92 is not the same
253 and 179
Byte 93 is not the same
255 and 223
Byte 132 is not the same
253 and 127
Byte 133 is not the same
255 and 223
Byte 204 is not the same
253 and 67
Byte 205 is not the same
#>
$privateKeyFile=[System.IO.FileInfo]'D:\Avanade\CBA\Scripts\Encryption\Keys\CBA.private.xml'
$privateKey=[System.IO.File]::OpenText($($privateKeyFile.FullName)).ReadToEnd()
$rsaProvider=新对象System.Security.Cryptography.RSAPcryptoServiceProvider
$rsaProvider.FromXmlString($privateKey)
$encryptedData='1!'ꨢﻥ睚紫震እ�풽꞊偓䷨頽ױ㻮앚튛堏焞娌젣래核儝쪅元㝂㢚覰齉C㑥㰺ᨅ㵉ァ镮邹꽋荺眢ꢈ쑷絓�ꮹ栊ハ垅懻惜䡠덟蓩瘫㙉ਧ騰י聗�၁틽ᮿ싓㈧ハ腰瑦ꊕ媘겻辖庖甏ܫ桑敘옐餈꿎請쌝⢸蒺銟஦ᩅ캼Շ疑ꊽ�䐼ꀑ醾耣咞䏎帾힆纄܏㎡㨇괎ꆠ䵢싐쇢绽굈ữ禘'
$encryptedDataAsByteArray1=[System.Text.Encoding]::UniCode.GetBytes($encryptedData)#此字节数组不工作
$FileToDecrypt=[System.IO.FileInfo]'D:\Avanade\CBA\Scripts\Encryption\D.txt.encrypted'#此文本与$encryptedData相同
$encryptedFile=[System.IO.File]::OpenRead($($FileToDecrypt.FullName))
$encryptedDataAsByteArray=新对象系统.Byte[]$encryptedFile.Length#此字节数组有效
$encryptedFile.Read($encryptedDataAsByteArray,0,$encryptedFile.Length)
$encryptedFile.Close()
对于($i=0;$i-lt$encryptedDataAsByteArray1.Count;$i++)
{
如果($encryptedDataAsByteArray[$i]-ne$encryptedDataAsByteArray[$i])
{
写入主机“字节$i不相同”
写入主机“$($encryptedDataAsByteArray[$i])和$($encryptedDataAsByteArray[$i])”
}
}
$decryptedDataAsByteArray=$rsaProvider.Decrypt($encryptedDataAsByteArray,$false)

在文件上应用base 64编码,例如使用诸如
openssl
命令行之类的实用程序。然后使用剪贴板将其复制到字符串中。最后,只需在应用程序中对其进行解码

openssl base64 -in test.bin
结果:

VGhlIHF1aWNrIGJyb3duIGZveCB3YXMganVtcGVkIGJ5IHRoZSBzbGVhenkgZG9n
Lg==
将其复制到字符串中的问题是,您会遇到字符编码问题。加密字节可以有任何值,包括控制字符和其他不可打印字符。那些复制不好


这里有一些方法。请注意,如果您(已经)在处理字节,则无需执行字符(UTF-8)编码/解码。

非常感谢!最后我使用了.NET中的Convert.ToBase64String,它工作得很好。在.NET Convert类上使用openssl有什么好处吗?呵呵,没有。我只是因为加密标签而遇到这个问题,我现在在Linux上。但想法是一样的。