bool CWSKey::Load(char *filename)
{
    int version = 0;
    char junk1[1234] = {0};
    char junk2[3456] = {0};
    FILE *file = 0;
    // Decrypt
    byte key[AES::DEFAULT_KEYLENGTH];
    byte iv[AES::BLOCKSIZE];

    // Write Key File
    file = fopen(filename, "rb");
    if (file == 0) {
        WSK_LOG("Failed To Open The Key File");
        return false;
    }

    // Read Encrypted Key Data
    fread(&version, sizeof(version), 1, file);      // read version
    if (version != WSK_VERSION) {                                   // Check Version
        WSK_LOG("Wrong Version Of Key");
        fclose(file);
        return false;
    }
    fread(junk1, sizeof(junk1), 1, file);           // read junk
    fread(&m_key, sizeof(m_key), 1, file);          // read Key Data
    fread(key, sizeof(key), 1, file);                       // AES KEY
    fread(iv, sizeof(iv), 1, file);                         // AES iv
    fread(junk2, sizeof(junk2), 1, file);           // read junk

    // Close file
    fclose(file);

    // Decrypt
    CFB_Mode<AES>::Decryption cfbDecryption(key, AES::DEFAULT_KEYLENGTH, iv);
    cfbDecryption.ProcessData((byte*)junk1, (byte*)junk1, sizeof(junk1));
    cfbDecryption.ProcessData((byte*)&m_key, (byte*)&m_key, sizeof(m_key));
    cfbDecryption.ProcessData((byte*)junk2, (byte*)junk2, sizeof(junk2));

    return true;
}
Exemple #2
0
void AESDecryptFile( const char *keyFilename,
                     const char *messageFilename,
                     const char *outputFilename ) {

   // hex decoding halvs length
    int bufferLength = 256;
    char *keyBuffer = new char[ bufferLength + 1 ];

    // add string termination
    keyBuffer[ bufferLength ] = '\0';
    

    // read in the key
    FileSource keyFile( keyFilename, true,
                        new HexDecoder(
                            new ArraySink( (unsigned char*)keyBuffer,
                                           bufferLength ) ) );

    // iv is all-zero
    char *iv = new char[ 32 ];
    for( int i=0; i<32; i++ ) {
        iv[i] = 0;
        }
    
    AESEncryption aesEncryption( (byte *)keyBuffer, 32 );
    CFB_Mode_ExternalCipher::Decryption cfbDecryption( aesEncryption,
                                                       (byte *)iv );


    FILE *messageFile = fopen( messageFilename, "r" );
    FILE *outputFile = fopen( outputFilename, "wb" );
    
	if( messageFile != NULL && outputFile != NULL ) {

        char *oneCharInputString = new char[2];
        oneCharInputString[1] = '\0';

        char *oneCharOutputString = new char[2];
        oneCharOutputString[1] = '\0';

        
		int charReadAsInt = getc( messageFile );
		
		while( charReadAsInt != EOF ) {
			
			unsigned char charReadAsChar = (unsigned char)charReadAsInt;

            oneCharInputString[0] = charReadAsChar;
           
            cfbDecryption.ProcessString( (byte *)oneCharOutputString,
                                         (byte *)oneCharInputString, 1 );

            // FIXME:  need to hex-encode output
            fprintf( outputFile, "%s", oneCharOutputString );

            
			charReadAsInt = getc( messageFile );
			}
        
		fclose( messageFile );
        fclose( outputFile );

        delete [] oneCharOutputString;
        delete [] oneCharInputString;
        }

    delete [] keyBuffer;
    delete [] iv;
    }