00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtCrypto>
00025
00026 #include <QCoreApplication>
00027
00028 #include <iostream>
00029
00030
00031 int main(int argc, char** argv)
00032 {
00033
00034
00035 QCA::Initializer init;
00036
00037 QCoreApplication app(argc, argv);
00038
00039
00040 if ( !QCA::isSupported( "cert" ) ) {
00041 std::cout << "Sorry, no PKI certificate support" << std::endl;
00042 return 1;
00043 }
00044
00045
00046 QCA::PrivateKey privKey;
00047 QCA::ConvertResult convRes;
00048 QCA::SecureArray passPhrase = "start";
00049 privKey = QCA::PrivateKey::fromPEMFile( "Userkey.pem", passPhrase, &convRes );
00050 if ( convRes != QCA::ConvertGood ) {
00051 std::cout << "Sorry, could not import Private Key" << std::endl;
00052 return 1;
00053 }
00054
00055
00056
00057 QCA::Certificate pubCert( "User.pem" );
00058 if ( pubCert.isNull() ) {
00059 std::cout << "Sorry, could not import public key certificate" << std::endl;
00060 return 1;
00061 }
00062
00063
00064 QCA::SecureMessageKey secMsgKey;
00065 QCA::CertificateChain chain;
00066 chain += pubCert;
00067 secMsgKey.setX509CertificateChain( chain );
00068
00069
00070 QCA::CMS cms;
00071 QCA::SecureMessage msg(&cms);
00072 msg.setRecipient(secMsgKey);
00073
00074
00075 QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
00076
00077
00078 msg.startEncrypt();
00079 msg.update(plainText);
00080 msg.end();
00081
00082 msg.waitForFinished(1000);
00083
00084
00085 if(!msg.success())
00086 {
00087 std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
00088 return 1;
00089 }
00090
00091
00092 QCA::SecureArray cipherText = msg.read();
00093 QCA::Base64 enc;
00094 std::cout << plainText.data() << " encrypts to (in base 64): ";
00095 std::cout << qPrintable( enc.arrayToString( cipherText ) ) << std::endl;
00096
00097
00098 if ( !privKey.canDecrypt() ) {
00099 std::cout << "Private key cannot be used to decrypt" << std::endl;
00100 return 1;
00101 }
00102 QCA::SecureArray plainTextResult;
00103 if ( 0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP ) ) {
00104 std::cout << "Decryption process failed" << std::endl;
00105 return 1;
00106 }
00107
00108 std::cout << qPrintable( enc.arrayToString( cipherText ) );
00109 std::cout << " (in base 64) decrypts to: ";
00110 std::cout << plainTextResult.data() << std::endl;
00111
00112 return 0;
00113 }
00114