首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图解密2密钥三重DES的无效块填充

试图解密2密钥三重DES的无效块填充
EN

Stack Overflow用户
提问于 2012-11-06 06:11:18
回答 1查看 2K关注 0票数 1

我正试图用crypto++在C++中实现2键三重DES。

我的实现是基于位于crypto++ wiki的这里中的代码。

wiki上的代码构建正确;在运行示例程序时,我可以看到它正在正确地加密和解密。

就我的执行而言,我试图做以下工作:

  1. 用户可以运行"desimp.exe加密test.txt“。该程序将加密test.txt,然后输出一个名为test.txt.des的加密文件。这看起来很正常。
  2. 用户可以运行"desimp.exe解密test.txt.des“,程序将解密test.txt.des并将解密文本输出到"decrypted.txt”文件中--我无法让它工作。我得到的错误是"StreamTransformationFilter:无效的PKCS #7块填充找到的“。

我相信我可能需要将iv中的数据保存到加密时的文件中。这是正确的吗?我尝试过这样做,并且我认为我能够正确地将iv保存到一个文件中--但我认为,为了在iv中读取用于解密,需要将它作为一个8字节的数组读取。当我试图保存iv时,test.txt.iv的文件大小为21字节。如果这是正确的方法,我不知道如何进行。如果这是错误的方法,我想知道我需要做什么不同。以下是代码:

代码语言:javascript
复制
#ifndef CRYPTOPP_DLL_ONLY
#define CRYPTOPP_DEFAULT_NO_DLL
#endif

#include "dll.h"
#include "rc6.h"
#include <stdio.h>  
#include <string.h>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <streambuf>

USING_NAMESPACE(CryptoPP)
USING_NAMESPACE(std)

#ifdef CRYPTOPP_IMPORTS
static PNew s_pNew = NULL;
static PDelete s_pDelete = NULL;
#endif
#ifdef CRYPTOPP_DLL_ONLY

int __cdecl main(int argc, char *argv[])
{

    AutoSeededRandomPool prng;
    SecByteBlock key(DES_EDE2::DEFAULT_KEYLENGTH);
    prng.GenerateBlock(key, key.size());
    byte iv[DES_EDE2::BLOCKSIZE];
    prng.GenerateBlock(iv, sizeof(iv));
    string plain = "CBC Mode Test";
    string cipher, encoded, recovered;


    char *fileName = argv[1];
    char *runMode = argv[2];
    char *ivFile = argv[3];

    cout << "ARGUMENT 1: " << fileName << endl;
    cout << "ARGUMENT 2: " << runMode << endl;

    string fileNameString(fileName);
    string encryptedFileNameString = fileNameString + ".des";//add .des to the filename of the encrypted file once it's generated
    string ivString = fileNameString + ".iv";//iv file
    string runModeString(runMode);

    if (runModeString == "encrypt")
    {
        ifstream t(fileName);
        string str((std::istreambuf_iterator<char>(t)),
        istreambuf_iterator<char>());
        try
        {
            cout << "plain text: " << str << endl;

            CBC_Mode< DES_EDE2 >::Encryption e;
            e.SetKeyWithIV(key, key.size(), iv);

            // The StreamTransformationFilter adds padding
            //  as required. ECB and CBC Mode must be padded
            //  to the block size of the cipher.
            StringSource ss1(str, true, 
                new StreamTransformationFilter(e,
                    new StringSink(cipher)
                ) // StreamTransformationFilter      
            ); // StringSource
        }
        catch(const CryptoPP::Exception& e)
        {
            cerr << e.what() << endl;
            exit(1);
        }
        // Pretty print
        StringSource ss2(cipher, true,
            new HexEncoder(
                new StringSink(encoded)
            ) // HexEncoder
        ); // StringSource

        cout << "cipher text: " << encoded << endl;//"encoded" is just the pretty print version of the ciphertext.
        ofstream fout(encryptedFileNameString); 
        fout << cipher; 
        fout.close();//outputs/saves the encrypted file
        cout << "Encrypted file was saved to local path as " << encryptedFileNameString << endl;

        ofstream fout2(ivString); 
        fout2 << iv; 
        fout2.close();
        cout << "iv was saved to local path as " << ivString << endl;
    }

    if (runModeString == "decrypt")// USER WANTS TO DECRYPT A FILE
        {           
            ifstream t2(fileName);
            string str2((istreambuf_iterator<char>(t2)),istreambuf_iterator<char>());
            cipher = str2;

        try
        {
            CBC_Mode< DES_EDE2 >::Decryption d;
            d.SetKeyWithIV(key, key.size(), iv);
            // The StreamTransformationFilter removes
            //  padding as required.
            StringSource ss3(cipher, true, 
                new StreamTransformationFilter(d,
                    new StringSink(recovered)
                ) // StreamTransformationFilter
            ); // StringSource

            cout << "recovered text: " << recovered << endl;
        }
        catch(const CryptoPP::Exception& e)
            {
                cerr << e.what() << endl;
                exit(1);
            }
        }
        return 0;
    }//end main

    extern "C" __declspec(dllexport) void __cdecl SetNewAndDeleteFromCryptoPP(PNew pNew, PDelete pDelete, PSetNewHandler pSetNewHandler)
    {
        s_pNew = pNew;
        s_pDelete = pDelete;
    }

    void * __cdecl operator new (size_t size)
    {
        return s_pNew(size);
    }

    void __cdecl operator delete (void * p)
    {
        s_pDelete(p);
    }

    #endif
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-06 07:04:53

我认为您需要以std::ios_base::binary模式打开加密文件(用于读写);否则,I/O库将破坏类似行尾的序列。

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

https://stackoverflow.com/questions/13245385

复制
相关文章

相似问题

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