본문 바로가기
개발자 커뮤니티에 오신 것을 환영합니다!

PHP [php] openssl encrypt/decrypt(AES-256-ECB)

페이지 정보

profile_image
작성자 하나를하더라도최선을
댓글 0건 조회 11,295회 작성일 22-08-09 18:54

본문

function encrypt($plaintext, $password) {
    $method = "AES-256-ECB";
    $key = hash('sha256', $password, true);
    $encrypt_iv = openssl_random_pseudo_bytes(16);
 
    $ciphertext = openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $encrypt_iv);
    $hash = hash_hmac('sha256', $ciphertext . $encrypt_iv, $key, true);
 
    return $encrypt_iv . $hash . $ciphertext;
}
 
function decrypt($ivHashCiphertext, $password) {
    $method = "AES-256-ECB";
    $decrypt_iv = substr($ivHashCiphertext, 016);
    $hash = substr($ivHashCiphertext, 1632);
    $ciphertext = substr($ivHashCiphertext, 48);
    $key = hash('sha256', $password, true);
 
    if (!hash_equals(hash_hmac('sha256', $ciphertext . $decrypt_iv, $key, true), $hash)) return null;
 
    return openssl_decrypt($ciphertext, $method, $key, OPENSSL_RAW_DATA, $decrypt_iv);
}

댓글목록

등록된 댓글이 없습니다.


Copyright © program1472 | IT HUB. All rights reserved.