首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Poco密码中设置加密填充

在Poco密码中设置加密填充
EN

Stack Overflow用户
提问于 2016-05-13 12:32:10
回答 1查看 965关注 0票数 0

是否有可能在Poco中使用AES128设置加密填充?我找不到任何选择。

代码语言:javascript
复制
std::string Crypto::Encrypt(const std::string &input, const std::string &key)
{
    Poco::Crypto::Cipher::ByteVec iv { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    Poco::Crypto::Cipher::ByteVec key2 {key.begin(), key.end()};

    Poco::Crypto::Cipher::Ptr pCipher = Poco::Crypto::CipherFactory::defaultFactory()
        .createCipher(Poco::Crypto::CipherKey("aes128", key2, iv));

    std::string output = pCipher->encryptString(input);

    return std::move(output);
}

在简单的OpenSSL中,我有以下选项:

代码语言:javascript
复制
EVP_CIPHER_CTX *ctx;

EVP_CIPHER_CTX_set_padding(ctx, 0)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-28 09:09:34

默认情况下,已启用填充。

Crypto/include/Poco/Crypto/CryptoTransform.hhttps://pocoproject.org/docs/Poco.Crypto.CryptoTransform.html#230中的注释中,默认情况下加密操作是填充的。

代码语言:javascript
复制
...
virtual int setPadding(int padding);
    /// Enables or disables padding. By default encryption operations are padded using standard block 
    /// padding and the padding is checked and removed when decrypting. If the padding parameter is zero then 
    /// no padding is performed, the total amount of data encrypted or decrypted must then be a multiple of 
    /// the block size or an error will occur.
...

如果您想更改填充选项,您应该覆盖createEncryptor()creatoreDecryptor()Poco::Crypto::Cipher中,如下所示

代码语言:javascript
复制
class CipherWithPadding : public Poco::Crypto::Cipher
{
public:
    // by default no padding, for more information refer to CryptoTransform::setPadding
    CipherWithPadding(Poco::Crypto::Cipher::Ptr cipher_impl, int padding = 0) 
    : cipher_(cipher_impl)
    , padding_(padding)
    {
    }

    virtual ~CipherWithPadding()
    {
    }

    virtual const std::string& name() const
    {
        return cipher_->name();
    }

    virtual Poco::Crypto::CryptoTransform* createEncryptor() override
    {
        auto ptransform = cipher_->createEncryptor();
        if (ptransform)
            ptransform->setPadding(padding_);
        return ptransform;
    }

    virtual Poco::Crypto::CryptoTransform* createDecryptor() override
    {
        auto ptransform = cipher_->createDecryptor();
        if (ptransform)
            ptransform->setPadding(padding_);
        return ptransform;
    }

protected:
    int padding_;
    Poco::Crypto::Cipher::Ptr cipher_;

protected:
    CipherWithPadding();

private:
    CipherWithPadding(const CipherWithPadding&);
    CipherWithPadding& operator= (const CipherWithPadding&);
};

那么您的函数应该如下所示

代码语言:javascript
复制
std::string Crypto::Encrypt(const std::string &input, const std::string &key)
{
  Poco::Crypto::Cipher::ByteVec iv { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  Poco::Crypto::Cipher::ByteVec key2 {key.begin(), key.end()};

  CipherWithPadding cipher(Poco::Crypto::CipherFactory::defaultFactory()
      .createCipher(Poco::Crypto::CipherKey("aes128", key2, iv)));

  std::string output = cipher.encryptString(input);

  return std::move(output);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37210164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档