如何解密C#中通过PowerShell加密的字符串
是否有可能解密C#中通过PowerShell加密的字符串,以及如何解密 字符串通过PowerShell加密,如下所示:如何解密C#中通过PowerShell加密的字符串,c#,.net,powershell,encryption,securestring,C#,.net,Powershell,Encryption,Securestring,是否有可能解密C#中通过PowerShell加密的字符串,以及如何解密 字符串通过PowerShell加密,如下所示: $pw=读取主机“输入密码”–AsSecureString ConvertFrom SecureString$pw |输出文件“C:\file.txt” 要使用PowerShell将其转换回,我可以使用这些命令调用C#classSystem.Runtime.InteropServices.Marshal $pwdSec=获取内容“C:\file.txt”|转换为SecureS
$pw=读取主机“输入密码”–AsSecureString
ConvertFrom SecureString$pw |输出文件“C:\file.txt”
要使用PowerShell将其转换回,我可以使用这些命令调用C#classSystem.Runtime.InteropServices.Marshal
$pwdSec=获取内容“C:\file.txt”|转换为SecureString
$bPswd=[System.Runtime.InteropServices.Marshall]::SecureStringToBSTR($pwdSec)
$pswd=[System.Runtime.InteropServices.Marshall]::PtrToStringAuto($bPswd)
文件包含已转换为加密标准的字符串字符串(“hello”)
因此,如果打开file.txt
文件,它看起来类似于:
01000000D08C9DDF0115D1118C7A00C04FC297EB0100000052DED6C2DB80E748933432E19B9DE8B1000
0000020000000000003660000C0000000000000016DC35885D76D07BAB289EB9927CFC1000000000480
0000A000000010000003106CDE553F45B08D13D89D11336170B28000005CC865C1EEE1B57E84ED3D1A2
D3F2D0EC0F189B532E61C18D1F31444D6F119A1E8368477FD2D81F541400000CB0262E58B08AE14F37
22c14c69684841b6b21c
您拥有的ConvertFrom SecureString
输出文件是一个UTF-16(密码)字符串,受ProtectedData.Protect保护,存储为十六进制转储
要还原编码,请使用:
// Read file to string
string exportedData = File.ReadAllText(@"file.txt");
// Remove all new-lines
exportedData = exportedData.Replace(Environment.NewLine, "");
// Convert the hex dump to byte array
int length = exportedData.Length / 2;
byte[] encryptedData = new byte[length];
for (int index = 0; index < length; ++index)
{
encryptedData[index] =
byte.Parse(
exportedData.Substring(2 * index, 2),
NumberStyles.HexNumber, CultureInfo.InvariantCulture);
}
// Decrypt the byte array to Unicode byte array
byte[] data = ProtectedData.Unprotect(
encryptedData, (byte[])null, DataProtectionScope.CurrentUser);
// Convert Unicode byte array to string
string password = Encoding.Unicode.GetString(data);
//将文件读取为字符串
字符串exportedData=File.ReadAllText(@“File.txt”);
//删除所有新行
exportedData=exportedData.Replace(Environment.NewLine,“”);
//将十六进制转储转换为字节数组
int length=exportedData.length/2;
字节[]加密数据=新字节[长度];
对于(int-index=0;index
当不使用指定键指定-键时,上述代码起作用。然后,安全字符串将受到保护。因此,字符串必须在编码时在同一台机器和同一个帐户上解码。我需要在power shell中加密字符串并在.Net中解密
请找到以下函数来加密任何字符串。这里(1..16)是一个字节数组
function EncriptStringData {
[CmdletBinding()]
param (
[string] $PlainText
)
$someSecureString = $PlainText | ConvertTo-SecureString -AsPlainText -Force
$encryptedTextThatIcouldSaveToFile = ConvertFrom-SecureString -key (1..16) -SecureString $someSecureString
return $encryptedTextThatIcouldSaveToFile
}
现在,我将这个加密字符串输出用作.Net程序的输入,并获得与.Net程序输出相同的明文。
请查找以下函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
namespace MyNameSpace
{
public class DecryptStringData
{
public string GetDecryptString(string EncriptData)
{
try
{
byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
byte[] asBytes = Convert.FromBase64String(EncriptData);
string[] strArray = Encoding.Unicode.GetString(asBytes).Split(new[] { '|' });
if (strArray.Length != 3) throw new InvalidDataException("input had incorrect format");
byte[] magicHeader = HexStringToByteArray(EncriptData.Substring(0, 32));
byte[] rgbIV = Convert.FromBase64String(strArray[1]);
byte[] cipherBytes = HexStringToByteArray(strArray[2]);
SecureString str = new SecureString();
SymmetricAlgorithm algorithm = SymmetricAlgorithm.Create(); //This for .Net 4.5
//Use this for .Net core // AesManaged algorithm = new AesManaged();
ICryptoTransform transform = algorithm.CreateDecryptor(key, rgbIV);
using (var stream = new CryptoStream(new MemoryStream(cipherBytes), transform, CryptoStreamMode.Read))
{
int numRed = 0;
byte[] buffer = new byte[2]; // two bytes per unicode char
while ((numRed = stream.Read(buffer, 0, buffer.Length)) > 0)
{
str.AppendChar(Encoding.Unicode.GetString(buffer).ToCharArray()[0]);
}
}
string secretvalue = convertToUNSecureString(str);
return secretvalue;
}
catch (Exception ex)
{
return ex.Message;
}
}
public static byte[] HexStringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2) bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public static string convertToUNSecureString(SecureString secstrPassword)
{
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secstrPassword);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用System.Runtime.InteropServices;
使用系统安全;
使用System.Security.Cryptography;
名称空间MyNameSpace
{
公共类解密数据
{
公共字符串GetDecryptString(字符串EncryptData)
{
尝试
{
字节[]键=新字节[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
byte[]asBytes=Convert.FromBase64String(EncriptData);
string[]strArray=Encoding.Unicode.GetString(asBytes.Split)(新[]{'.});
如果(strArray.Length!=3)抛出新的InvalidDataException(“输入的格式不正确”);
字节[]magicHeader=hextStringToByteArray(EncriptData.Substring(0,32));
字节[]rgbIV=Convert.FromBase64String(strArray[1]);
字节[]cipherBytes=HexStringToByteArray(strArray[2]);
SecureString str=新的SecureString();
SymmetricAlgorithm algorithm=SymmetricAlgorithm.Create();//这适用于.Net 4.5
//将其用于.Net core//AESModed algorithm=new AESModed();
ICryptoTransform transform=算法.CreateDecryptor(密钥,rgbIV);
使用(var stream=newcryptostream(新内存流(cipherBytes)、转换、CryptoStreamMode.Read))
{
int numRed=0;
byte[]buffer=新字节[2];//每个unicode字符两个字节
而((numRed=stream.Read(buffer,0,buffer.Length))>0)
{
str.AppendChar(Encoding.Unicode.GetString(buffer.tocharray()[0]);
}
}
string secretvalue=convertToUNSecureString(str);
返回secretvalue;
}
捕获(例外情况除外)
{
返回ex.消息;
}
}
公共静态字节[]HexStringToByteArray(字符串十六进制)
{
int numbercars=十六进制长度;
字节[]字节=新字节[numbercars/2];
对于(inti=0;i
}可能存在的副本