为什么加密++;你认为我的信息比它们更大吗? 我使用C++的代码>加密+++/代码>库,用128的公钥和我的私钥从磁盘加密。但是,当我调用函数时,程序终止,并显示错误消息:

为什么加密++;你认为我的信息比它们更大吗? 我使用C++的代码>加密+++/代码>库,用128的公钥和我的私钥从磁盘加密。但是,当我调用函数时,程序终止,并显示错误消息:,c++,crypto++,C++,Crypto++,在抛出的实例后调用terminate “CryptoPP::InvalidArgument”what():RSA/OAEP-MGF1(SHA-1):消息 256的长度超过了此公钥的最大值214 我不知道为什么它认为我的消息是256位的,而它们显然是128位的。下面是我的代码: std::string encryptWithBothKeys(std::string key_name1, std::string key_name2, std::string plain){ std::stri

在抛出的实例后调用terminate “CryptoPP::InvalidArgument”what():RSA/OAEP-MGF1(SHA-1):消息 256的长度超过了此公钥的最大值214

我不知道为什么它认为我的消息是256位的,而它们显然是128位的。下面是我的代码:

std::string encryptWithBothKeys(std::string key_name1, std::string key_name2, std::string plain){
    std::string cipher1, cipher2;
    AutoSeededRandomPool rng;

    RSA::PublicKey pub_key;
    RSA::PrivateKey priv_key;
    loadPublicKey(key_name1, pub_key);
    loadPrivateKey(key_name2, priv_key);

    RSAES_OAEP_SHA_Encryptor e_pub(pub_key);
    RSAES_OAEP_SHA_Encryptor e_priv(priv_key);

    StringSource ss1(plain, true,
      new PK_EncryptorFilter(rng, e_pub,
        new StringSink(cipher1)
      )
    );

    StringSource ss2(cipher1, true,
      new PK_EncryptorFilter(rng, e_priv,
        new StringSink(cipher2)
      )
    );

    return cipher2;
}

void load(const std::string& filename, BufferedTransformation& bt){
  FileSource file(filename.c_str(), true /*pumpAll*/);

  file.TransferTo(bt);
  bt.MessageEnd();
}

void loadPrivateKey(const std::string& filename, PrivateKey& key){
  ByteQueue queue;

  load(filename, queue);
  key.Load(queue);  
}

void loadPublicKey(const std::string& filename, PublicKey& key){
  ByteQueue queue;

  load(filename, queue);
  key.Load(queue);  
}
在主函数中,我这样称呼它:

std::string encrypted_msg = encryptWithBothKeys("their.pub", "my.key", "0123456789ABCDEF");
有什么建议吗

编辑:第一次加密后的密文大小为256。我想我需要弄清楚为什么它会变大

我不知道为什么它认为我的消息是256位的,而它们显然是128位的。下面是我的代码:

std::string encryptWithBothKeys(std::string key_name1, std::string key_name2, std::string plain){
    std::string cipher1, cipher2;
    AutoSeededRandomPool rng;

    RSA::PublicKey pub_key;
    RSA::PrivateKey priv_key;
    loadPublicKey(key_name1, pub_key);
    loadPrivateKey(key_name2, priv_key);

    RSAES_OAEP_SHA_Encryptor e_pub(pub_key);
    RSAES_OAEP_SHA_Encryptor e_priv(priv_key);

    StringSource ss1(plain, true,
      new PK_EncryptorFilter(rng, e_pub,
        new StringSink(cipher1)
      )
    );

    StringSource ss2(cipher1, true,
      new PK_EncryptorFilter(rng, e_priv,
        new StringSink(cipher2)
      )
    );

    return cipher2;
}

void load(const std::string& filename, BufferedTransformation& bt){
  FileSource file(filename.c_str(), true /*pumpAll*/);

  file.TransferTo(bt);
  bt.MessageEnd();
}

void loadPrivateKey(const std::string& filename, PrivateKey& key){
  ByteQueue queue;

  load(filename, queue);
  key.Load(queue);  
}

void loadPublicKey(const std::string& filename, PublicKey& key){
  ByteQueue queue;

  load(filename, queue);
  key.Load(queue);  
}
这些是字节,不是位

你用的是什么尺寸的?我猜是2048位的密钥,256字节,剩下≈214字节由于OAEP填充

所以问题变成了,
plain
cipher1
的大小是多少:

StringSource ss1(plain, true,
  new PK_EncryptorFilter(rng, e_pub,
    new StringSink(cipher1)
  )
);
我猜
plain1
足够小了。但是,当对其进行填充和加密时,它会扩展到256字节,如
cipher1
。因此,执行此操作时,消息
cipher1
太大:

StringSource ss2(cipher1, true,
  new PK_EncryptorFilter(rng, e_priv,
    new StringSink(cipher2)
  )
);
此外,上面是否应该使用
PK_DecryptorFilter
?您确定要再次加密吗?如果是这样,那么为什么要使用
e_priv


使用私钥加密不是有效的加密操作。如果您想要“使用私钥加密”的东西,它通常意味着您想要一个带恢复的概率签名方案(PSSR)。Crypto++提供了两种密码。

检查常规/字符集设置。如果是Unicode,则将其更改为MBCS。可能是因为设置了Unicode标志,所以字符串的大小增加了一倍..我不知道你的意思是什么?我在谷歌上搜索累了,但它似乎是某种Windows/Visual Studio设置。我在Linux上使用g++道歉-与MS CryptoAPI和Crypto++混淆了我们如何相应地缩小它?