加密 $plainText:明文 $key:密钥 function encrypt($plainText, $key) { // Ensure key length is appropriate for AES (16, 24, or 32 bytes) $keyLength = strlen($key); if ($keyLength !== 16 && $keyLength !== 24 && $keyLength !== 32) { return [null, "Invalid key length"]; } // Generate a random IV $iv = openssl_random_pseudo_bytes(16); if ($iv === false) { return [null, "IV generation failed"]; } // Encrypt the plaintext using AES in CFB mode $ciphertext = openssl_encrypt($plainText, 'aes-' . ($keyLength * 8) . '-cfb', $key, OPENSSL_RAW_DATA, $iv); if ($ciphertext === false) { return [null, "Encryption error"]; } // Prepend the IV to the ciphertext $ciphertext = $iv . $ciphertext; // Encode the result as Base64 URL $encryptedID = strtr(base64_encode($ciphertext), '+/&
AES对称加密解密
于 2024-07-10 09:28:26 首次发布