Я шифрую свои данные в PHP следующим образом:
$encrypted_string = mcrypt_encrypt(MCRYPT_DES, 'abcdefgh' , $input, MCRYPT_MODE_CBC, 'qwerasdf' );
$encrypted_string = base64_encode($encrypted_string);
return $encrypted_string;
И расшифровываем то же самое в C # следующим образом:
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 #, выдается следующее исключение:
Входная строка была в неправильном формате.
В следующей строке: Byte.Parse (hexCode, NumberStyles.AllowHexSpecifier);
Есть идеи, как что я делаю неправильно?