00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <QtCrypto>
00024 #include <QCoreApplication>
00025
00026 #include <iostream>
00027
00028 int main(int argc, char **argv)
00029 {
00030
00031
00032 QCA::Initializer init;
00033
00034 QCoreApplication app(argc, argv);
00035
00036
00037
00038 QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello";
00039
00040
00041
00042 if(!QCA::isSupported("pkey") ||
00043 !QCA::PKey::supportedIOTypes().contains(QCA::PKey::RSA))
00044 std::cout << "RSA not supported!\n";
00045 else {
00046
00047
00048
00049
00050
00051
00052
00053
00054 QCA::PrivateKey seckey = QCA::KeyGenerator().createRSA(1024);
00055 if(seckey.isNull()) {
00056 std::cout << "Failed to make private RSA key" << std::endl;
00057 return 1;
00058 }
00059
00060 QCA::PublicKey pubkey = seckey.toPublicKey();
00061
00062
00063 if(!pubkey.canEncrypt()) {
00064 std::cout << "Error: this kind of key cannot encrypt" << std::endl;
00065 return 1;
00066 }
00067
00068
00069
00070 QCA::SecureArray result = pubkey.encrypt(arg, QCA::EME_PKCS1_OAEP);
00071 if(result.isEmpty()) {
00072 std::cout << "Error encrypting" << std::endl;
00073 return 1;
00074 }
00075
00076
00077 QString rstr = QCA::arrayToHex(result.toByteArray());
00078 std::cout << "\"" << arg.data() << "\" encrypted with RSA is \"";
00079 std::cout << qPrintable(rstr) << "\"" << std::endl;
00080
00081
00082
00083
00084 QCA::SecureArray passPhrase = "pass phrase";
00085 seckey.toPEMFile("keyprivate.pem", passPhrase);
00086
00087
00088 QCA::ConvertResult conversionResult;
00089 QCA::PrivateKey privateKey = QCA::PrivateKey::fromPEMFile( "keyprivate.pem",
00090 passPhrase,
00091 &conversionResult);
00092 if (! (QCA::ConvertGood == conversionResult) ) {
00093 std::cout << "Private key read failed" << std::endl;
00094 }
00095
00096
00097
00098 QCA::SecureArray decrypt;
00099 if(0 == privateKey.decrypt(result, &decrypt, QCA::EME_PKCS1_OAEP)) {
00100 std::cout << "Error decrypting.\n";
00101 return 1;
00102 }
00103
00104
00105 std::cout << "\"" << qPrintable(rstr) << "\" decrypted with RSA is \"";
00106 std::cout << decrypt.data() << "\"" << std::endl;
00107
00108
00109
00110 if(!privateKey.canSign()) {
00111 std::cout << "Error: this kind of key cannot sign" << std::endl;
00112 return 1;
00113 }
00114 privateKey.startSign( QCA::EMSA3_MD5 );
00115 privateKey.update( arg );
00116 QByteArray argSig = privateKey.signature();
00117
00118
00119
00120
00121
00122
00123
00124 rstr = QCA::arrayToHex(argSig);
00125 std::cout << "Signature for \"" << arg.data() << "\" using RSA, is ";
00126 std::cout << "\"" << qPrintable( rstr ) << "\"" << std::endl;
00127
00128
00129
00130 if(pubkey.canVerify()) {
00131 pubkey.startVerify( QCA::EMSA3_MD5 );
00132 pubkey.update( arg );
00133 if ( pubkey.validSignature( argSig ) ) {
00134 std::cout << "Signature is valid" << std::endl;
00135 } else {
00136 std::cout << "Bad signature" << std::endl;
00137 }
00138 }
00139
00140
00141
00142 if ( pubkey.canVerify() &&
00143 pubkey.verifyMessage( arg, argSig, QCA::EMSA3_MD5 ) ) {
00144 std::cout << "Signature is valid" << std::endl;
00145 } else {
00146 std::cout << "Signature could not be verified" << std::endl;
00147 }
00148
00149 }
00150
00151 return 0;
00152 }
00153