0
在努力進行加密使用加密+大文件,我下面ecrypting使用Java類初始化 - 更新 - 最後in Crypto++,加密+初始化,更新,最終得到加密輸出
演示代碼似乎工作,每次的將被調用時,輸出字節打印到屏幕:
encoder.Put(buffer, ready);
但我找不到反正讓所有的密文的輸出。例如
'H' -> print 01 to screen
'E' -> print A9 to screen
'L' -> print 5J to screen
'L' -> print 13 to screen
'O' -> print 3d to screen ...
但我不能檢索完整的結果字節數組 01A95J133d ...
與使用初始化更新決賽加密大型文件的問題in here但看起來不提供了一個工作解決方案。
這是一個完整的演示代碼:
enum { ENCRYPT_MODE = 1, DECRYPT_MODE = 2 };
struct JavaAlgorithmParameter
{
JavaAlgorithmParameter()
: key(NULL), ksize(0), iv(NULL), vsize(0) {}
const byte* key;
size_t ksize;
const byte* iv;
size_t vsize;
};
/////////////////////////
/////////////////////////
class JavaCipher
{
public:
static JavaCipher* getInstance(const std::string& transformation);
void init(int opmode, const JavaAlgorithmParameter& params);
size_t update(const byte* in, size_t isize, byte* out, size_t osize);
size_t final(byte* out, size_t osize);
std::string getAlgorithm() const;
protected:
JavaCipher(const std::string& transformation);
private:
std::string m_transformation;
member_ptr<SymmetricCipher> m_cipher;
member_ptr<StreamTransformationFilter> m_filter;
};
/////////////////////////
/////////////////////////
JavaCipher* JavaCipher::getInstance(const std::string& transformation)
{
return new JavaCipher(transformation);
}
JavaCipher::JavaCipher(const std::string& transformation)
: m_transformation(transformation) { }
std::string JavaCipher::getAlgorithm() const
{
return m_transformation;
}
/////////////////////////
/////////////////////////
size_t JavaCipher::final(byte* out, size_t osize)
{
m_filter.get()->MessageEnd();
if (!out || !osize || !m_filter.get()->AnyRetrievable())
return 0;
size_t t = CryptoPP::STDMIN(m_filter.get()->MaxRetrievable(), (word64)osize);
t = m_filter.get()->Get(out, t);
return t;
}
/////////////////////////
/////////////////////////
size_t JavaCipher::update(const byte* in, size_t isize, byte* out, size_t osize)
{
if (in && isize)
m_filter.get()->Put(in, isize);
if (!out || !osize || !m_filter.get()->AnyRetrievable())
return 0;
size_t t = STDMIN(m_filter.get()->MaxRetrievable(), (word64)osize);
t = m_filter.get()->Get(out, t);
return t;
}
/////////////////////////
/////////////////////////
void JavaCipher::init(int opmode, const JavaAlgorithmParameter& params)
{
if (m_transformation == "AES/ECB/PKCSPadding" && opmode == ENCRYPT_MODE)
{
m_cipher.reset(new ECB_Mode<AES>::Encryption);
m_cipher.get()->SetKey(params.key, params.ksize);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::PKCS_PADDING));
}
else if (m_transformation == "AES/ECB/PKCSPadding" && opmode == DECRYPT_MODE)
{
m_cipher.reset(new ECB_Mode<AES>::Decryption);
m_cipher.get()->SetKey(params.key, params.ksize);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::PKCS_PADDING));
}
else if (m_transformation == "AES/CBC/PKCSPadding" && opmode == ENCRYPT_MODE)
{
m_cipher.reset(new CBC_Mode<AES>::Encryption);
m_cipher.get()->SetKeyWithIV(params.key, params.ksize, params.iv);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::PKCS_PADDING));
}
else if (m_transformation == "AES/CBC/PKCSPadding" && opmode == DECRYPT_MODE)
{
m_cipher.reset(new CBC_Mode<AES>::Decryption);
m_cipher.get()->SetKeyWithIV(params.key, params.ksize, params.iv);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::PKCS_PADDING));
}
else if (m_transformation == "AES/CTR/NoPadding" && opmode == ENCRYPT_MODE)
{
m_cipher.reset(new CTR_Mode<AES>::Encryption);
m_cipher.get()->SetKeyWithIV(params.key, params.ksize, params.iv);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::NO_PADDING));
}
else if (m_transformation == "AES/CTR/NoPadding" && opmode == DECRYPT_MODE)
{
m_cipher.reset(new CTR_Mode<AES>::Decryption);
m_cipher.get()->SetKeyWithIV(params.key, params.ksize, params.iv);
m_filter.reset(new StreamTransformationFilter(*m_cipher.get(), NULL, BlockPaddingSchemeDef::NO_PADDING));
}
else
throw NotImplemented(m_transformation + " is not implemented");
}
/////////////////////////
/////////////////////////
int main(int argc, char* argv[])
{
try
{
byte key[32], iv[16];
OS_GenerateRandomBlock(false, key, COUNTOF(key));
OS_GenerateRandomBlock(false, iv, COUNTOF(iv));
HexEncoder encoder(new FileSink(cout));
JavaAlgorithmParameter params;
params.key = key;
params.ksize = COUNTOF(key);
params.iv = iv;
params.vsize = COUNTOF(iv);
//JavaCipher* cipher = JavaCipher::getInstance("AES/CTR/NoPadding");
JavaCipher* cipher = JavaCipher::getInstance("AES/CBC/PKCSPadding");
cipher->init(ENCRYPT_MODE, params);
cout << "Algorithm: " << cipher->getAlgorithm() << endl;
cout << "Key: ";
encoder.Put(key, COUNTOF(key));
cout << endl;
cout << "IV: ";
encoder.Put(iv, COUNTOF(iv));
cout << endl;
char * allText = FileUtil::readAllByte("1MB.txt");
long len = strlen(allText);
byte buffer[64];
size_t ready = 0;
for (unsigned int i = 0; i <= len; i++)
{
byte b = allText[i];
//cout << "Put 0x";
encoder.Put(b);
cout << endl;
ready = cipher->update(&b, 1, buffer, COUNTOF(buffer));
if (ready)
{
//cout << "Get: ";
encoder.Put(buffer, ready);
cout << endl;
}
}
ready = cipher->final(NULL, 0);
if (ready)
{
//cout << "Final: ";
encoder.Put(buffer, ready);
cout << endl;
}
ready = cipher->final(buffer, COUNTOF(buffer));
if (ready)
{
//cout << "Final: ";
encoder.Put(buffer, ready);
cout << endl;
}
delete cipher;
getchar();
}
catch (const Exception& ex)
{
cerr << ex.what() << endl;
}
return 0;
}
你似乎是一個C#開發。用C#而不是C++來完成你的任務是不可能的? .Net似乎擁有你需要的一切。 – jww
對不起:)我在C#中的所有這些都是在oneday。但是我認爲像Crypto ++這樣的多線程C++庫會更好更快。 – Andiana
我很抱歉,我一直試圖提供的幫助沒有幫助。也許你應該在繼續使用Crypto ++庫之前瞭解更多關於C++的知識。 – jww