在Poco Crypto中设置加密填充

时间:2016-05-13 12:32:11

标签: poco-libraries

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

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中我有这个选项:

EVP_CIPHER_CTX *ctx;

EVP_CIPHER_CTX_set_padding(ctx, 0)

1 个答案:

答案 0 :(得分:1)

默认启用填充。

来自... 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. ... reset_index的评论,默认情况下会加密加密操作。

createEncryptor()

如果您想更改填充选项,则应覆盖creatoreDecryptor()中的Poco::Crypto::Cipherclass 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&); }; ,如下所示

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);
}

然后你的功能应该如下

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc]init]; 
numberFormatter.locale = [NSLocale currentLocale]; 
numberFormatter.numberStyle = NSNumberFormatterDecimalStyle; 
numberFormatter.usesGroupingSeparator = YES;

NSString* str = [NSString StringWithFormat:@"%@", 
[numberFormatter stringForObjectValue:dd]];