00001 /* 00002 Copyright (C) 2003 Justin Karneges <justin@affinix.com> 00003 Copyright (C) 2005-2006 Brad Hards <bradh@frogmouth.net> 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00019 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00021 */ 00022 00023 // QtCrypto has the declarations for all of QCA 00024 #include <QtCrypto> 00025 #include <stdio.h> 00026 00027 #include <QCoreApplication> 00028 00029 int main(int argc, char **argv) 00030 { 00031 QCoreApplication app(argc, argv); 00032 00033 // the Initializer object sets things up, and 00034 // also does cleanup when it goes out of scope 00035 QCA::Initializer init; 00036 00037 // we use the first argument if provided, or 00038 // use "hello" if no arguments 00039 QCA::SecureArray arg = (argc >= 2) ? argv[1] : "hello"; 00040 00041 // AES128 testing 00042 if(!QCA::isSupported("aes128-cbc-pkcs7")) 00043 printf("AES128-CBC not supported!\n"); 00044 else { 00045 // Create a random key - you'd probably use one from another 00046 // source in a real application 00047 QCA::SymmetricKey key(16); 00048 00049 // Create a random initialisation vector - you need this 00050 // value to decrypt the resulting cipher text, but it 00051 // need not be kept secret (unlike the key). 00052 QCA::InitializationVector iv(16); 00053 00054 // create a 128 bit AES cipher object using Cipher Block Chaining (CBC) mode 00055 QCA::Cipher cipher(QString("aes128"),QCA::Cipher::CBC, 00056 // use Default padding, which is equivalent to PKCS7 for CBC 00057 QCA::Cipher::DefaultPadding, 00058 // this object will encrypt 00059 QCA::Encode, 00060 key, iv); 00061 00062 // we use the cipher object to encrypt the argument we passed in 00063 // the result of that is returned - note that if there is less than 00064 // 16 bytes (1 block), then nothing will be returned - it is buffered 00065 // update() can be called as many times as required. 00066 QCA::SecureArray u = cipher.update(arg); 00067 00068 // We need to check if that update() call worked. 00069 if (!cipher.ok()) { 00070 printf("Update failed\n"); 00071 } 00072 // output the results of that stage 00073 printf("AES128 encryption of %s is [%s]\n", 00074 arg.data(), 00075 qPrintable(QCA::arrayToHex(u.toByteArray())) ); 00076 00077 00078 // Because we are using PKCS7 padding, we need to output the final (padded) block 00079 // Note that we should always call final() even with no padding, to clean up 00080 QCA::SecureArray f = cipher.final(); 00081 00082 // Check if the final() call worked 00083 if (!cipher.ok()) { 00084 printf("Final failed\n"); 00085 } 00086 // and output the resulting block. The ciphertext is the results of update() 00087 // and the result of final() 00088 printf("Final block for AES128 encryption is [0x%s]\n", qPrintable(QCA::arrayToHex(f.toByteArray())) ); 00089 00090 // re-use the Cipher t decrypt. We need to use the same key and 00091 // initialisation vector as in the encryption. 00092 cipher.setup( QCA::Decode, key, iv ); 00093 00094 // Build a single cipher text array. You could also call update() with 00095 // each block as you receive it, if that is more useful. 00096 QCA::SecureArray cipherText = u.append(f); 00097 00098 // take that cipher text, and decrypt it 00099 QCA::SecureArray plainText = cipher.update(cipherText); 00100 00101 // check if the update() call worked 00102 if (!cipher.ok()) { 00103 printf("Update failed\n"); 00104 } 00105 00106 // output results 00107 printf("Decryption using AES128 of [0x%s] is %s\n", 00108 qPrintable(QCA::arrayToHex(cipherText.toByteArray())), plainText.data()); 00109 00110 // Again we need to call final(), to get the last block (with its padding removed) 00111 plainText = cipher.final(); 00112 00113 // check if the final() call worked 00114 if (!cipher.ok()) { 00115 printf("Final failed\n"); 00116 } 00117 00118 // output results 00119 printf("Final decryption block using AES128 is %s\n", plainText.data()); 00120 // instead of update() and final(), you can do the whole thing 00121 // in one step, using process() 00122 printf("One step decryption using AES128: %s\n", 00123 QCA::SecureArray(cipher.process(cipherText)).data() ); 00124 00125 } 00126 00127 return 0; 00128 } 00129