Расшифровка строки с использованием ASP.Net зашифрованного Php - PullRequest
0 голосов
/ 08 января 2010

Я шифрую свои данные в 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);

Есть идеи, как что я делаю неправильно?

1 Ответ

1 голос
/ 08 января 2010

Попробуйте Byte.Parse(hexCode, System.Globalization.NumberStyles.HexNumber);

Поскольку AllowHexSpecifier предназначен для шестнадцатеричных чисел в стиле 0x1b.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...