PHP中的AES-256加密

ar5n3qh5  于 2023-08-02  发布在  PHP
关注(0)|答案(5)|浏览(186)

我需要一个PHP函数,AES256_encode($dataToEcrypt)$data加密为AES-256,另一个AES256_decode($encryptedData)则相反。有谁知道这个函数应该有什么代码?

aor9mmx1

aor9mmx11#

查看mcrypt module
AES-Rijndael示例摘自here

  1. $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
  2. $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
  3. $key = pack('H*', "bcb04b7e103a0cd8b54763051cef08bc55abe029fdebae5e1d417e2ffb2a00a3");
  4. # show key size use either 16, 24 or 32 byte keys for AES-128, 192
  5. # and 256 respectively
  6. $key_size = strlen($key);
  7. echo "Key size: " . $key_size . "\n";
  8. $text = "Meet me at 11 o'clock behind the monument.";
  9. echo strlen($text) . "\n";
  10. $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
  11. echo strlen($crypttext) . "\n";

字符串
这就是decrypt function

展开查看全部
nhaq1z21

nhaq1z212#

我需要一个PHP函数,AES256_encode($dataToEcrypt)$data加密为AES-256,另一个AES256_decode($encryptedData)则相反。有谁知道这个函数应该有什么代码?
有一个difference between encrypting and encoding
你真的需要AES-256吗?AES-256与AES-128的安全性没有那么重要;你更有可能在协议层搞砸而不是被黑客攻击,因为你使用了128位的分组密码而不是256位的分组密码。

重要提示-使用库

x1c 0d1x的数据

一个快速而肮脏的AES-256实现

如果您对构建自己的感兴趣,不是为了在生产环境中部署它,而是为了您自己的学习,我提供了一个示例AES 256

  1. /**
  2. * This is a quick and dirty proof of concept for StackOverflow.
  3. *
  4. * @ref http://stackoverflow.com/q/6770370/2224584
  5. *
  6. * Do not use this in production.
  7. */
  8. abstract class ExperimentalAES256DoNotActuallyUse
  9. {
  10. /**
  11. * Encrypt with AES-256-CTR + HMAC-SHA-512
  12. *
  13. * @param string $plaintext Your message
  14. * @param string $encryptionKey Key for encryption
  15. * @param string $macKey Key for calculating the MAC
  16. * @return string
  17. */
  18. public static function encrypt($plaintext, $encryptionKey, $macKey)
  19. {
  20. $nonce = random_bytes(16);
  21. $ciphertext = openssl_encrypt(
  22. $plaintext,
  23. 'aes-256-ctr',
  24. $encryptionKey,
  25. OPENSSL_RAW_DATA,
  26. $nonce
  27. );
  28. $mac = hash_hmac('sha512', $nonce.$ciphertext, $macKey, true);
  29. return base64_encode($mac.$nonce.$ciphertext);
  30. }
  31. /**
  32. * Verify HMAC-SHA-512 then decrypt AES-256-CTR
  33. *
  34. * @param string $message Encrypted message
  35. * @param string $encryptionKey Key for encryption
  36. * @param string $macKey Key for calculating the MAC
  37. */
  38. public static function decrypt($message, $encryptionKey, $macKey)
  39. {
  40. $decoded = base64_decode($message);
  41. $mac = mb_substr($message, 0, 64, '8bit');
  42. $nonce = mb_substr($message, 64, 16, '8bit');
  43. $ciphertext = mb_substr($message, 80, null, '8bit');
  44. $calc = hash_hmac('sha512', $nonce.$ciphertext, $macKey, true);
  45. if (!hash_equals($calc, $mac)) {
  46. throw new Exception('Invalid MAC');
  47. }
  48. return openssl_decrypt(
  49. $ciphertext,
  50. 'aes-256-ctr',
  51. $encryptionKey,
  52. OPENSSL_RAW_DATA,
  53. $nonce
  54. );
  55. }
  56. }

字符串

用法

首先,生成两个密钥(是的,其中两个)并以某种方式存储它们。

  1. $eKey = random_bytes(32);
  2. $aKey = random_bytes(32);


然后要加密/解密消息:

  1. $plaintext = 'This is just a test message.';
  2. $encrypted = ExperimentalAES256DoNotActuallyUse::encrypt($plaintext, $eKey, $aKey);
  3. $decrypted = ExperimentalAES256DoNotActuallyUse::decrypt($encrypted, $eKey, $aKey);


如果你没有random_bytes(),就用random_compat

展开查看全部
n3schb8v

n3schb8v3#

MCRYPT_RIJNDAEL_256不等同于AES_256。

从AES中解密RIJNDAEL的方法是使用MCRYPT_RIJNDAEL_128,并在加密之前添加要加密的字符串
AES-256具有BlockSize= 128 bit和KeySize= 256 bit Rijndael-256具有BlockSize= 256 bit和KeySize= 256 bit
只有AES/Rijndael 128 bit是相同的。Rijndael-192和Rijndael-256与AES-192和AES-256不同(块大小和轮数不同)。

kkih6yb8

kkih6yb84#

您可以在PHP中使用password_hash函数加密和解密数据。你还需要一个成本函数。确保在PHP环境中启用了OpenSSL和Mcrypt扩展。

  1. <?php
  2. $plaintext = 'My secret message 1234';
  3. $password = '3sc3RLrpd17';
  4. $method = 'aes-256-cbc';
  5. $key = password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]);
  6. // IV must be exact 16 chars (128 bit)
  7. $iv = random_bytes(16);
  8. $encrypted = base64_encode(openssl_encrypt($plaintext, $method, $key, OPENSSL_RAW_DATA, $iv));
  9. $decrypted = openssl_decrypt(base64_decode($encrypted), $method, $key, OPENSSL_RAW_DATA, $iv);
  10. echo 'plaintext=' . $plaintext . "\n";
  11. echo 'cipher=' . $method . "\n";
  12. echo 'encrypted to: ' . $encrypted . "\n";
  13. echo 'decrypted to: ' . $decrypted . "\n\n";
  14. ?>

字符串

  • $plaintext:这个变量保存我们想要加密的消息。在该示例中,“我的秘密消息1234”是明文。
  • $password:在这里,我们定义用于导出加密密钥的密码。
  • $method:我们将加密方法设置为“aes-256-CBC”,这是密码块链接(CBC)模式中的AES-256加密模式。
  • $key:代码使用提供的密码生成加密密钥,使用password_hash()函数和PASSWORD_BCRYPT算法,成本因子为12。
  • $iv:CBC模式下的AES-256需要初始化向量(IV),并且必须正好是16字节长(128位)。
  • $encrypted:使用openssl_encrypt()函数获取加密密文,该函数接受明文、加密方法、加密密钥、IV等参数。然后对密文进行Base64编码以便于表示。
  • $decrypted:为了确保解密过程正常,我们使用openssl_decrypt()函数来反转加密过程。这将在解码Base64表示后返回原始明文。

来源:Secure AES-256 Encryption and Decryption in PHP

展开查看全部
rkttyhzu

rkttyhzu5#

  1. $key = '324325923495kdfgiert734t'; // key used for decryption in jasper code
  2. $text = 'string_to_be_encrypted';
  3. $encrypted = fnEncrypt($text, $key);
  4. function fnEncrypt( $plaintext, $key )
  5. {
  6. $plaintext = pkcs5_pad($plaintext, 16);
  7. return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, hex2bin($key), $plaintext, MCRYPT_MODE_ECB));
  8. }
  9. function pkcs5_pad ($text, $blocksize)
  10. {
  11. $pad = $blocksize - (strlen($text) % $blocksize);
  12. return $text . str_repeat(chr($pad), $pad);
  13. }
  14. function hex2bin($hexdata)
  15. {
  16. $bindata = "";
  17. for ($i = 0; $i < strlen($hexdata); $i += 2)
  18. {
  19. $bindata .= chr(hexdec(substr($hexdata, $i, 2)));
  20. }
  21. return $bindata;
  22. }

字符串

展开查看全部

相关问题