SOL9 Sample: RSASignatureKey
|
1 Screenshot
2 Source code
/*
* RSASignatureKey.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2009/03/05
#include <sol/crypt/RSACryptServiceProvider.h>
#include <sol/crypt/RSASignatureKey.h>
#include <sol/crypt/MessageDigest.h>
#include <sol/crypt/Signature.h>
void cryptError()
{
_tprintf(_T("%s\n"), CryptError::getMessage(GetLastError()));
}
void _tmain(int argc, TCHAR** argv)
{
try {
RSACryptServiceProvider csp;
{
printf("RSASignatureKey:\n");
RSASignatureKey signatureKey(csp);
printf("\n1. Try to get(export) publicKey from signatureKey\n");
Bytes pubKey;
if (signatureKey.getPublicKey(pubKey) == NO_ERROR) {
printf("2. OK, signatureKey.getPublicKey()\n");
pubKey.dump();
} else {
printf("3. Failed, signatureKey.getPublicKey()\n");
cryptError();
}
printf("\n4. Try to export privateKey from signatureKey\n");
Bytes priKey;
if (signatureKey.getPrivateKey(priKey) == NO_ERROR) {
printf("5. OK, signatureKey.getPrivateKey()\n");
priKey.dump();
} else {
printf("6. Failed in signatureKey.getPrivateKey\n");
cryptError();
}
printf("\n7. Try to import the publicKey from exported publicKey data\n");
CryptKey impPublicKey;
if (impPublicKey.importKey(csp.getProviderHandle(),
pubKey.getData(), pubKey.getSize()) == NO_ERROR) {
printf("8. OK, imppublickKey.importKey()\n");
} else {
printf("9. Failed in impPublicKey.importKey()\n");
cryptError();
}
printf("\n10. Try to import the privateKey from exported privateKey data\n");
CryptKey impPrivateKey;
if (impPrivateKey.importKey(csp.getProviderHandle(),
priKey.getData(), priKey.getSize()) == NO_ERROR) {
printf("11. OK, impPrivateKey.importKey()\n");
} else {
printf("12. Failed in impPrivateKey.importKey()\n");
cryptError();
}
Signature signature(csp, CALG_MD5);
const char* message = "Hello world! This is a SOL9 RSASignatureKey class sample";
printf("\n13. Try to hash string=[%s]\n", message);
if (signature.hashString(message) == NO_ERROR) {
printf("14. OK, signature.hashString()\n");
} else {
printf("15. Failed in signature.hashString()\n");
cryptError();
}
printf("\n16. Try to sign by signature\n");
Bytes signedBytes;
if (signature.sign(signedBytes) ==NO_ERROR) {
printf("17. OK, signature.sign()\n");
signedBytes.dump();
} else {
printf("18. Failed in signature.sign()\n");
cryptError();
}
//
printf("\n19. Try to verify a signature by importedPublicKey\n");
if (signature.verify(impPublicKey.getKeyHandle(), signedBytes) == NO_ERROR) {
printf("20. OK signature.verify()\n");
} else {
printf("21. Failed in signature.verify()\n");
cryptError();
}
}
} catch (Exception& ex) {
printf("Exception = %s\n", ex.getErrorMessage());
}
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.