密碼庫LibTomCrypt學習記錄——(2.9)分組密碼演算法的工作模式——OFB程式碼示例
OFB加密檔案示例
//#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "tomcrypt.h"
#include "TestMode.h"
//#include "Test_ECB_AES.h"
void UserIO(char * p_print, char *p_need_inf )
{
printf(p_print);
scanf("%s", p_need_inf);
}
void InputOperation(char *p_oper )
{
while(1)
{
UserIO("\n需要執行什麼操作? 加密輸入 1, 解密輸入 0 \n", p_oper);
if ( p_oper[0] == '1' || p_oper[0] == '0' )
{
return;
}
else
{
printf("\n輸入操作不正確!\n");
}
}
}
void InputInFileName(char *ana_file_name )
{
FILE *fp_test_file;
while(1)
{
UserIO("\n請指定輸入檔名 ( 要求 : 檔案路徑不能有空格 )\n", ana_file_name);
fp_test_file = fopen(ana_file_name, "rb");
if ( fp_test_file != NULL )
{
fclose(fp_test_file);
return;
}
else
{
printf("\n檔名異常!無法開啟!\n");
}
}
}
void InputOutFileName(char *ana_file_name )
{
FILE *fp_test_file;
while(1)
{
UserIO("\n請指定輸出檔名 ( 要求 : 檔案路徑不能有空格 )\n", ana_file_name);
fp_test_file = fopen(ana_file_name, "wb");
if ( fp_test_file != NULL )
{
fclose(fp_test_file);
return;
}
else
{
printf("\n檔名異常!無法開啟!\n");
}
}
}
void Inputkey(char *p_key)
{
while(1)
{
UserIO("\n請輸入金鑰 長度為16位元組\n", p_key);
if ( strlen(p_key) >= 16 )
{
break;
}
else
{
printf("\n金鑰長度異常!\n");
}
}
}
int Test_CBC_AES_ENC_DEC_FILE(void)
{
int idx, err, i, res;
BYTE buf[64];
symmetric_OFB ofb;
int keylen, msglen, taillen;
BYTE key[32], IV[16], pt[64], ct[64];
// char *p_operate;
char oper[256];
char input_file_name[256];
char output_file_name[256];
char *p_buf1 = NULL, *p_buf2 = NULL;
FILE *fp_in, *fp_out;
int buf_len = 1024*1024;
int read_size = 0;
int oper_flag = 0;
int counter = 0;
p_buf1 = (char*)malloc(buf_len);
p_buf2 = (char*)malloc(buf_len);
if (p_buf1 == NULL || p_buf2 == NULL )
{
return 0;
}
IV[0] = 0;
InputOperation( oper );
InputInFileName( input_file_name);
InputOutFileName(output_file_name);
Inputkey( key);//只取前面16 bytes
if( ( fp_in = fopen(input_file_name, "rb")) == NULL )
{
return 0;
}
if( ( fp_out = fopen(output_file_name, "wb")) == NULL )
{
return 0;
}
/* AES can be under rijndael or aes... try to find it */
if ( register_cipher (&aes_desc) != CRYPT_OK )
{
return CRYPT_INVALID_CIPHER;
}
if ((idx = find_cipher("aes")) == -1)
{
return CRYPT_NOP;
}
printf("\n開始工作!\n" );
keylen = 16;
// Str2Num(key, 1, key);
if ((err = ofb_start(idx, key, key, keylen, 0, &ofb)) != CRYPT_OK)
{
return err;
}
oper_flag = (strcmp( oper, "1") == 0);// 加密輸入 1, 解密輸入 0 \n", p_oper);
while ( (read_size = fread(p_buf1, 1, buf_len, fp_in)) > 0 )
{
if ( (counter++)%10 == 0 )
{
printf(".");
}
msglen = read_size;//read_size/128*128;
taillen = read_size - msglen;
if ( oper_flag)// 加密
{
if ((err = ofb_encrypt( p_buf1, p_buf2, msglen, &ofb)) != CRYPT_OK) { return err; }
}
else//解密
{
if ((err = ofb_decrypt( p_buf1, p_buf2, msglen, &ofb)) != CRYPT_OK) { return err; }
}
fwrite(p_buf2, 1, msglen, fp_out);
if ( (taillen) != 0 )//讀到尾巴了
{
memset(p_buf1 + msglen,0xff, taillen );
msglen = 128;
if ( oper_flag)// 加密
{
if ((err = ofb_encrypt( p_buf1 + msglen, p_buf2, msglen, &ofb)) != CRYPT_OK) { return err; }
}
else//解密
{
if ((err = ofb_decrypt( p_buf1 + msglen, p_buf2, msglen, &ofb)) != CRYPT_OK) { return err; }
}
fwrite(p_buf2, 1, taillen, fp_out);
break;
}
}
cbc_done(&ofb);
unregister_cipher (&aes_desc);
free(p_buf1);
free(p_buf2);
printf("\n執行完畢!\n" );
return CRYPT_OK;
}