[BEZ] cvičení 4

Odpovědět
Uživatelský avatar
Destroyer
VCKLAN TEAM
Příspěvky: 805
Registrován: čtv 13. srp 2009 13:50:15
Bydliště: Praha 12
Kontaktovat uživatele:

cvičení 4

Příspěvek od Destroyer » stř 08. dub 2015 22:53:00

20150408_160216.jpg

Kód: Vybrat vše

/** 
 * \file ukol4-sifrovani.cpp
 * Code for encrypting a file. 
 * \author Miroslav Vlach <vlachmir@fit.cvut.cz> */
#include <stdio.h>
#include <string>
#include <cstring>
#include <iostream>
#include <openssl/evp.h>
#include <openssl/pem.h>

#define bufferInLen 1024
#define bufferOutLen 1024 + EVP_MAX_IV_LENGTH

using namespace std;

int main(int argc, char* argv[])
{
	FILE *fp, *in, *out;

	if (argc != 3) {
		cout << "Chybne zadani, potreba zadat   jmeno_souboru_k_zasifrovani  jmeno_souboru_verejneho_klice" << endl;
		return 1;
	}

	if (!(fp = fopen(argv[2], "rb"))) {
		cout << "Cannot open public key file" << endl;
		return 1;
	}

	if (!(in = fopen(argv[1], "rb"))) {
		cout << "Cannot open data file" << endl;
		return 1;
	}

	if (!(out = fopen("encrypted", "wb"))) {
		cout << "Cannot open output file" << endl;
		return 1;	
	}

	EVP_PKEY * pubkey;
	pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL);
	fclose(fp);



	int bytesRead = 1;
	char bufferIn[bufferInLen];
	char bufferOut[bufferOutLen];
	int tmpLen = 0, stLen = 0;
	unsigned char *ek = new unsigned char[EVP_PKEY_size(pubkey)];
	unsigned char *iv = new unsigned char[EVP_MAX_IV_LENGTH];

	for (int i = 0; i < EVP_MAX_IV_LENGTH; i++)
		iv[i] = '\0';
	
	int ekLen, ivMaxLen = EVP_MAX_IV_LENGTH;
	EVP_CIPHER_CTX ctx;

	/* 1 = EVP_aes_128_cbc
	 * 2 = EVP_aes_128_ecb
	 * 3 = EVP_aes_192_cbc
	 * 4 = EVP_aes_192_ecb
	*/
	int cipherTypeF = 3; 
	if (!EVP_SealInit(&ctx, EVP_aes_192_cbc(), &ek, &ekLen, iv, &pubkey, 1)) {
		cout << "SealInit failed" << endl;
		return 1;
	}

	fwrite(&cipherTypeF, 1, sizeof(cipherTypeF), out);

	fwrite(&ekLen, 1, sizeof(ekLen), out); // enc. key len.
	fwrite(ek, 1, ekLen, out); // enc. key

	fwrite(&ivMaxLen, sizeof(ivMaxLen), 1, out); // delka iv
	fwrite(iv, ivMaxLen, 1,  out); // iv

	while (bytesRead > 0) {
		bytesRead = fread(bufferIn, 1, bufferInLen, in);
		EVP_SealUpdate(&ctx, (unsigned char*)bufferOut, &tmpLen, (const unsigned char*)bufferIn, bytesRead);
		stLen += tmpLen;
		fwrite(bufferOut, 1, tmpLen, out);
	}
	EVP_SealFinal(&ctx, (unsigned char*)bufferOut , &tmpLen);
	stLen += tmpLen;
	fwrite(bufferOut, 1, tmpLen, out);


	delete[] ek;
	delete[] iv;
	fclose(out);
	fclose(in);
	EVP_PKEY_free(pubkey);
	EVP_CIPHER_CTX_cleanup(&ctx);
	return 0;
}

Kód: Vybrat vše

/** 
 * \file ukol4-desifrovani.cpp
 * Code for decrypting a file. 
 * \author Miroslav Vlach <vlachmir@fit.cvut.cz> */
#include <stdio.h>
#include <string>
#include <iostream>
#include <openssl/evp.h>
#include <openssl/pem.h>

#define bufferInLen 1024
#define bufferOutLen 1024 + EVP_MAX_IV_LENGTH

using namespace std;

int main(int argc, char* argv[])
{
	FILE *fp, *in, *out;

	if (argc != 3) {
		cout << "Chybne zadani, potreba zadat   jmeno_souboru_k_desifrovani  jmeno_souboru_privatniho_klice" << endl;
		return 1;
	}

	if (!(fp = fopen(argv[2], "rb"))) {
		cout << "Cannot open private key file" << endl;
		return 1;
	}

	if (!(in = fopen(argv[1], "rb"))) {
		cout << "Cannot open data file" << endl;
		return 1;
	}

	if (!(out = fopen("decrypted", "wb"))) {
		cout << "Cannot open output file" << endl;
		return 1;	
	}

	EVP_PKEY * privkey;
	privkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
	fclose(fp);

	int bytesRead = 1;
	char bufferIn[bufferInLen];
	char bufferOut[bufferOutLen];	
	int tmpLen = 0, otLen = 0;

	unsigned char *ek = new unsigned char[EVP_PKEY_size(privkey)];
	unsigned char *iv = new unsigned char[EVP_MAX_IV_LENGTH];
	int ekLen, ivLen;
	
	int cipherTypeF;
	const EVP_CIPHER* cipherType;
	
	EVP_CIPHER_CTX ctx;

	fread(&cipherTypeF, 1, sizeof(cipherTypeF), in);

	switch (cipherTypeF) {
		case 1:
			cipherType = EVP_aes_128_cbc();
			break;
		case 2:
			cipherType = EVP_aes_128_ecb();
			break;
		case 3:
			cipherType = EVP_aes_192_cbc();
			break;
		case 4:
			cipherType = EVP_aes_192_ecb();
			break;
		default:
			cout << "Cipher not found" << endl;
			return 1;
	}

	bytesRead = fread(&ekLen, 1, sizeof(ekLen), in); // en. key len.
	bytesRead = fread(ek, 1, ekLen, in); // enc. key
	bytesRead = fread(&ivLen, sizeof(ivLen), 1, in); // delka iv
	bytesRead = fread(iv, ivLen, 1,  in); // iv

	if (!EVP_OpenInit(&ctx, cipherType, ek, ekLen, iv, privkey)) {
		cout << "OpenInit failed" << endl;
		return 1;
	}

	while (bytesRead > 0) {
		bytesRead = fread(bufferIn, 1, bufferInLen, in);
		EVP_OpenUpdate(&ctx, (unsigned char*)bufferOut, &tmpLen,  
							(const unsigned char*)bufferIn, bytesRead);
		otLen += tmpLen;
		fwrite(bufferOut, 1, tmpLen, out);
	}
	EVP_OpenFinal(&ctx, (unsigned char*)bufferOut, &tmpLen);
	otLen += tmpLen;
	fwrite(bufferOut, 1, tmpLen, out);


	delete[] ek;
	delete[] iv;
	fclose(out);
	fclose(in);
	EVP_PKEY_free(privkey);
	EVP_CIPHER_CTX_cleanup(&ctx);
	return 0;
}
You're pro or you're a noob. That's life
Obrázek Obrázek

Odpovědět