SOL9 Sample: RSAExchangeKey
|
1 Screenshot
2 Source code
/*
* RSAExchangeKey.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2009/03/05
#include <sol/crypt/RSACryptServiceProvider.h>
#include <sol/crypt/RSAExchangeKey.h>
void cryptError()
{
_tprintf(_T("%s\n"), CryptError::getMessage(GetLastError()) );
exit(1);
}
void _tmain(int argc, TCHAR** argv)
{
try {
RSACryptServiceProvider csp;
{
printf("RSAExchangeKey:\n");
RSAExchangeKey exchangeKey(csp);
const size_t size = 64;
size_t dsize = size;
const size_t bsize = size+ 128;
unsigned char buffer[bsize];
memset(buffer, 0, sizeof(buffer));
strcpy_s((char*)buffer, SizeOf(buffer), "This is a sample text. Hello world!");
printf("\n1. Try to encrypt by exchangeKey SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
if (exchangeKey.encrypt(buffer, &dsize, bsize, TRUE) == NO_ERROR) {
printf("2. OK exchangKey.encrypt\n");
Bytes enc(buffer, dsize);
enc.dump();
} else {
cryptError();
}
printf("\n3. Try to decrypt by exchangeKey SIZE=%d \n", dsize);
if (exchangeKey.decrypt(buffer, &dsize, TRUE) == NO_ERROR) {
printf("4. OK exchangeKey.decrypt\n");
printf("SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
} else {
cryptError();
}
printf("\n5. Try to get(export) publicKey from exchangeKey\n");
Bytes pubKey;
if (exchangeKey.getPublicKey(pubKey) == NO_ERROR) {
printf("6. OK, exchangeKey.getPublicKey:");
pubKey.dump();
} else {
cryptError();
}
printf("\n7. Try to export privateKey from exchangeKey\n");
Bytes priKey;
if (exchangeKey.getPrivateKey(priKey) == NO_ERROR) {
printf("8. OK, getPrivateKey from an exchangeKey");
priKey.dump();
} else {
cryptError();
}
printf("\n9. Try to import publicKey from exported publicKey data\n");
CryptKey impPublicKey;
if (impPublicKey.importKey(csp.getProviderHandle(),
pubKey.getData(), pubKey.getSize()) == NO_ERROR) {
printf("10. OK, impoorted a publickKey\n");
} else {
cryptError();
}
printf("\n11. Try to import privateKey from exported privateKey data\n");
CryptKey impPrivateKey;
if (impPrivateKey.importKey(csp.getProviderHandle(),
priKey.getData(), priKey.getSize()) == NO_ERROR) {
printf("12. OK, imported a privateKey\n");
} else {
cryptError();
}
// Encrypt data by ImportedPublicKey and decrypt it by ImportedPrivateKey
memset(buffer, 0, sizeof(buffer));
strcpy_s((char*)buffer, SizeOf(buffer), "Encrypt by importedPublicKey and decrypt by importedPrivateKey!");
printf("\n13. Try to encrypt by imported PublicKey SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
if (impPublicKey.encrypt(buffer, &dsize, bsize, TRUE) == NO_ERROR) {
printf("14. OK, encrypted by importedPublicKey\n");
Bytes enc(buffer, dsize);
enc.dump();
} else {
cryptError();
}
printf("\n15. Try to decrypt by imported PrivateKey SIZE=%d\n", dsize);//, (char*)buffer);
if (impPrivateKey.decrypt(buffer, &dsize, TRUE) == NO_ERROR) {
printf("16. OK, SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
} else {
cryptError();
}
// Encrypt data by ImportedPrivateKey and decrypt it by ImportedPublicKey
memset(buffer, 0, sizeof(buffer));
strcpy_s((char*)buffer, SizeOf(buffer), "Encrypt by importedPrivateKey and decrypt by importedPublicKey!");
printf("\n17. Try to encrypt by imported PrivateKey SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
if (impPrivateKey.encrypt(buffer, &dsize, bsize, TRUE) == NO_ERROR) {
printf("18. OK, encrypted by importedPrivateKey\n");
Bytes enc(buffer, dsize);
enc.dump();
} else {
cryptError();
}
printf("\n19. Try to decrypt by importedPublicKey SIZE=%d\n", dsize);//, (char*)buffer);
if (impPublicKey.decrypt(buffer, &dsize, TRUE) == NO_ERROR) {
printf("20. OK, SIZE=%d DATA=[%s]\n", dsize, (char*)buffer);
} else {
cryptError();
}
}
} catch (Exception& ex) {
printf("Exception = %s\n", ex.getErrorMessage());
}
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.