1 int main(const std::vector<std::string>& args) 2 { 3 /*TO DO*/ 4 Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256")); 5 6 std::string in("I love karen!"); 7 std::string out = pCipher->encryptString(in, Cipher::ENC_BASE64); 8 std::string result = pCipher->decryptString(out, Cipher::ENC_BASE64); 9 std::cout<<"加密後:"<<out<<std::endl; 10 std::cout<<"解密後:"<<result<<std::endl; 11 12 return Application::EXIT_OK; 13 }
一、第4行代碼,建立一個256位的AES加密算法;html
注:http://zh.wikipedia.org/wiki/%E9%AB%98%E7%BA%A7%E5%8A%A0%E5%AF%86%E6%A0%87%E5%87%86 AES維基百科算法
二、第七、8行代碼,分別實現加密與解密過程。post
三、在加密與解密過程當中,代碼中使用了BASE64編碼方式,另外在POCO::Crypto中還有如下編碼方式:編碼
enum Encoding
/// Transport encoding to use for encryptString() and decryptString().
{
ENC_NONE = 0x00, /// Plain binary output
ENC_BASE64 = 0x01, /// Base64-encoded output
ENC_BINHEX = 0x02, /// BinHex-encoded output
ENC_BASE64_NO_LF = 0x81, /// Base64-encoded output, no linefeeds
ENC_BINHEX_NO_LF = 0x82, /// BinHex-encoded output, no linefeeds
};加密
1 int main(const std::vector<std::string>& args) 2 { 3 /*TO DO*/ 4 Cipher::Ptr pCipher = CipherFactory::defaultFactory().createCipher(CipherKey("aes256")); 5 6 static const std::string SECRET_MESSAGE = "This is a secret message. Don't tell anyone."; 7 8 std::stringstream sstr; 9 EncryptingOutputStream encryptor(sstr, *pCipher); 10 encryptor << SECRET_MESSAGE; 11 encryptor.close(); 12 13 DecryptingInputStream decryptor(sstr, *pCipher); 14 std::string iresult; 15 Poco::StreamCopier::copyToString(decryptor, iresult); 16 17 std::cout<<"解密後:"<<iresult<<std::endl; 18 return Application::EXIT_OK; 19 }
一、第6行,爲原文;url
二、第9行,加密;第13行,解密;spa
三、第15行,獲得解密結果,就是原文了。code