QCA

publickeyexample.cpp
1/*
2 Copyright (C) 2003 Justin Karneges <justin@affinix.com>
3 Copyright (C) 2005 Brad Hards <bradh@frogmouth.net>
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23#include <QtCrypto>
24
25#include <QCoreApplication>
26
27#include <iostream>
28
29#ifdef QT_STATICPLUGIN
30#include "import_plugins.h"
31#endif
32
33int main(int argc, char **argv)
34{
35 // the Initializer object sets things up, and
36 // also does cleanup when it goes out of scope
38
39 QCoreApplication app(argc, argv);
40
41 // We need to ensure that we have certificate handling support
42 if (!QCA::isSupported("cert")) {
43 std::cout << "Sorry, no PKI certificate support" << std::endl;
44 return 1;
45 }
46
47 // Read in a private key
48 QCA::PrivateKey privKey;
49 QCA::ConvertResult convRes;
50 QCA::SecureArray passPhrase = "start";
51 privKey = QCA::PrivateKey::fromPEMFile(QStringLiteral("Userkey.pem"), passPhrase, &convRes);
52 if (convRes != QCA::ConvertGood) {
53 std::cout << "Sorry, could not import Private Key" << std::endl;
54 return 1;
55 }
56
57 // Read in a matching public key cert
58 // you could also build this using the fromPEMFile() method
59 QCA::Certificate pubCert(QStringLiteral("User.pem"));
60 if (pubCert.isNull()) {
61 std::cout << "Sorry, could not import public key certificate" << std::endl;
62 return 1;
63 }
64 // We are building the certificate into a SecureMessageKey object, via a
65 // CertificateChain
66 QCA::SecureMessageKey secMsgKey;
68 chain += pubCert;
69 secMsgKey.setX509CertificateChain(chain);
70
71 // build up a SecureMessage object, based on our public key certificate
72 QCA::CMS cms;
73 QCA::SecureMessage msg(&cms);
74 msg.setRecipient(secMsgKey);
75
76 // Some plain text - we use the first command line argument if provided
77 QByteArray plainText = (argc >= 2) ? argv[1] : "What do ya want for nuthin'";
78
79 // Now use the SecureMessage object to encrypt the plain text.
80 msg.startEncrypt();
81 msg.update(plainText);
82 msg.end();
83 // I think it is reasonable to wait for 1 second for this
84 msg.waitForFinished(1000);
85
86 // check to see if it worked
87 if (!msg.success()) {
88 std::cout << "Error encrypting: " << msg.errorCode() << std::endl;
89 return 1;
90 }
91
92 // get the result
93 QCA::SecureArray cipherText = msg.read();
94 QCA::Base64 enc;
95 std::cout << plainText.data() << " encrypts to (in base 64): ";
96 std::cout << qPrintable(enc.arrayToString(cipherText)) << std::endl;
97
98 // Show we can decrypt it with the private key
99 if (!privKey.canDecrypt()) {
100 std::cout << "Private key cannot be used to decrypt" << std::endl;
101 return 1;
102 }
103 QCA::SecureArray plainTextResult;
104 if (0 == privKey.decrypt(cipherText, &plainTextResult, QCA::EME_PKCS1_OAEP)) {
105 std::cout << "Decryption process failed" << std::endl;
106 return 1;
107 }
108
109 std::cout << qPrintable(enc.arrayToString(cipherText));
110 std::cout << " (in base 64) decrypts to: ";
111 std::cout << plainTextResult.data() << std::endl;
112
113 return 0;
114}
Base64 encoding / decoding
Cryptographic Message Syntax messaging system.
A chain of related Certificates.
Definition qca_cert.h:1226
Public Key (X.509) certificate.
Definition qca_cert.h:857
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
Generic private key.
bool decrypt(const SecureArray &in, SecureArray *out, EncryptionAlgorithm alg)
Decrypt the message.
bool canDecrypt() const
Test if this key can be used for decryption.
static PrivateKey fromPEMFile(const QString &fileName, const SecureArray &passphrase=SecureArray(), ConvertResult *result=nullptr, const QString &provider=QString())
Import the key in Privacy Enhanced Mail (PEM) format from a file.
Secure array of bytes.
Definition qca_tools.h:317
char * data()
Pointer to the data in the secure array.
Key for SecureMessage system.
void setX509CertificateChain(const CertificateChain &c)
Set the public key part of this X.509 key.
Class representing a secure message.
QString arrayToString(const MemoryRegion &a)
Process an array in the "forward" direction, returning a QString.
void init(KXmlGuiWindow *window, KGameDifficulty *difficulty=nullptr)
@ EME_PKCS1_OAEP
Optimal asymmetric encryption padding (PKCS#1, Version 2.0)
QCA_EXPORT bool isSupported(const char *features, const QString &provider=QString())
Test if a capability (algorithm) is available.
ConvertResult
Return value from a format conversion.
@ ConvertGood
Conversion succeeded, results should be valid.
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:56 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.