什么是openssl iv,为什么我需要一个键和一个iv? [英] What is an openssl iv, and why do I need a key and an iv?

查看:366
本文介绍了什么是openssl iv,为什么我需要一个键和一个iv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用以下脚本来加密和解密某些数据.我之所以使用它,是因为我当前的加密在我们的新服务器上不起作用.我们目前正在使用mcrypt,所以我想更改为openssl.

I am about to use the following script to encrypt and decrypt some data. I am using it because my current encryption does not work on our new server. We are currently using mcrypt so I want to change to openssl.

在我们的数据库中,我们使用aes加密,该加密使用128位密钥,因此我知道密钥是什么,但是我不知道openssl iv是什么?以及为什么我需要一个键和一个iv.

In our database we use aes encryption which uses a 128bit key so I know what a key is, but I do not know what an openssl iv is? And why would I need a key and an iv.

我要使用的代码是这个,我在网站上找到了它,因为我不太了解加密.

The code I am about to use is this, which I found on a website because I don't understand encryption very well.

很显然,我将对其进行修改,以便将密钥保留在其他位置.

Obviously I will modify it so that the key is kept somewhere else.

function encrypt_decrypt($action, $string) {
    $output = false;

    $encrypt_method = "AES-256-CBC";
    $secret_key = 'This is my secret key';
    $secret_iv = 'This is my secret iv';

    // hash
    $key = hash('sha256', $secret_key);

    // iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
    $iv = substr(hash('sha256', $secret_iv), 0, 16);

    if( $action == 'encrypt' ) {
        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
        $output = base64_encode($output);
    }
    else if( $action == 'decrypt' ){
        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
    }

    return $output;
}

$plain_txt = "This is my plain text";
echo "Plain Text = $plain_txt\n";

$encrypted_txt = encrypt_decrypt('encrypt', $plain_txt);
echo "Encrypted Text = $encrypted_txt\n";

$decrypted_txt = encrypt_decrypt('decrypt', $encrypted_txt);
echo "Decrypted Text = $decrypted_txt\n";

if( $plain_txt === $decrypted_txt ) echo "SUCCESS";
else echo "FAILED";

echo "\n";

推荐答案

初始化矢量是使CBC(密码块链接)模式下的AES工作的一部分-IV并非OpenSSL独有.CBC通过将前一个块与当前块进行异或来工作.第一个块没有上一个块,因此IV可以达到这个目的.

The Initialization Vector is part of what makes AES in CBC (Cipher Block Chaining) mode work - IVs are not unique to OpenSSL. CBC works by XORing the previous block with the current block. The very first block has no previous block, so the IV serves that purpose.

为什么这样做是必要的,所以需要对分组密码的工作原理有一些了解.没有这种链接和IV,我们剩下的是称为ECB或电子密码簿的AES模式.欧洲央行的弱点是可以选择明文攻击,还有许多其他问题.

Why this is necessary requires a bit of understanding of how block ciphers work. Without this chaining and IV, we're left with a mode of AES called ECB, or Electronic Code Book. ECB has weaknesses that allow a chosen plaintext attack, among many other problems.

我建议花点时间在CBC初始化向量的最佳实践上.错误使用它们会削弱AES的整体安全性.简短的解释是:

I would recommend spending a bit of time with best practices for CBC initialization vectors. Using them incorrectly can weaken the overall security of AES. The short explanation is:

  • IV应该是随机的,并且由CSPRNG生成.
  • IV不应重复使用.也就是说,请勿使用相同的IV加密明文"A"和明文"B".每个记录都应该有自己的IV.
  • IV不是像钥匙一样的秘密.它可以与密文一起以明文形式存储.

还请记住,此建议仅适用于AES-CBC.如果您曾经研究过其他AES模式(例如GCM),则此方法不适用.

Also keep in mind that this advice only applies to AES-CBC. If you ever investigate other modes of AES, such as GCM, this does not apply.

这篇关于什么是openssl iv,为什么我需要一个键和一个iv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆