[BEZ] cvičení 3

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

cvičení 3

Příspěvek od Destroyer » pon 23. bře 2015 4:30:50

Kód: Vybrat vše

hexdump -C Mad_scientist.bmp  | less
20150325_155632.jpg
WIP
You're pro or you're a noob. That's life

crish
Příspěvky: 4
Registrován: stř 12. bře 2014 0:13:40

Re: cvičení 3

Příspěvek od crish » pát 27. bře 2015 17:27:00

Kód: Vybrat vše

#include <stdlib.h>
#include <openssl/evp.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
using namespace std;

int main(void) {
    
    unsigned char ot[1024];  // open text
    unsigned char st[1024];  // sifrovany text
    unsigned char key[EVP_MAX_KEY_LENGTH] = "Muj klic";  // klic pro sifrovani
    unsigned char iv[EVP_MAX_IV_LENGTH] = "inicial. vektor";  // inicializacni vektor
    
    int otLength;
    int stLength = 0;
    
    
    FILE * picture;
    FILE * encrypted;
    unsigned char buffer[10];
    unsigned char * header;
    int32_t length;
    
    picture = fopen("Mad_scientist.bmp", "rb");  // "B" cteni Binary
    encrypted = fopen("Mad_scientist_cbc.bmp", "wb");
    
    fread(buffer, 1, 10, picture);
    fwrite(buffer, 1, 10, encrypted);
    fread(&length, 4, 1, picture);
    fwrite(&length, 4, 1, encrypted);
    
    header = new unsigned char[length - 14];
    fread(header, 1, (length - 14), picture);
    fwrite(header, 1, (length - 14), encrypted);
    delete [] header;
    
    
    cout << length << endl;
    EVP_CIPHER_CTX ctx;
    EVP_EncryptInit(&ctx, EVP_des_cbc(), key, iv);  // nastaveni kontextu pro sifrovani
    
    while (!feof(picture)) {
        otLength = fread(ot, 1, sizeof (ot), picture);
        
        /* Sifrovani */
        
        EVP_EncryptUpdate(&ctx, st, &stLength, ot, otLength); // sifrovani ot
        fwrite(st, 1, stLength, encrypted);
    }
    
    EVP_EncryptFinal(&ctx, st, &stLength); // ziskani sifrovaneho textu z kontextu
    fwrite(st, 1, stLength, encrypted);
    
    fclose(picture);
    fclose(encrypted);
    
    exit(0);
}

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

Re: cvičení 3

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

Kód: Vybrat vše

/** 
 * \file ukol2-cely.cpp
 * Main file. 
 * \author Miroslav Vlach <vlachmir@fit.cvut.cz> */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <openssl/evp.h>

int main(int argc, char * argv[]) {
    if (argc != 3)
    {
        printf("Chybne zadani, potreba zadat jmeno_souboru_bez_pripony mod napr. soubor ECB\n");
        return 1;
    }
    char cipherName[8];
    int mod;
    if (strcmp("ECB",argv[2]) == 0)
    {
        mod = 0;
        strcpy(cipherName,"DES-ECB");
    }
    else if (strcmp("CBC",argv[2]) == 0)
    {
        mod = 1;
        strcpy(cipherName,"DES-CBC");
    }
    else
    {
        printf("Chybne zadany mod!");
        return 1;
    }
    char * unencryptedfileName = (char *)calloc(strlen(argv[1])+5,sizeof(char)); 
        strcat(unencryptedfileName,argv[1]); 
        strcat(unencryptedfileName,".bmp");
    char * encryptedfileName = (char *)calloc(strlen(argv[1])+9,sizeof(char)); 
        strcat(encryptedfileName,argv[1]); 
        mod == 0 ? strcat(encryptedfileName,"_ecb.bmp") : strcat(encryptedfileName,"_cbc.bmp");
    char * decryptedfileName = (char *)calloc(strlen(argv[1])+13,sizeof(char)); 
        strcat(decryptedfileName,argv[1]); 
        mod == 0 ? strcat(decryptedfileName,"_ecb_dec.bmp") : strcat(decryptedfileName,"_cbc_dec.bmp");

    unsigned char ot[1024];  // otevreny text
    unsigned char st[1024];  // sifrovany text
    unsigned char key[EVP_MAX_KEY_LENGTH] = "z8b6azqo";  // klic pro sifrovani
    unsigned char iv[EVP_MAX_IV_LENGTH] = "vector";  // inicializacni vektor

    int otLength;
    int stLength = 0;
   
    FILE * unencryptedfile;
    FILE * encryptedfile;
    FILE * decryptedfile;
    unsigned char buffer[10];
    unsigned char * header;
    int32_t length;
   
    const EVP_CIPHER * cipher;
 
    OpenSSL_add_all_ciphers();
    cipher = EVP_get_cipherbyname(cipherName);
    if(!cipher) {
        printf("Sifra %s neexistuje.\n", cipherName);
        exit(1);
    }

    // --- Sifrovani ---
    unencryptedfile = fopen(unencryptedfileName, "rb"); 
    encryptedfile = fopen(encryptedfileName, "wb");

   
    // vyreseni hlavicky souboru
    fread(buffer, 1, 10, unencryptedfile);
    fwrite(buffer, 1, 10, encryptedfile);
    fread(&length, 4, 1, unencryptedfile);
    fwrite(&length, 4, 1, encryptedfile);

    // presun zbytku hlavicky az do zacatku souboru
    header = (unsigned char *)malloc((length - 14));
    fread(header, 1, (length - 14), unencryptedfile);
    fwrite(header, 1, (length - 14), encryptedfile);
    free(header);
   
    EVP_CIPHER_CTX ctx;
    EVP_EncryptInit(&ctx, cipher, key, iv);  // nastaveni kontextu pro sifrovani
   
    while (!feof(unencryptedfile)) {
        otLength = fread(ot, 1, sizeof(ot), unencryptedfile);
        EVP_EncryptUpdate(&ctx, st, &stLength, ot, otLength); // sifrovani ot
        fwrite(st, 1, stLength, encryptedfile);
    }
   
    EVP_EncryptFinal(&ctx, st, &stLength); // ziskani sifrovaneho textu z kontextu
    fwrite(st, 1, stLength, encryptedfile);
   
    fclose(unencryptedfile);
    fclose(encryptedfile);
    EVP_CIPHER_CTX_cleanup(&ctx);
    
    // --- Desifrovani ---
    encryptedfile = fopen(encryptedfileName, "rb");
    decryptedfile = fopen(decryptedfileName, "wb");
    // vyreseni hlavicky souboru
    fread(buffer, 1, 10, encryptedfile);
    fwrite(buffer, 1, 10, decryptedfile);
    fread(&length, 4, 1, encryptedfile);
    fwrite(&length, 4, 1, decryptedfile);

    // presun zbytku hlavicky az do zacatku souboru
    header = (unsigned char *)malloc((length - 14));
    fread(header, 1, (length - 14), encryptedfile);
    fwrite(header, 1, (length - 14), decryptedfile);
    free(header);

    EVP_DecryptInit(&ctx, cipher, key, iv);  // nastaveni kontextu pro sifrovani
   
    while (!feof(encryptedfile)) {
        stLength = fread(st, 1, sizeof(st), encryptedfile);
        EVP_DecryptUpdate(&ctx, ot, &otLength,  st, stLength); // sifrovani ot
        fwrite(ot, 1, otLength, decryptedfile);
    }
   
    EVP_DecryptFinal(&ctx, ot, &stLength); // ziskani sifrovaneho textu z kontextu
    fwrite(ot, 1, stLength, decryptedfile);


    fclose(encryptedfile);
    fclose(decryptedfile);
    free(unencryptedfileName);
    free(encryptedfileName);
    free(decryptedfileName);
    EVP_CIPHER_CTX_cleanup(&ctx); // uvolneni EVP_EncryptInit
    EVP_cleanup(); // uvolneni OpenSSL_add_all_ciphers
    return 0;
}
You're pro or you're a noob. That's life

Odpovědět