[英]Cant get password hash via openssl in c same format as crypt, mkpasswd or openssl via cmdline
[英]What is the equivalent of my openssl cmdline in C?
我正在嘗試在 C 中實現以下 openssl 命令行:
openssl enc -aes-256-cbc -d -in /tmp/out_enc -K \
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \
-iv 0 -nopad -p
命令行的輸出:
key=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
iv =00000000000000000000000000000000
<CONNECTION_REQUEST ATS_ID="2" ATP_SEQ_ID="1" REGISTRATION_ID="Y2G5R52S8PP6YX47" SERIAL_NUMBER="724574802" SPC_PRODUCT_TITLE="SPC5300" SPC_FW_VERSION="3.8.5 - R.31629" ATS_NAME="Système (ATS) 2" ATP1_ID="2" ATP1_UID="34" ATP1_NAME="Principal ATP 1" ATP1_COMMS_INTERFACE="1" ATP1_DEST="1, 192.168.1.62:52000" ATP1_CATEGORY="50"/>�z
這是我在 C 中的等價物:
long
_ast_crypt_decrypt_generic(unsigned char* ciphertext, long cipherlen, unsigned char* plaintext, const EVP_CIPHER *cipher) {
long result = 0;
/* A 256 bit key */
unsigned char *key = (unsigned char *)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"0000000000000000";
EVP_CIPHER_CTX *ctx = NULL;
int len;
long plaintext_len;
if (cipher) {
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new())) {
g_warning("AstCrypt : EVP_CIPHER_CTX_new failed");
goto end;
}
if (1 != EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv)) {
g_warning("AstCrypt : EVP_DecryptInit_ex");
}
EVP_CIPHER_CTX_set_padding(ctx, 0);
if (1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, cipherlen)) {
g_warning("AstCrypt : EVP_DecryptUpdate");
goto end;
}
plaintext_len = len;
if (1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) {
g_warning("AstCrypt : EVP_DecryptFinal_ex failed");
goto end;
}
plaintext_len += len;
result= plaintext_len;
}
else {
g_warning("AstCrypt : Failed to get the openssl elements.");
}
end:
if (ctx)
EVP_CIPHER_CTX_free(ctx);
return result;
}
long plainSize = _ast_crypt_decrypt_generic(headerData, headerLength, bPlain, EVP_aes_256_cbc());
//fwrite(bPlain, plainSize, 1, stdout);
前面的代碼示例沒有正確解密數據(在 C 中)。
你有什么想法我缺少什么嗎?
這可能是對 openssl 庫中的 key/iv 格式缺乏理解。
答案是 key/iv 必須以十六進制表示:
以下按預期工作:
unsigned char key[] =
{ 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA,0 };
//unsigned char key[] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} ;
/* A 128 bit IV */
unsigned char iv[] =
{ 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,0 };
問題是您假設密鑰是“密碼”的直接副本。 您在該命令行中所做的是將密鑰指定為十六進制字符串。 因此,您需要做的是將您的“密碼”從十六進制字符串轉換為密鑰緩沖區。 在您的示例中,您應該將每個字節設置為十六進制值“0xAA”。
因為您是直接傳入密鑰,所以不需要 MD5 參數(到您的函數或 openssl)。
如果您想使用密碼而不是“十六進制字符串”,那么您需要以某種方式將其散列到密鑰緩沖區中。 這就是 MD5 參數的用武之地。您可以使用 PBKDF2 之類的東西(盡管我會使用 openssl 默認的 SHA256 而不是 MD5 作為散列函數)。
例如 openssl enc -aes-256-cbc -pbkdf2 -k 密碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.