使用<openssl aes =“”> </openssl>未正确解密字符串

时间:2014-11-06 12:23:22

标签: c++ c linux encryption openssl

我想写一个简单的程序,它将使用<openssl/aes.h>加密一个测试,同时解密它。我在下面写了程序

在此处添加我的全部代码:

#include <stdio.h>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <string.h>


int main(void)
{
    //encryption testing
    unsigned char inputb[2048] = {'\0'};
    unsigned char encpb[2048]= {'\0'};
    unsigned char opb[2048]= {'\0'};
#define MAX_SIZE 100

    unsigned char oneKey[] = "6BC1BEE22E409F96E93D7E117393172A";
    AES_KEY key; 
    AES_KEY key1; 

    char testchat[] = "!!!test doctors file!!! @Hospitan name(norman) SICKAPP_NAME=9873471093 @Duration (Duration\
    of doctor visitdfwhedf in months)higibujiji TESTATION=-5 #Expiry date MADICINE_NAME=678041783478\n";
    char NULL_byte[16] = {0};
    memcpy((char*)inputb, (testchat), strlen(testchat)+1);
    printf("\n\ninputb= %s strlen(testchat)=%d \n\n",inputb, strlen(testchat));
    AES_set_encrypt_key(oneKey, 128, &key);
    unsigned char tmp_char[50] = {'\0'};
    char* pChar = (char*)inputb;
    unsigned char tmp_char_encpb[MAX_SIZE];
    while(*pChar != '\0') {
        memset(tmp_char, '\0',  50);
        memset(tmp_char_encpb, '\0',  MAX_SIZE);
        if(strlen(pChar) < 16)  {
            strncpy((char*)tmp_char, (char*)pChar, strlen(pChar)+1);
            strncat((char*)tmp_char, NULL_byte, 16 - strlen(pChar)+1);
        }
        else
            strncpy((char*)tmp_char, (char*)pChar, 16);
        printf("Line:%d tmp_char = %s pChar=%d\n", __LINE__, tmp_char, strlen(pChar));
        AES_encrypt(tmp_char, tmp_char_encpb, &key);
        strcat((char*)encpb, (char*)tmp_char_encpb);

        pChar += 16;
    }
    printf("len encpb=%d\n", strlen((char*)encpb));
    //now test with decrypting  and check if all okk....
    unsigned char oneKey1[] = "6BC1BEE22E409F96E93D7E117393172A";
    AES_set_decrypt_key(oneKey1,128,&key1);
    unsigned char tmp_char_dencpb[MAX_SIZE];
    pChar = (char*)encpb;
    while(*pChar != '\0') {
        memset(tmp_char, '\0',  50);
        if(strlen(pChar) < 16)  {
            strncpy((char*)tmp_char, (char*)pChar, strlen(pChar)+1);
            strncat((char*)tmp_char, NULL_byte, 16 - strlen(pChar)+1);
        }
        else
            strncpy((char*)tmp_char, (char*)pChar, 16);
        AES_decrypt(tmp_char, tmp_char_dencpb, &key1);
        strncat((char*)opb, (char*)tmp_char_dencpb,16);
        memset(tmp_char_dencpb, '\0',  MAX_SIZE);
        pChar += 16;
    }

    printf("\n\nopb = %s\n\n",opb);
    return 0;
}

我正在建设:

g++ mytest.cpp -lssl -lcrypto

贯穿GDB:

Program received signal SIGSEGV, Segmentation fault.
0x0000003e48437122 in ____strtoll_l_internal () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.47.el6_2.12.x86_64 keyutils-libs-1.4-3.el6.x86_64 krb5-libs-1.9-22.el6_2.1.x86_64 libcom_err-1.41.12-11.el6.x86_64 libgcc-4.4.6-3.el6.x86_64 libselinux-2.0.94-5.2.el6.x86_64 libstdc++-4.4.6-3.el6.x86_64 openssl-1.0.0-20.el6_2.4.x86_64 zlib-1.2.3-27.el6.x86_64
(gdb) backtrace
#0  0x0000003e48437122 in ____strtoll_l_internal () from /lib64/libc.so.6
#1  0x0000000000400e9b in GetExpiryDate (exp_date=0x7fffffffd970) at LicReader.cpp:66
#2  0x0000000000400eeb in IsLicenseExpired () at LicReader.cpp:74
#3  0x0000000000400f3b in main (argc=1, argv=0x7fffffffda88) at LicReader.cpp:86
(gdb)

OP:在一段时间内我得到了当前解密的字符串,并且有时间获得了垃圾字符。(当i / p字符串发生变化时)

我错过了什么吗?谁能说出为什么AES_decrypt在gsometimes中无效?

1 个答案:

答案 0 :(得分:1)

零终止字符串操作不是如何管理加密数据...例如,您使用strcat将加密数据添加到encpb ...但是如果有&#会发生什么? 39;加密数据中为零?如果您没有获得所有数据,会发生什么。处理实际的块大小,即16字节。如果加密的数据不是16字节的倍数会怎样?你必须把它填充到16的倍数。怎么样?有很多不同的方式,比如PKCS7。此外,你应该看看密码块链接和盐渍......很多东西要学习!