2016-10-10 111 views
0

我現在用的充氣城堡,並在C#下面的代碼進行加密,並在C#中的數據進行解密加密和解密使用充氣城堡在C#和PHP

public static string BCEncrypt(string input) 
{ 
    string keyString = "mysecretkey12345"; 
    string keyStringBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyString)); 
    byte[] inputBytes = Encoding.UTF8.GetBytes(input); 
    byte[] iv = new byte[16]; 

    //Set up 
    AesEngine engine = new AesEngine(); 
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC 
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding()); 
    //PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7 
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(keyStringBase64)); 
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); 

    // Encrypt 
    cipher.Init(true, keyParamWithIV); 
    byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)]; 
    int length = cipher.ProcessBytes(inputBytes, outputBytes, 0); 
    cipher.DoFinal(outputBytes, length); //Do the final block 
    return Convert.ToBase64String(outputBytes); 
} 

public static string BCDecrypt(string input) 
{ 
    string keyString = "mysecretkey12345"; 
    string keyStringBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyString)); 
    byte[] inputBytes = Encoding.UTF8.GetBytes(input); 
    byte[] iv = new byte[16]; 
    //Set up 
    AesEngine engine = new AesEngine(); 
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC 
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(blockCipher); //Default scheme is PKCS5/PKCS7 
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(keyStringBase64)); 
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); 

    //Decrypt    
    byte[] outputBytes = Convert.FromBase64String(input); 
    cipher.Init(false, keyParamWithIV); 
    byte[] comparisonBytes = new byte[cipher.GetOutputSize(outputBytes.Length)]; 
    int length = cipher.ProcessBytes(outputBytes, comparisonBytes, 0); 
    cipher.DoFinal(comparisonBytes, length); //Do the final block 
    return System.Text.Encoding.UTF8.GetString(comparisonBytes, 0, comparisonBytes.Length); 
} 

這是我使用的代碼PHP代碼:

<? 
    function encrypt($input, $key) { 
     $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB); 
     $input = Security::pkcs5_pad($input, $size); 
     $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, ''); 
     $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND); 
     mcrypt_generic_init($td, $key, $iv); 
     $data = mcrypt_generic($td, $input); 
     mcrypt_generic_deinit($td); 
     mcrypt_module_close($td); 
     $data = base64_encode($data); 
     return $data; 
    } 

    function pkcs5_pad ($text, $blocksize) { 
     $pad = $blocksize - (strlen($text) % $blocksize); 
     return $text . str_repeat(chr($pad), $pad); 
    } 

    function decrypt($sStr, $sKey) { 
     $decrypted= mcrypt_decrypt(
      MCRYPT_RIJNDAEL_128, 
      $sKey, 
      base64_decode($sStr), 
      MCRYPT_MODE_ECB 
     ); 
     $dec_s = strlen($decrypted); 
     $padding = ord($decrypted[$dec_s-1]); 
     $decrypted = substr($decrypted, 0, -$padding); 
     return $decrypted; 
    } 

    echo "Input: " . $_REQUEST["inp"] . "<br>Decrypt: ". decrypt($_REQUEST["inp"], 'mysecretkey12345')."<br>"; 
    ?> 

當我嘗試使用C#短的加密字符串,如「greatscott」我得到以下結果:dSk7z0F4JYsc0zhl95 + YMW ==

這然後使用PHP代碼解密確定。

然而,當我嘗試使用C#代碼,如「這是一個很長的字符串」更長的字符串加密,我得到以下結果:xcL4arrFD8Fie73evfHjvUjNEmZrA9h6SmO0ZRE82Hw =

而且這不會解密。如果我嘗試同樣的加密字符串「這是一個很長的字符串」通過PHP加密函數,我得到xcL4arrFD8Fie73evfHjva6yJyeUOrB8IudISDhQk24 =

所以加密字符串的前半部分是相同的,但下半年是沒有的。這讓我認爲我得到了填充不正確的東西。

任何意見,將不勝感激。

謝謝

+0

在PHP中使用openssl進行加密和解密。 –

+3

您在PHP代碼中使用ECB模式,在C#代碼中使用CBC模式。此外,您正在C#加密函數中使用全零IV,但在PHP代碼中使用了隨機IV。您也不會在任一代碼中發送IV。你在這裏有很多錯誤。 –

+0

使用CTR模式。忘記你聽說過任何其他模式。 – Ben

回答

0

感謝盧克和詹姆斯的建議。

我現在使用openssl在PHP中進行加密和解密,我生成一個隨機的IV並在系統之間傳遞它以進行解密。

這是我現在使用的代碼:

C#

public static string BCEncrypt(string input, out string iv_base64) 
{ 
    byte[] inputBytes = Encoding.UTF8.GetBytes(input); 
    SecureRandom random = new SecureRandom(); 
    byte[] iv = new byte[16]; 
    random.NextBytes(iv); 
    iv_base64 = Convert.ToBase64String(iv); 
    string keyString = "mysecretkey12345"; 
    string keyStringBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyString)); 

    //Set up 
    AesEngine engine = new AesEngine(); 
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC 
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding()); 
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(keyStringBase64)); 
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); 

    // Encrypt 
    cipher.Init(true, keyParamWithIV); 
    byte[] outputBytes = new byte[cipher.GetOutputSize(inputBytes.Length)]; 
    int length = cipher.ProcessBytes(inputBytes, outputBytes, 0); 
    cipher.DoFinal(outputBytes, length); //Do the final block 
    return Convert.ToBase64String(outputBytes); 
} 

public static string BCDecrypt(string input, string iv_base64) 
{ 
    string keyString = "mysecretkey12345"; 
    string keyStringBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(keyString)); 
    byte[] inputBytes = Encoding.UTF8.GetBytes(input); 
    byte[] iv = Convert.FromBase64String(iv_base64); 
    //Set up 
    AesEngine engine = new AesEngine(); 
    CbcBlockCipher blockCipher = new CbcBlockCipher(engine); //CBC 
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(engine), new Pkcs7Padding()); 
    KeyParameter keyParam = new KeyParameter(Convert.FromBase64String(keyStringBase64)); 
    ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv, 0, 16); 

    //Decrypt    
    byte[] outputBytes = Convert.FromBase64String(input); 
    cipher.Init(false, keyParamWithIV); 
    byte[] comparisonBytes = new byte[cipher.GetOutputSize(outputBytes.Length)]; 
    int length = cipher.ProcessBytes(outputBytes, comparisonBytes, 0); 
    cipher.DoFinal(comparisonBytes, length); //Do the final block 
    return Encoding.UTF8.GetString(comparisonBytes, 0, comparisonBytes.Length); 
} 

PHP的側面看是這樣的:

$iv = base64_decode($iv_base64); 
$method = "aes-128-cbc"; 
$password = "mysecretkey12345"; 
$decrypted = openssl_decrypt($data, $method, $password,0, $iv); 

並生成IV和加密在PHP中的字符串我正在使用:

$iv = openssl_random_pseudo_bytes(16) 
$encrypted = openssl_encrypt("something interesting", $method, $password,0,$iv); 

所以我現在可以用C#或PHP加密,並用C#或PHP解密。

我正在通過使用https的兩個系統之間的基礎64編碼iv和加密的字符串。

對此解決方案的任何意見? (安全還是其他?)