PHP AES 加密/解密

标签 php encryption cryptography aes encryption-symmetric

我找到了一个在 PHP 中编码/解码字符串的示例。起初它看起来非常好,但它不会工作:-(

有人知道问题出在哪里吗?

$Pass = "Passwort";
$Clear = "Klartext";

$crypted = fnEncrypt($Clear, $Pass);
echo "Encrypted: ".$crypted."</br>";

$newClear = fnDecrypt($crypted, $Pass);
echo "Decrypted: ".$newClear."</br>";

function fnEncrypt($sValue, $sSecretKey) {
    return trim(base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, $sDecrypted, MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND))));
}

function fnDecrypt($sValue, $sSecretKey) {
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sSecretKey, base64_decode($sEncrypted), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND)));
}

结果是:

加密:boKRNTYYNp7AiOvY1CidqsAn9wX4ufz/D9XrpjAOPk8=

解密:—‚(ÑÁ ^ yÈ~F'¸®Ó–í œð2Á_B‰—

最佳答案

请使用现有的 secure PHP encryption library

除非您有破解其他人的密码实现的经验,否则编写自己的密码通常是个坏主意。

这里没有示例authenticate the ciphertext ,这使它们容易受到位重写攻击。

如果您可以安装 PECL 扩展,libsodium更好

<?php
// PECL libsodium 0.2.1 and newer

/**
 * Encrypt a message
 * 
 * @param string $message - message to encrypt
 * @param string $key - encryption key
 * @return string
 */
function safeEncrypt($message, $key)
{
    $nonce = \Sodium\randombytes_buf(
        \Sodium\CRYPTO_SECRETBOX_NONCEBYTES
    );

    return base64_encode(
        $nonce.
        \Sodium\crypto_secretbox(
            $message,
            $nonce,
            $key
        )
    );
}

/**
 * Decrypt a message
 * 
 * @param string $encrypted - message encrypted with safeEncrypt()
 * @param string $key - encryption key
 * @return string
 */
function safeDecrypt($encrypted, $key)
{   
    $decoded = base64_decode($encrypted);
    $nonce = mb_substr($decoded, 0, \Sodium\CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
    $ciphertext = mb_substr($decoded, \Sodium\CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');

    return \Sodium\crypto_secretbox_open(
        $ciphertext,
        $nonce,
        $key
    );
}    

然后进行测试:

<?php
// This refers to the previous code block.
require "safeCrypto.php"; 

// Do this once then store it somehow:
$key = \Sodium\randombytes_buf(\Sodium\CRYPTO_SECRETBOX_KEYBYTES);
$message = 'We are all living in a yellow submarine';

$ciphertext = safeEncrypt($message, $key);
$plaintext = safeDecrypt($ciphertext, $key);

var_dump($ciphertext);
var_dump($plaintext);

这可用于您将数据传递给客户端的任何情况(例如,用于没有服务器端存储的 session 的加密 cookie、加密的 URL 参数等),并且具有相当高的确定性,最终用户无法破译或可靠地篡改它。

自从 libsodium is cross-platform ,这也使得与 PHP 进行通信变得更容易,例如Java 小程序或 native 移动应用程序。


注意:如果您特别需要将由 libsodium 提供支持的加密 cookie 添加到您的应用程序中,我的雇主 Paragon Initiative Enterprises正在开发一个名为 Halite 的库这一切都会为您完成。

关于PHP AES 加密/解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3422759/

相关文章:

c# - CryptDeriveKey 算法名称

php - 在php Mysql中插入urdu数据出错

php - 如何使用 Selenium 2 驱动程序在 Mink 中设置页面加载超时?

php - 我如何使用 PDO 和 memcached 设计缓存系统?

windows - 如何从中间解密密码?

c - DES 加密和密码模式

ssl - 如何传递 SSL_CTX 使用的 EC 命名曲线列表?

ruby - Ruby 中的 DES ECB

security - 安全性:此身份验证/加密方案有多脆弱?

php - Doctrine2 - FindOneBy 外键