Let’s make a program that reads in a cipher input and produces a translated outp
ID: 3690478 • Letter: L
Question
Let’s make a program that reads in a cipher input and produces a translated output
The program’s overall structure is this:
Open two files, one for input “r” and one for output “w”
Read a line from the input file into an input string
Go through character from the input string and put its cipher substitution equivalent into an output string
Output the string to the output file
When finished, the output file should be a readable famous paper
Good news though! I have provided a template that explains thoroughly what to do
What you need to do:
1) Download cipherText.txt to your project directory (or a location you can easily access)
2) Download cipher.txt to your project directory as well. This works off of the Cipher example that was done in section 2.
3) Download “Homework 9 Template.c”
4) Go through the template and fill in all the TODO: comments
Alternatively you can use the template as guidelines and attempt to write your own solution to this problem.
Some of you may find this easier
Explanation / Answer
include #include #include #include #include // Link with the Advapi32.lib file. #pragma comment (lib, "advapi32") #define KEYLENGTH 0x00800000 #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8 bool MyDecryptFile( LPTSTR szSource, LPTSTR szDestination, LPTSTR szPassword); void MyHandleError( LPTSTR psz, int nErrorNumber); int _tmain(int argc, _TCHAR* argv[]) { if(argc < 3) { _tprintf(TEXT("Usage: ") TEXT(" | ")); _tprintf(TEXT(" is optional. ")); _tprintf(TEXT("Press any key to exit.")); _gettch(); return 1; } LPTSTR pszSource = argv[1]; LPTSTR pszDestination = argv[2]; LPTSTR pszPassword = NULL; if(argc >= 4) { pszPassword = argv[3]; } //--------------------------------------------------------------- // Call EncryptFile to do the actual encryption. if(MyDecryptFile(pszSource, pszDestination, pszPassword)) { _tprintf( TEXT("Encryption of the file %s was successful. "), pszSource); _tprintf( TEXT("The encrypted data is in file %s. "), pszDestination); } else { MyHandleError( TEXT("Error encrypting file! "), GetLastError()); } return 0; } //------------------------------------------------------------------- // Code for the function MyDecryptFile called by main. //------------------------------------------------------------------- // Parameters passed are: // pszSource, the name of the input file, an encrypted file. // pszDestination, the name of the output, a plaintext file to be // created. // pszPassword, either NULL if a password is not to be used or the // string that is the password. bool MyDecryptFile( LPTSTR pszSourceFile, LPTSTR pszDestinationFile, LPTSTR pszPassword) { //--------------------------------------------------------------- // Declare and initialize local variables. bool fReturn = false; HANDLE hSourceFile = INVALID_HANDLE_VALUE; HANDLE hDestinationFile = INVALID_HANDLE_VALUE; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL; HCRYPTPROV hCryptProv = NULL; DWORD dwCount; PBYTE pbBuffer = NULL; DWORD dwBlockLen; DWORD dwBufferLen; //--------------------------------------------------------------- // Open the source file. hSourceFile = CreateFile( pszSourceFile, FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(INVALID_HANDLE_VALUE != hSourceFile) { _tprintf( TEXT("The source encrypted file, %s, is open. "), pszSourceFile); } else { MyHandleError( TEXT("Error opening source plaintext file! "), GetLastError()); goto Exit_MyDecryptFile; } //--------------------------------------------------------------- // Open the destination file. hDestinationFile = CreateFile( pszDestinationFile, FILE_WRITE_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(INVALID_HANDLE_VALUE != hDestinationFile) { _tprintf( TEXT("The destination file, %s, is open. "), pszDestinationFile); } else { MyHandleError( TEXT("Error opening destination file! "), GetLastError()); goto Exit_MyDecryptFile; } //--------------------------------------------------------------- // Get the handle to the default provider. if(CryptAcquireContext( &hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) { _tprintf( TEXT("A cryptographic provider has been acquired. ")); } else { MyHandleError( TEXT("Error during CryptAcquireContext! "), GetLastError()); goto Exit_MyDecryptFile; } //--------------------------------------------------------------- // Create the session key. if(!pszPassword || !pszPassword[0]) { //----------------------------------------------------------- // Decrypt the file with the saved session key. DWORD dwKeyBlobLen; PBYTE pbKeyBlob = NULL; // Read the key BLOB length from the source file. if(!ReadFile( hSourceFile, &dwKeyBlobLen, sizeof(DWORD), &dwCount, NULL)) { MyHandleError( TEXT("Error reading key BLOB length! "), GetLastError()); goto Exit_MyDecryptFile; } // Allocate a buffer for the key BLOB. if(!(pbKeyBlob = (PBYTE)malloc(dwKeyBlobLen))) { MyHandleError( TEXT("Memory allocation error. "), E_OUTOFMEMORY); } //----------------------------------------------------------- // Read the key BLOB from the source file. if(!ReadFile( hSourceFile, pbKeyBlob, dwKeyBlobLen, &dwCount, NULL)) { MyHandleError( TEXT("Error reading key BLOB length! "), GetLastError()); goto Exit_MyDecryptFile; } //----------------------------------------------------------- // Import the key BLOB into the CSP. if(!CryptImportKey( hCryptProv, pbKeyBlob, dwKeyBlobLen, 0, 0, &hKey)) { MyHandleError( TEXT("Error during CryptImportKey!/n"), GetLastError()); goto Exit_MyDecryptFile; } if(pbKeyBlob) { free(pbKeyBlob); } } else { //----------------------------------------------------------- // Decrypt the file with a session key derived from a // password. //----------------------------------------------------------- // Create a hash object. if(!CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { MyHandleError( TEXT("Error during CryptCreateHash! "), GetLastError()); goto Exit_MyDecryptFile; } //----------------------------------------------------------- // Hash in the password data. if(!CryptHashData( hHash, (BYTE *)pszPassword, lstrlen(pszPassword), 0)) { MyHandleError( TEXT("Error during CryptHashData! "), GetLastError()); goto Exit_MyDecryptFile; } //----------------------------------------------------------- // Derive a session key from the hash object. if(!CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey)) { MyHandleError( TEXT("Error during CryptDeriveKey! "), GetLastError()) ; goto Exit_MyDecryptFile; } } //--------------------------------------------------------------- // The decryption key is now available, either having been // imported from a BLOB read in from the source file or having // been created by using the password. This point in the program // is not reached if the decryption key is not available. //--------------------------------------------------------------- // Determine the number of bytes to decrypt at a time. // This must be a multiple of ENCRYPT_BLOCK_SIZE. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE; dwBufferLen = dwBlockLen; //--------------------------------------------------------------- // Allocate memory for the file read buffer. if(!(pbBuffer = (PBYTE)malloc(dwBufferLen))) { MyHandleError(TEXT("Out of memory! "), E_OUTOFMEMORY); goto Exit_MyDecryptFile; } //--------------------------------------------------------------- // Decrypt the source file, and write to the destination file. bool fEOF = false; do { //----------------------------------------------------------- // Read up to dwBlockLen bytes from the source file. if(!ReadFile( hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL)) { MyHandleError( TEXT("Error reading from source file! "), GetLastError()); goto Exit_MyDecryptFile; } if(dwCountRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.