poco的pki部分做的真烂,很多算法根本没有实现,不建议在生产环境中使用。
//============================================================================
// Name : testpoco.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include
#include
#include
#include
using namespace std;
#include
using namespace std;
using namespace Poco::Crypto;
typedef std::vector ByteVec;
int aes() {
Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(
CipherKey("aes256"));
std::string in("hongrui xing!");
std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64);
std::string result = pCipher->decryptString(out, Cipher::ENC_BASE64);
std::cout << "加密后:" << out << std::endl;
std::cout << "解密后:" << result << std::endl;
return 0;
}
int aes2() {
unsigned char i[] = { 219, 157, 180, 130, 179, 204, 171, 242, 251, 112, 209,
173, 100, 219, 141, 179 };
unsigned char k[] = { 49, 17, 199, 69, 242, 178, 153, 25, 129, 86, 8, 125,
151, 171, 20, 162, 4, 247, 64, 221, 42, 57, 80, 69, 48, 164, 8, 34,
44, 24, 9, 90 };
ByteVec key = ByteVec(k, k + sizeof(k) / sizeof(unsigned char));
ByteVec iv = ByteVec(i, i + sizeof(i) / sizeof(unsigned char));
//CipherFactory& cf = CipherFactory::defaultFactory;
CipherFactory& cf = CipherFactory::defaultFactory();
Cipher* c = cf.createCipher(CipherKey("aes256", key, iv));
string plain = "Ahmed";
string crypted = c->encryptString(plain, Cipher::ENC_BASE64);
string decrypted = c->decryptString(crypted, Cipher::ENC_BASE64);
cout << plain << endl << crypted << decrypted;
return 0;
}
int des() {
unsigned char i[] = { 219, 157, 180, 130, 179, 204, 171, 242, 251, 112, 209,
173, 100, 219, 141, 179 };
unsigned char k[] = { 49, 17, 199, 69, 242, 178, 153, 25, 129, 86, 8, 125,
151, 171, 20, 162, 4, 247, 64, 221, 42, 57, 80, 69, 48, 164, 8, 34,
44, 24, 9, 90 };
ByteVec key = ByteVec(k, k + sizeof(k) / sizeof(unsigned char));
ByteVec iv = ByteVec(i, i + sizeof(i) / sizeof(unsigned char));
CipherFactory& cf = CipherFactory::defaultFactory();
Cipher* c = cf.createCipher(CipherKey("des", key, iv));
string plain = "Ahmed";
string crypted = c->encryptString(plain, Cipher::ENC_BASE64);
string decrypted = c->decryptString(crypted, Cipher::ENC_BASE64);
cout << plain << endl << crypted << decrypted;
return 0;
}
int main() {
aes();
aes2();
des();
}
好像没有实现3des,而且没有实现,出现
terminate called after throwing an instance of 'Poco::NotFoundException'
what(): Not found
原因是
An uncaught exception is—by definition—not caught anywhere.
If an exception cannot be handled, the C++ exception mechanism will call std::terminate() (see include header ), which will call a customizable termination handler. On your platform, the standard termination handler prints the output of std::exception::what() (which Poco's exceptions inherit from). Unfortunately, the way Poco's exceptions are designed, this will not contain any useful information.
There are multiple ways an exception cannot be handled:
-
No suitable catch() handler is found and the unwinding mechanism exits main(). You can try wrapping your main() code in try...catch to print the exception's displayText().
-
A function exits with an exception that does not match its exception specification (... functionname(...) throw(...)). This will call std::unexpected() which in turn will call std::terminate() (by default).
-
An exception is thrown from within a destructor that is called during the unwinding process of another exception. Never throw exceptions in destructors!
-
An exception is thrown while trying to create the original exception object. Never throw exceptions in custom exception classes!
When using Poco threads and a thread is terminated by an unhandled exception, Poco will invoke its internal ErrorHandler and the program will not exit, so I doubt that this is a threading issue.
阅读(4462) | 评论(0) | 转发(0) |