C# 用Php加密的ASP.Net解密字符串
我正在用PHP加密数据,如下所示:C# 用Php加密的ASP.Net解密字符串,c#,php,encryption,C#,Php,Encryption,我正在用PHP加密数据,如下所示: $encrypted_string = mcrypt_encrypt(MCRYPT_DES, 'abcdefgh' , $input, MCRYPT_MODE_CBC, 'qwerasdf' ); $encrypted_string = base64_encode($encrypted_string); return $encrypted_string; public string Decrypt(string input) { input = S
$encrypted_string = mcrypt_encrypt(MCRYPT_DES, 'abcdefgh' , $input, MCRYPT_MODE_CBC, 'qwerasdf' );
$encrypted_string = base64_encode($encrypted_string);
return $encrypted_string;
public string Decrypt(string input)
{
input = Server.UrlDecode(input);
byte[] binary = Convert.FromBase64String(input);
input = Encoding.GetEncoding(28591).GetString(binary);
DES tripleDes = DES.Create();
tripleDes.IV = Encoding.ASCII.GetBytes("NAVEEDNA");
tripleDes.Key = Encoding.ASCII.GetBytes("abcdegef");
tripleDes.Mode = CipherMode.CBC;
tripleDes.Padding = PaddingMode.Zeros;
ICryptoTransform crypto = tripleDes.CreateDecryptor();
byte[] decodedInput = Decoder(input);
byte[] decryptedBytes = crypto.TransformFinalBlock(decodedInput, 0, decodedInput.Length);
return Encoding.ASCII.GetString(decryptedBytes);
}
public byte[] Decoder(string input)
{
byte[] bytes = new byte[input.Length / 2];
int targetPosition = 0;
for (int sourcePosition = 0; sourcePosition < input.Length; sourcePosition += 2)
{
string hexCode = input.Substring(sourcePosition, 2);
bytes[targetPosition++] = Byte.Parse(hexCode, NumberStyles.AllowHexSpecifier);
}
return bytes;
}
并在C中对其进行解密,如下所示:
$encrypted_string = mcrypt_encrypt(MCRYPT_DES, 'abcdefgh' , $input, MCRYPT_MODE_CBC, 'qwerasdf' );
$encrypted_string = base64_encode($encrypted_string);
return $encrypted_string;
public string Decrypt(string input)
{
input = Server.UrlDecode(input);
byte[] binary = Convert.FromBase64String(input);
input = Encoding.GetEncoding(28591).GetString(binary);
DES tripleDes = DES.Create();
tripleDes.IV = Encoding.ASCII.GetBytes("NAVEEDNA");
tripleDes.Key = Encoding.ASCII.GetBytes("abcdegef");
tripleDes.Mode = CipherMode.CBC;
tripleDes.Padding = PaddingMode.Zeros;
ICryptoTransform crypto = tripleDes.CreateDecryptor();
byte[] decodedInput = Decoder(input);
byte[] decryptedBytes = crypto.TransformFinalBlock(decodedInput, 0, decodedInput.Length);
return Encoding.ASCII.GetString(decryptedBytes);
}
public byte[] Decoder(string input)
{
byte[] bytes = new byte[input.Length / 2];
int targetPosition = 0;
for (int sourcePosition = 0; sourcePosition < input.Length; sourcePosition += 2)
{
string hexCode = input.Substring(sourcePosition, 2);
bytes[targetPosition++] = Byte.Parse(hexCode, NumberStyles.AllowHexSpecifier);
}
return bytes;
}
公共字符串解密(字符串输入)
{
输入=Server.UrlDecode(输入);
字节[]二进制=Convert.FromBase64String(输入);
输入=Encoding.GetEncoding(28591).GetString(二进制);
DES tripleDes=DES.Create();
tripleDes.IV=Encoding.ASCII.GetBytes(“NAVEEDNA”);
tripleDes.Key=Encoding.ASCII.GetBytes(“abcdegef”);
tripleDes.Mode=CipherMode.CBC;
三倍。填充=填充模式。零;
ICryptoTransform crypto=tripleDes.CreateDecryptor();
字节[]decodedInput=解码器(输入);
byte[]decryptedBytes=crypto.TransformFinalBlock(decodedInput,0,decodedInput.Length);
返回Encoding.ASCII.GetString(decryptedBytes);
}
公共字节[]解码器(字符串输入)
{
字节[]字节=新字节[input.Length/2];
int targetPosition=0;
对于(int-sourcePosition=0;sourcePosition
当我试图在C中解密字符串时,它抛出以下异常:
$encrypted_string = mcrypt_encrypt(MCRYPT_DES, 'abcdefgh' , $input, MCRYPT_MODE_CBC, 'qwerasdf' );
$encrypted_string = base64_encode($encrypted_string);
return $encrypted_string;
public string Decrypt(string input)
{
input = Server.UrlDecode(input);
byte[] binary = Convert.FromBase64String(input);
input = Encoding.GetEncoding(28591).GetString(binary);
DES tripleDes = DES.Create();
tripleDes.IV = Encoding.ASCII.GetBytes("NAVEEDNA");
tripleDes.Key = Encoding.ASCII.GetBytes("abcdegef");
tripleDes.Mode = CipherMode.CBC;
tripleDes.Padding = PaddingMode.Zeros;
ICryptoTransform crypto = tripleDes.CreateDecryptor();
byte[] decodedInput = Decoder(input);
byte[] decryptedBytes = crypto.TransformFinalBlock(decodedInput, 0, decodedInput.Length);
return Encoding.ASCII.GetString(decryptedBytes);
}
public byte[] Decoder(string input)
{
byte[] bytes = new byte[input.Length / 2];
int targetPosition = 0;
for (int sourcePosition = 0; sourcePosition < input.Length; sourcePosition += 2)
{
string hexCode = input.Substring(sourcePosition, 2);
bytes[targetPosition++] = Byte.Parse(hexCode, NumberStyles.AllowHexSpecifier);
}
return bytes;
}
输入字符串的格式不正确
在下一行:Byte.Parse(hexCode,NumberStyles.allowehexspecifier)
知道我做错了什么吗?试试Byte.Parse(hexCode,System.Globalization.numberstyle.HexNumber)代码>
因为AllowHexSpecifier用于0x1b样式的十六进制数