프로토콜을 통해 전송 된 암호를 해독하기 위해 PHP (서비스)로 코드를 작성했습니다. 프로토콜은 AES256을 사용하여 "Mac then Encrypt"(MtE) 한 다음 base-64를 사용하여 인코딩 할 암호를 요구합니다.
메시지 구조는 php.net의이 주석에 설명되어 있습니다.
base46encoded (iv + ecrypted(mac + password))
프로세스는 PHP를 사용하여 쉬웠습니다.
public static function getPassword($password, $key, $mac_algorithm = 'sha1',
$enc_algorithm = MCRYPT_RIJNDAEL_256, $enc_mode = MCRYPT_MODE_CBC)
{
// truncating pre-shared key to 32 bytes.
$key = substr($key, 0, 32);
// decoding the message (being a password) from base64
$password = base64_decode($password);
// getting the iv size based on algorithm and encryption mode
$iv_size = mcrypt_get_iv_size($enc_algorithm, $enc_mode);
// extracting iv from message header (normally the first 32 byte) for decryption
$iv_dec = substr($password, 0, $iv_size);
// getting the encrypted message after the header (after the first 32 byte)
$password = substr($password, $iv_size);
// decrypting message using the pre-shared key and extracted iv
$password = mcrypt_decrypt($enc_algorithm, $key, $password, $enc_mode, $iv_dec);
// getting block size for hash algorithm in bytes (sha1 block size is 160 bit)
$mac_block_size = ceil(static::getMacAlgoBlockSize($mac_algorithm)/8);
// extracting the mac from the header of decrypted message
$mac_dec = substr($password, 0, $mac_block_size);
// extracting the valuable message
$password = substr($password, $mac_block_size);
// eliminate extra null terminators padded as the result of enc/decryption the following if and the next statement are check clauses for unpack function
$password = unpack('Z*', $password);
if (!isset($password[1]))
{
return false;
}
// obtaining the pure intended message (being the password) from the unpack result
$password = $password[1];
// regenerating the mac to control the authenticity and correctness of transmission
$mac = hash_hmac($mac_algorithm, $password, $key, true);
// see if transmitted mac (mac_dec) and the generated mac are the same and the data is valid
if($mac_dec == $mac)
{
return $password;
}
else
{
return false;
}
}
이제 문제는 응용 프로그램이 iOS에서이 프로토콜을 중심으로 개발되고 AESCrypt 및 CCCrypt를 시도했지만 암호 해독 결과가 다르다는 것입니다 (무의미한 말).
표준 CCHmac, Base64EncodedStringWithOptions : NSDataBase64EncodingEndLineWithCarriageReturn 및 This SO answer for CCCrypt를 사용했습니다.
mcrypt_generic_init 함수는 키와 IV를 모두 지정하여 암호를 초기화합니다. 키의 길이는 128 비트, 192 비트 또는 256 비트 암호화를 수행하는지 여부를 결정합니다.
따라서 AES256과 호환되도록 256 비트 (32 바이트) 키와 함께 MCRYPT_RIJNDAEL_128을 사용하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다