2010-01-08 128 views
0

我加密我的數據在PHP這樣的:使用ASP.Net解密字符串被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

嘗試Byte.Parse(hexCode, System.Globalization.NumberStyles.HexNumber);

由於AllowHexSpecifier是0x1b風格的十六進制數。