src/OpenSSLCipherStream/UTF8OpenSSLCipherInputStream.cpp
/** Read the given file, decrypt it with the key, and write the result
	* to the string variable "result". */
Read the given file, decrypt it with the key, and write the result
to the string variable "result".
Read the given file decrypt it with the key and write the result to the string variable " result " .
void decrypt(const char* filename, std::string &result, const std::string &password) {
		std::basic_ifstream<unsigned char> input_stream(filename, std::ios_base::binary);
		readFixedSizeString(input_stream, strlen(OPEN_SSL_MAGIC_PREFIX));
		std::basic_string<unsigned char> salt = readFixedSizeString(input_stream, PKCS5_SALT_LEN);
		const EVP_CIPHER* cipher = choose_cipher(filename);
		const EVP_MD *dgst = EVP_md5();
		unsigned char key[EVP_MAX_KEY_LENGTH];
		unsigned char iv[EVP_MAX_IV_LENGTH];
		if (!EVP_BytesToKey(cipher, dgst, salt.c_str(),
			reinterpret_cast<const unsigned char *>(password.c_str()),
			password.size(), 1, key, iv))
			throw UnexpectedInputException("UTF8OpenSSLCipherInputStream::decrypt",
				"Error while converting password->key");
		EVP_CIPHER_CTX ctx;
		EVP_CIPHER_CTX_init(&ctx);
		EVP_DecryptInit_ex(&ctx, cipher, NULL, key, iv);
		unsigned char src[SRC_BUF_SIZE];
		unsigned char dst[DST_BUF_SIZE];
		if (static_cast<int>((sizeof(dst)-sizeof(src))/sizeof(unsigned char)) < 
			EVP_CIPHER_block_size(cipher))
			throw InternalInconsistencyException("UTF8OpenSSLCipherInputStream::decrypt",
				"DST_BUF_SIZE is not large enough!");
		while (!input_stream.eof()) {
			input_stream.read(src, sizeof(src)/sizeof(src[0]));
			size_t nb = input_stream.gcount(); 
			if (input_stream.bad())
				throw UnexpectedInputException("UTF8OpenSSLCipherInputStream::decrypt",
					"Error while reading from: ", filename);
			if (nb == 0) break;
			int olen, tlen;
			if (EVP_DecryptUpdate(&ctx, dst, &olen, src, nb) != 1)
				throw UnexpectedInputException("UTF8OpenSSLCipherInputStream::decrypt",
					"Error while decrypting (update): ", filename);
			if (EVP_DecryptFinal_ex(&ctx, dst + olen, & tlen) != 1)
				throw UnexpectedInputException("UTF8OpenSSLCipherInputStream::decrypt",
					"Error while decrypting (final): ", filename);
			result.append(reinterpret_cast<const char*>(dst), olen+tlen);
		}
		EVP_CIPHER_CTX_cleanup (&ctx);
	}
void decrypt ( const char * filename , std :: string & result , const std :: string & password ) { std :: basic_ifstream < unsigned char > input_stream ( filename , std :: ios_base :: binary ) ; readFixedSizeString ( input_stream , strlen ( OPEN_SSL_MAGIC_PREFIX ) ) ; std :: basic_string < unsigned char > salt = readFixedSizeString ( input_stream , PKCS5_SALT_LEN ) ; const EVP_CIPHER * cipher = choose_cipher ( filename ) ; const EVP_MD * dgst = EVP_md5 ( ) ; unsigned char key [ EVP_MAX_KEY_LENGTH ] ; unsigned char iv [ EVP_MAX_IV_LENGTH ] ; if ( ! EVP_BytesToKey ( cipher , dgst , salt . c_str ( ) , reinterpret_cast < const unsigned char * > ( password . c_str ( ) ) , password . size ( ) , 1 , key , iv ) ) throw UnexpectedInputException ( " " , " " ) ; EVP_CIPHER_CTX ctx ; EVP_CIPHER_CTX_init ( & ctx ) ; EVP_DecryptInit_ex ( & ctx , cipher , NULL , key , iv ) ; unsigned char src [ SRC_BUF_SIZE ] ; unsigned char dst [ DST_BUF_SIZE ] ; if ( static_cast < int > ( ( sizeof ( dst ) - sizeof ( src ) ) / sizeof ( unsigned char ) ) < EVP_CIPHER_block_size ( cipher ) ) throw InternalInconsistencyException ( " " , " " ) ; while ( ! input_stream . eof ( ) ) { input_stream . read ( src , sizeof ( src ) / sizeof ( src [ 0 ] ) ) ; size_t nb = input_stream . gcount ( ) ; if ( input_stream . bad ( ) ) throw UnexpectedInputException ( " " , " " , filename ) ; if ( nb == 0 ) break ; int olen , tlen ; if ( EVP_DecryptUpdate ( & ctx , dst , & olen , src , nb ) != 1 ) throw UnexpectedInputException ( " " , " " , filename ) ; if ( EVP_DecryptFinal_ex ( & ctx , dst + olen , & tlen ) != 1 ) throw UnexpectedInputException ( " " , " " , filename ) ; result . append ( reinterpret_cast < const char * > ( dst ) , olen + tlen ) ; } EVP_CIPHER_CTX_cleanup ( & ctx ) ; }
Read the given file, decrypt it with the key, and write the result
 to the string variable "result".
Read the given file decrypt it with the key and write the result to the string variable " result " .
// Open the file.
 // Read the magic prefix and the salt.
 // Choose a cipher to use.
 // Choose a digest to use
 // Convert password to key+iv.
 // Set up the encryption context.
 // Buffers to read from/to.
 // Read in a block of data.
 // number of bytes read
 // Decrypt the data.
 // Append the decrypted data to dst.
