VC6使用Crypto++库执行对称加密运算时异常报错的解决办法
使用Crypto++库时运行VC6编译出来的程序,在运行到对称密码算法时会出现异常报错。
使用对称密码算法时,参考的代码是Crypto++ Wiki的代码,下载页面:http://www.cryptopp.com/wiki/Hash_Functions,在页面最下面有Sample Programs,里面执行密码运行时是这条语句:
// Encryption CryptoPP::StringSource( PlainText, true, new CryptoPP::StreamTransformationFilter( Encryptor, new CryptoPP::StringSink( CipherText ) ) // StreamTransformationFilter ); // StringSource
我改动之后使用ArraySource和ArrarySink,代码如下:
// Encryption CryptoPP::ArraySource( btPlain, size, true, new CryptoPP::StreamTransformationFilter( Encryptor, new CryptoPP::ArraySink( btCypher, sizeof(btCypher) ) ) // StreamTransformationFilter ); // StringSource
在VS2008编译运行,运行过程正常,没有任何异常报错。但使用VC6编译后,运行时会出现异常报错,跟踪到代码是Crypto++的filters.cpp的第550行:
if (!allowAuthenticatedSymmetricCipher && dynamic_cast<AuthenticatedSymmetricCipher *>(&c) != 0) throw InvalidArgument("StreamTransformationFilter: please use AuthenticatedEncryptionFilter and AuthenticatedDecryptionFilter for AuthenticatedSymmetricCipher");
所以才抛出的异常。最快解决这个问题的办法就是,加参数,使得变量“allowAuthenticatedSymmetricCipher”为true:
CryptoPP::ArraySource( buf, size, true, new CryptoPP::StreamTransformationFilter( Encryptor, new CryptoPP::ArraySink( btCypher, sizeof(btCypher) ), CryptoPP::BlockPaddingSchemeDef::DEFAULT_PADDING, true ) // StreamTransformationFilter ); // StringSource
修改完成后,VC6编译生成,运行就没有异常报错啦。