我需要在C++中編碼一些數據,並在php中解碼它,但PHP解碼不正確。 我用相同的密鑰和iv檢查了編碼這個相同的消息,結果有差異。 這是我的代碼:在c + +和PHP的openssl加密
struct ctr_state
{
unsigned char ivec[AES_BLOCK_SIZE];
unsigned int num;
unsigned char ecount[AES_BLOCK_SIZE];
};
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
unsigned char iv[AES_BLOCK_SIZE];
struct ctr_state state;
int init_ctr(struct ctr_state *state, const byte iv[16])
{
/* aes_ctr128_encrypt requires 'num' and 'ecount' set to zero on the
* first call. */
state->num = 0;
memset(state->ecount, 0, AES_BLOCK_SIZE);
/* Initialise counter in 'ivec' to 0 */
memset(state->ivec + 8, 0, 8);
/* Copy IV into 'ivec' */
memcpy(state->ivec, iv, 8);
}
void aes_encoder(byte *read, byte *write, int size, byte *enc_key, byte *iv)
{
AES_KEY key;
if (AES_set_encrypt_key(enc_key, 128, &key) < 0)
{
Logger::getInstance()->Error("problem with setting encrypt key");
}
init_ctr(&state, iv);
AES_ctr128_encrypt(read, write, size, &key, state.ivec, state.ecount, &state.num);
}
byte *key = (byte*)"2123456789";
byte *iv = (byte*)"2asdasdasdasdasd";
QByteArray message = "this is message";
byte *data = reinterpret_cast<byte *>(message.data());
aes_encoder(data, data, message.size(), key, iv);
qDebug() << message.toBase64();
的結果是: 「HF/nlW4e + FmuF8Bfny9M」
和PHP代碼:
<?php
$message = "this is message";
$key = "2123456789";
$iv = "2asdasdasdasdasd";
$encrypted = openssl_encrypt($message, 'aes-128-ctr', $key, true, $iv);
echo base64_encode($encrypted);
結果: 「RLLUkP54El9FCeWpO/BI」
爲什麼結果不一樣?
在memset(state-> ivec + 8,0,8)行中使用了硬編碼的'8';'? – Floris
我將此代碼從此處取下:http://stackoverflow.com/a/3146214/1349417 – januszmk
重複使用CTR模式的隨機數*完全*打破加密。每個帶有重用隨機數的消息都可以在不知道密鑰的情況下恢復。將隨機代碼複製粘貼到互聯網上並不是編寫與安全相關的代碼的方式 - 您需要精確地瞭解每一個細節。或者,您可以使用久經考驗的庫。 – ntoskrnl