C++ C+中的AES 256加密+;和Qt 5

C++ C+中的AES 256加密+;和Qt 5,c++,qt,encryption,aes,C++,Qt,Encryption,Aes,我有一个加密的Java代码,如下所示 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = null; if(isIVUsedForCrypto) { cipher = Cipher.getInsta

我有一个加密的Java代码,如下所示

private static byte[] encrypt(byte[] raw, byte[] clear) throws 
   Exception {  
    SecretKeySpec skeySpec = new SecretKeySpec(raw,  "AES");  
    Cipher cipher = null;

    if(isIVUsedForCrypto) {
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(IV));  
    }
    else 
    {
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);  
    }
    byte[] encrypted = cipher.doFinal(clear);  
    return encrypted;  
}  

 public static byte[] toByte(String hexString) { 

    int len = hexString.length()/2;  
    byte[] result = new byte[len];  
    try{
    for (int i = 0; i < len; i++) { 
        result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2),16).byteValue();  
    }
    }catch (Exception e) {

    }
    return result;  
}  

public static String toHex(byte[] buf) {  
    if (buf == null)  
        return "";  
    StringBuffer result = new StringBuffer(2*buf.length);  
    for (int i = 0; i < buf.length; i++) {  
        appendHex(result, buf[i]);  
    }  
    return result.toString();  
}  
private final static String HEX = "0123456789ABCDEF";  
private static void appendHex(StringBuffer sb, byte b) {  
    sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));  
}  

我需要为上面的方法(在java中)编写C++等价物。我不知道密码的C++类,希望有人提供一个例子来显示同样的。< /P> 提前谢谢

编辑

我的原始密钥将是十六进制的->729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A6860FC7697E727E

我的最终加密密码是-->812DCE870D82E93DB62CDA66AAF37FB2

这在Java中是可行的,但我需要一个类似的C++解决方案。

试试这个:

#include <crypto++/aes.h>
#include <crypto++/modes.h>
#include <crypto++/filters.h>
#include <crypto++/hex.h>
#include <crypto++/sha.h>
#include <crypto++/md5.h>

QString Foo::decrypt(const QString &password)
{
    string plain;
    string encrypted = password.toStdString();
    // Hex decode symmetric key:
    HexDecoder decoder;
    decoder.Put( (byte *)PRIVATE_KEY,32*2 );
    decoder.MessageEnd();
    word64 size = decoder.MaxRetrievable();
    char *decodedKey = new char[size];
    decoder.Get((byte *)decodedKey, size);
    // Generate Cipher, Key, and CBC
    byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
    StringSource( reinterpret_cast<const char *>(decodedKey), true,
                  new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
    memset( iv, 0x00, AES::BLOCKSIZE );
    try {
        CBC_Mode<AES>::Decryption Decryptor
        ( key, sizeof(key), iv );
        StringSource( encrypted, true,
                      new HexDecoder(new StreamTransformationFilter( Decryptor,
                                     new StringSink( plain ) ) ) );
    }
    catch (Exception &e) { // ...
    }
    catch (...) { // ...
    }
    return QString::fromStdString(plain);
}

QString Foo::encrypt(const QString &password)
{
    string plain = password.toStdString();
    string ciphertext;
    // Hex decode symmetric key:
    HexDecoder decoder;
    decoder.Put( (byte *)PRIVATE_KEY, 32*2 );
    decoder.MessageEnd();
    word64 size = decoder.MaxRetrievable();
    char *decodedKey = new char[size];
    decoder.Get((byte *)decodedKey, size);
    // Generate Cipher, Key, and CBC
    byte key[ AES::MAX_KEYLENGTH ], iv[ AES::BLOCKSIZE ];
    StringSource( reinterpret_cast<const char *>(decodedKey), true,
                  new HashFilter(*(new SHA256), new ArraySink(key, AES::MAX_KEYLENGTH)) );
    memset( iv, 0x00, AES::BLOCKSIZE );
    CBC_Mode<AES>::Encryption Encryptor( key, sizeof(key), iv );
    StringSource( plain, true, new StreamTransformationFilter( Encryptor,
                  new HexEncoder(new StringSink( ciphertext ) ) ) );
    return QString::fromStdString(ciphertext);
}
就我个人而言,我不喜欢在源代码中透露私钥。所以我将在命令行中将其传递给编译器:

g++ -DPRIVATE_KEY \"\"\"123...\"\"\" ...

其中PRIVATE_KEY是纯文本形式的私钥。如果你在十六进制编码中有密钥,只要删除<代码>十六进制解码对称密钥< /C>步骤> < /p>“我不知道密码的C++类”。从下面留下的评论判断,您至少知道一个库。你有没有调查过为什么它看起来不符合你的要求?你还尝试过哪些其他图书馆?是否有某种原因,它必须是C++,而不能简单地是OpenSSL API栈?或者一个完成工作的瘦包装?我试过Botan,但它没有给我一个十六进制加密字符串作为输出Yes会尝试现在让你知道…谢谢这个例子上面的代码使用任何库吗?或者我可以和Botan一起用?好的。目前我只需要使用AES-256算法进行加密。如果我给出一个十六进制的原始键,它会工作吗?是否有任何代码行需要从encrypt方法中删除。检查问题中的编辑如果您计划将密钥作为十六进制编码字符串,请删除两条注释之间的行(
HEX decode symmetric key
和下一条注释)好的,我知道了,它是用于AES 256的吗?我不想用盐
//...
#define PRIVATE_KEY "729308A8E815F6A46EB3A8AE6D5463CA7B64A0E2E11BC26A68106FC7697E727E37011"
QString encrypted = Foo::encryptPassword("test");
// use encrypted
g++ -DPRIVATE_KEY \"\"\"123...\"\"\" ...