「Qtの応用 - AES」の版間の差分

ナビゲーションに移動 検索に移動
183行目: 183行目:
  // This invocation corresponds to Java's Cipher.getInstance("AES/CFB8/NoPadding")
  // This invocation corresponds to Java's Cipher.getInstance("AES/CFB8/NoPadding")
  auto dec = new CFB_Mode<AES>::Decryption(aes_key, sizeof(aes_key), iv, 1);
  auto dec = new CFB_Mode<AES>::Decryption(aes_key, sizeof(aes_key), iv, 1);
</syntaxhighlight>
<br><br>
== Qt AESライブラリ ==
以下の例では、Qt AESライブラリを使用して、鍵長256[bit]のCBCモードで暗号化している。<br>
<syntaxhighlight lang="c++">
#include <QCryptographicHash>
#include "QAESEncryption.h"
void Encrypt(const QString &plainText)
{
    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);
    QString key("your-string-key");  // AES256は32[byte]の鍵長
    QString iv("your-IV-vector");    // AESのブロックサイズは16[byte]の固定長のため、初期化ベクトルも16[byte]
    QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
    QByteArray hashIV  = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
    QByteArray encodeText = encryption.encode(plainText.toLocal8Bit(), hashKey, hashIV);
    return;
}
</syntaxhighlight>
<br>
以下の例では、Qt AESライブラリを使用して、鍵長256[bit]のCBCモードで復号している。<br>
<syntaxhighlight lang="c++">
#include <QCryptographicHash>
#include "QAESEncryption.h"
void Decrypt(const QByteArray &encodeText)
{
    QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CBC);
    QString key("your-string-key");  // 暗号化と同じ鍵
    QString iv("your-IV-vector");    // 暗号化と同じ初期化ベクトル
    QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
    QByteArray hashIV  = QCryptographicHash::hash(iv.toLocal8Bit(), QCryptographicHash::Md5);
    QByteArray decodeText = encryption.decode(encodeText, hashKey, hashIV);
    QString decodedString = QString(encryption.removePadding(decodeText));
    return;
}
  </syntaxhighlight>
  </syntaxhighlight>
<br><br>
<br><br>

案内メニュー