我现在用的充气城堡,并在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 =
所以加密字符串的前半部分是相同的,但下半年是没有的。这让我认为我得到了填充不正确的东西。
任何意见,将不胜感激。
谢谢
在PHP中使用openssl进行加密和解密。 –
您在PHP代码中使用ECB模式,在C#代码中使用CBC模式。此外,您正在C#加密函数中使用全零IV,但在PHP代码中使用了随机IV。您也不会在任一代码中发送IV。你在这里有很多错误。 –
使用CTR模式。忘记你听说过任何其他模式。 – Ben