ase方式对bmp图像进行加密
废话不多说,直接上代码
#define _CRT_SECURE_NO_WARNINGS
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void handleErrors()
{
printf("Wrong encryption progress\n");
}
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext)
{
EVP_CIPHER_CTX *ctx;
int len;
int ciphertext_len;
/* Create and initialise the context */
if (!(ctx = EVP_CIPHER_CTX_new()))
handleErrors();
/* Initialise the encryption operation. IMPORTANT - ensure you use a key
* and IV size appropriate for your cipher
*/
if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv))
handleErrors();
/* Provide the message to be encrypted, and obtain the encrypted output.
* EVP_EncryptUpdate can be called multiple times if necessary
*/
if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
handleErrors();
ciphertext_len = len;
/* Finalise the encryption. Further ciphertext bytes may be written at
* this stage.
*/
if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
handleErrors();
ciphertext_len += len;
/* Clean up */
EVP_CIPHER_CTX_free(ctx);
return ciphertext_len;
}
int main(int argc, char **argv)
{
const char * fileName = "D:\\work\\code\\gitlab\\image_entroy\\Debug\\pic_original.bmp";
const char * outfileName = "D:\\work\\code\\gitlab\\image_entroy\\Debug\\pic_original_ent.bmp";
unsigned char *pBmpBuf;//读入图像数据的指针
//======================= STEP 0=====================================//
/*Key initialization.
It will be automaticalledly padded to 128 bit key
*/
unsigned char *key = (unsigned char *)"29285542";
//======================= STEP 1=====================================//
/*IV initialization.
The IV size for *most* modes is the same as the block size.
For AES128 this is 128 bits
*/
unsigned char *iv = (unsigned char *)"1234567890123456";
//======================= STEP 2=====================================//
/*read the file from given filename in binary mode*/
printf("Start to read the .bmp file \n");
FILE *fp;
fp = fopen(fileName, "rb");
if (fp == NULL) {
printf("Open Unsuccessfully");
}
else printf("Open Successfully");
const int FILE_SIZE = 184974;
unsigned char ciphertext[FILE_SIZE];
//======================= STEP 3=====================================//
/*allocate memory for bitmapHeader and bitmapImage,
then read bytes for these variables.*/
//allocate memory for the final ciphertext
unsigned char *header = (unsigned char*)malloc(54);
/*as this is a .bmpfile, we need to read the header
(the first 54 bytes) into bitmapHeader*/
fread(header, 54, 1, fp); //获取图像宽、高、每像素所占位数等信息
int bmpWidth = 460;
int bmpHeight = 134;
int biBitCount = 24;//定
int lineByte = (bmpWidth * biBitCount / 8 + 3) / 4 * 4;//灰度图像有颜色表,且颜色表表项为256
pBmpBuf = (unsigned char*)malloc(lineByte * bmpHeight);
fread(pBmpBuf, 1, lineByte * bmpHeight, fp);
fclose(fp);//关闭文件
//read the bitmap image content until the end of the .bmp file
//======================= STEP 4=====================================//
//encrypt the bitmapImage with the given studentID key
int ciphertextlen = encrypt(pBmpBuf, lineByte * bmpHeight, key,iv, ciphertext);
//======================= STEP 5=====================================//
/*merge header and bitmap to the final ciphertext
and output it into a .bmp file*/
FILE *p = fopen(outfileName, "w+");
fwrite(header, 54, 1, p);
fwrite(ciphertext, ciphertextlen, 1, p);
free(pBmpBuf);
free(header);
return 1;
}
加密前输入图片为:
加密后的图片: