| RSA BSAFE Crypto-C |
Cryptographic Components for C |
| Search |
/* $Id: dintroex.c,v 1.6 2004/12/03 02:08:42 sparki Exp $ */ /* * Copyright (C) 1998-2004 RSA Security Inc. * * This file shall only be used to demonstrate how to interface to an * RSA Security Inc. licensed development product. * * You have a royalty-free right to use, reproduce and distribute this * demonstration file, provided that you agree that RSA Security Inc. * has no warranty, implied or otherwise, or liability for this * demonstration file (including any modified version). This software * is provided "as is" without warranties or representations of any * kind. RSA Security disclaims all conditions and warranties, statutory * and otherwise, both express and implied, with respect to the software, * its quality and performance, including but not limited to, all * implied warranties of merchantability, fitness for a particular * purpose, title and noninfringement of third party rights. Without * limiting the foregoing, RSA Security does not warrant that the * software is error-free or that errors in the product will be * corrected. You agree that RSA Security shall not be liable for any * direct, indirect, incidental, special, consequential, punitive or * other damages whatsoever resulting from your use of this software * or any modified version. * * */ /* This program demonstrates data encryption and decryption using AI_RC4. */ #include "bsafe.h" #include "demoutil.h" /* in samples/common/include */ B_ALGORITHM_METHOD *DEMO_ALGORITHM_CHOOSER[] = { &AM_RC4_ENCRYPT, &AM_RC4_DECRYPT, (B_ALGORITHM_METHOD *)NULL_PTR /* This will fix a problem that the IA64 compiler finds when * * seeing a short chooser list */ #ifdef IA64_FORCE_LARGE IA64_FORCE_LARGE #endif }; #ifdef CRYPTOC_APP #define MAIN dintroexMain #else #define MAIN main #endif int MAIN(int argc, char *argv[]) { B_KEY_OBJ rc4Key = (B_KEY_OBJ)NULL_PTR; B_ALGORITHM_OBJ rc4Encrypter = (B_ALGORITHM_OBJ)NULL_PTR; B_ALGORITHM_OBJ rc4Decrypter = (B_ALGORITHM_OBJ)NULL_PTR; /* The RC4 key is hard coded in this example. In a real application, use a random number generator to produce the key. */ unsigned char rc4KeyData[10] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10 }; unsigned int rc4KeyDataLen = sizeof (rc4KeyData); ITEM rc4KeyItem = {NULL, 0}; unsigned char *dataToEncrypt = (unsigned char *)"Encrypt this sentence."; unsigned int dataToEncryptLen; unsigned char *encryptedData = NULL_PTR; unsigned int outputLenUpdate, outputLenFinal; unsigned int encryptedDataLen; unsigned char *decryptedData = NULL_PTR; unsigned int decryptedLenUpdate, decryptedLenFinal; unsigned int decryptedDataLen; int status; do { /* The RSA_* demo code utilities are described in common/include/demoutil.h. This procedure simply checks the command-line arguments for input or output options. */ if ((status = RSA_SetOptions (argc, argv)) != 0) break; RSA_PrintMessage ("RC4 algorithm: Encryption phase\n"); RSA_PrintMessage ("================================\n"); dataToEncryptLen = T_strlen ((char *)dataToEncrypt) + 1; RSA_PrintBuf ("Data To Encrypt", dataToEncrypt, dataToEncryptLen); /* Create an algorithm object. */ if ((status = B_CreateAlgorithmObject (&rc4Encrypter)) != 0) break; /* Set the algorithm to a type that does rc4 encryption. In this sample, AI_RC4 is used. */ if ((status = B_SetAlgorithmInfo (rc4Encrypter, AI_RC4, NULL_PTR)) != 0) break; /* Create a key object. */ if ((status = B_CreateKeyObject (&rc4Key)) != 0) break; /* Set the key object with the 10-byte key. */ rc4KeyItem.data = rc4KeyData; rc4KeyItem.len = rc4KeyDataLen; if ((status = B_SetKeyInfo (rc4Key, KI_Item, (POINTER)&rc4KeyItem)) != 0) break; /* Init */ if ((status = B_EncryptInit (rc4Encrypter, rc4Key, DEMO_ALGORITHM_CHOOSER, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Update */ /* Since RC4 is a stream cipher, the size of the encrypted data will be the same as the size of the data to encrypt. */ encryptedDataLen = dataToEncryptLen; encryptedData = T_malloc (encryptedDataLen); if (encryptedData == NULL_PTR) { status = RSA_DEMO_E_ALLOC; break; } if ((status = B_EncryptUpdate (rc4Encrypter, encryptedData, &outputLenUpdate, encryptedDataLen, dataToEncrypt, dataToEncryptLen, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Final */ if ((status = B_EncryptFinal (rc4Encrypter, encryptedData + outputLenUpdate, &outputLenFinal, dataToEncryptLen - outputLenUpdate, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; encryptedDataLen = outputLenUpdate + outputLenFinal; RSA_PrintBuf ("Encrypted Data", encryptedData, encryptedDataLen); RSA_PrintMessage ("\nRC4 algorithm: Decryption phase\n"); RSA_PrintMessage ("================================\n"); if ((status = B_CreateAlgorithmObject (&rc4Decrypter)) != 0) break; /* Use the same AI as for encryption */ if ((status = B_SetAlgorithmInfo (rc4Decrypter, AI_RC4, NULL_PTR)) != 0) break; /* Use the same key as for encryption */ if ((status = B_DecryptInit (rc4Decrypter, rc4Key, DEMO_ALGORITHM_CHOOSER, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; /* Set the buffer that will take the decrypted data to be the same size as the encrypted data's buffer. */ decryptedDataLen = encryptedDataLen; decryptedData = T_malloc (decryptedDataLen); if (decryptedData == NULL_PTR) { status = RSA_DEMO_E_ALLOC; break; } if ((status = B_DecryptUpdate (rc4Decrypter, decryptedData, &decryptedLenUpdate, decryptedDataLen, encryptedData, encryptedDataLen, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; if ((status = B_DecryptFinal (rc4Decrypter, decryptedData + decryptedLenUpdate, &decryptedLenFinal, decryptedDataLen - decryptedLenUpdate, (B_ALGORITHM_OBJ)NULL_PTR, (A_SURRENDER_CTX *)NULL_PTR)) != 0) break; decryptedDataLen = decryptedLenUpdate + decryptedLenFinal; RSA_PrintBuf ("Decrypted Data", decryptedData, decryptedDataLen); if ((decryptedDataLen == dataToEncryptLen) && (T_memcmp (dataToEncrypt, decryptedData, decryptedDataLen)) == 0) { RSA_PrintMessage ("Success! "); RSA_PrintMessage ("The decrypted data matches the original data.\n"); } else { RSA_PrintMessage ("The decrypted data does not match the original data."); status = RSA_DEMO_E_INFO_DOES_NOT_VERIFY; } } while (0); if (status != 0) RSA_PrintError ("dintroex", status); /* Done with the key and algorithm objects, so destroy them */ B_DestroyKeyObject (&rc4Key); B_DestroyAlgorithmObject (&rc4Encrypter); B_DestroyAlgorithmObject (&rc4Decrypter); /* Free up any memory allocated, save it to a file or print it out first if you need to save it. */ if (encryptedData != NULL_PTR) { T_free (encryptedData); encryptedData = NULL_PTR; } if (decryptedData != NULL_PTR) { T_free (decryptedData); decryptedData = NULL_PTR; } return (status); } /* end main */