私はC#とPHPで以下のコードを持っており、これまでのところ両者の結果が一致していません。 C#ではバウンシーキャッスルAPIを使用していますが、PHPではopenSSLを使用しています。確かに、私はC#でMicrosoft Crypto Libraryを使って同じことを暗号化していますが、同じ暗号テキストを持っていますが、PHPと交換するときは同じではありません。C#bouncycastleとPHP opensslの間でAES-256-CBC/PKCS7を交換できません
C#
public const string AES_ALGORITHM = "AES/CBC/PKCS7";
public const string CRYPTO_ALGORITHM = "AES";
public AESController(string key) {
encryptionKey = Convert.FromBase64String(key);
}
public string encrypt(string plainText, byte[] iv) {
string cipherText = "";
//setting up AES Key
KeyParameter aesKeyParam = ParameterUtilities.CreateKeyParameter(CRYPTO_ALGORITHM, encryptionKey);
// Setting up the Initialization Vector. IV is used for encrypting the first block of input message
ParametersWithIV aesIVKeyParam = new ParametersWithIV(aesKeyParam, iv);
// Create the cipher object for AES algorithm using GCM mode and NO padding
IBufferedCipher cipher = CipherUtilities.GetCipher(AES_ALGORITHM);
cipher.Init(true, aesIVKeyParam);
byte[] output = cipher.DoFinal(Encoding.UTF8.GetBytes(plainText));
cipherText = Convert.ToBase64String(output);
return cipherText;
}
public string decrypt(string cipherText, byte[] iv) {
string plainText = "";
//setting up AES Key
KeyParameter aesKeyParam = ParameterUtilities.CreateKeyParameter(CRYPTO_ALGORITHM, encryptionKey);
// Setting up the Initialization Vector. IV is used for encrypting the first block of input message
ParametersWithIV aesIVKeyParam = new ParametersWithIV(aesKeyParam, iv);
// Create the cipher object for AES algorithm using GCM mode and NO padding
IBufferedCipher cipher = CipherUtilities.GetCipher(AES_ALGORITHM);
cipher.Init(false, aesIVKeyParam);
byte[] output = cipher.DoFinal(Convert.FromBase64String(cipherText));
plainText = Encoding.UTF8.GetString(output);
return plainText;
}
static void Main(string[] args) {
AESController aes = new AESController("OXzN4fxHHgcKtAt/SJ4UWtNiQlzno7II1gIBs24CWpY=");
byte[] iv = Base64.Decode("fdL8sKmhC8YIrYHMzoJJvQ==");
string encryptedText = "TESTING123";
string cipherText = aes.encrypt(encryptedText, iv);
Console.Out.WriteLine("IV: " + Encoding.UTF8.GetString(Base64.Encode(iv)));
Console.Out.WriteLine("Plain Text:"+aes.decrypt(cipherText, iv));
Console.Out.WriteLine("Cipher Text:" + cipherText);
Console.In.ReadLine();
}
、その後、PHP:
$key = "OXzN4fxHHgcKtAt/SJ4UWtNiQlzno7II1gIBs24CWpY=";
$iv = base64_decode("fdL8sKmhC8YIrYHMzoJJvQ==");
//$iv = openssl_random_pseudo_bytes(16);
//$cipherText = "YMdJ0mdWZ+p50krLzhyI0Q==";
$plainText = "TESTING123";
$decrypted = openssl_encrypt($plainText, "aes-256-cbc", $key, 0, $iv);
var_dump($decrypted);
ので、C#のはBouncyCastleから来た暗号文は次のとおりです。
EnOtpv4fOGiAKMzXXJMYBA ==
のCs +のF7A/DBEGma7iQ/PQVLw ==
と私はPHPにそのC#の暗号文を取る場合、復号化された結果は次のとおりです:PHPから来ると暗号文があるBOOL(偽) (暗号化が失敗した)
最後に、PHPであれば、私はOPENSSL_ZERO_PADDINGにパディングを変更し、C#のから暗号文を解読し、その結果は次のとおりです。
_X GmNSlZx
私が集めることができるかについては
、キーとIVは、(両方ともBASE64文字列です)の両方で同じである、暗号化された結果が暗号の終わりに2 ==を説明し、同じ長さを(持っていますテキスト。私が見逃していることはありますか、または私はopensslライブラリにとって特別なことをする必要がありますか?ありがとう。
あなたはPHPコードでキーをbase64でデコードしていません。 – Narf
私が理解しているところでは、鍵はopenssl_encryptのBASE64形式であるとみなされます。 –
まあ、そうではありません。 :) – Narf