密码库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; 
}   

猜你喜欢

转载自blog.csdn.net/samsho2/article/details/84871120