Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C argv获取十六进制数据以便解密。字符串转换为无符号字符_C_String_Casting_Unsigned Char - Fatal编程技术网

C argv获取十六进制数据以便解密。字符串转换为无符号字符

C argv获取十六进制数据以便解密。字符串转换为无符号字符,c,string,casting,unsigned-char,C,String,Casting,Unsigned Char,我正在编写一个程序,它应该获得一个密钥和一个加密数据unsigned char hex,以便返回解密的信息。 我的代码如下: #include <stdlib.h> #include <stdio.h> #include "aes.h" int main(int argc, unsigned char * argv[]) { int i=1; //unsigned char ptext[16] = "Attack at down!"; unsign

我正在编写一个程序,它应该获得一个密钥和一个加密数据unsigned char hex,以便返回解密的信息。 我的代码如下:

#include <stdlib.h>
#include <stdio.h>
#include "aes.h"
int main(int argc, unsigned char * argv[])
{
    int i=1;
    //unsigned char ptext[16] = "Attack at down!";
    unsigned char ptext[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x03,0x15,0x02,0x01,0x00,0x19};
    //    unsigned char ctext[16]={0x9b,0x54,0xd4,0x9e,0xdc,0x96,0x69,0x30,0xf7,0xc5,0xd4,0x25,0x39,0x77,0xbe,0xb8};
    //unsigned char ctext[16]={0x54,0x9b,0x9e,0xd4,0x96,0xdc,0x30,0x69,0xc5,0xf7,0x25,0xd4,0x77,0x39,0xb8,0xbe};
    //unsigned char ctext[16]={0x51,0x0e,0xa4,0x12,0xa0,0x6f,0xdd,0x4e,0x4f,0x17,0x7c,0xe3,0x9d,0x0e,0x28,0xea};    
    unsigned char ctext[16]={0x2E,0x45,0x58,0x36,0xA8,0xAC,0x79,0x7E,0xC4,0xDF,0x51,0xDB,0xC4,0x88,0x28,0xC0};

    unsigned char key[KEY_128];
    //unsigned char key[KEY_128] ={0x0f,0x15,0x71,0xc9,0x47,0xd9,0xe8,0xf3,0x0f,0x15,0x71,0xc9,0x47,0xd9,0xe8,0xf3};
    unsigned char decptext[16];
    unsigned char test=0xFF;

    //gcc main.c aes.c -o decripter
    //sh decripter key encrypted --> return (decrypted)
    printf("\n test %x",test);       
    for(i=0;i<16;i++)
    {
        //How can I pass what I get from argv to the key unsigned char?? 
        key[i]=argv[i+1];
        printf("Key pos %d data->%s",i,key[i]);
    }  

    aes_ctx_t *ctx;

    init_aes();
    ctx = aes_alloc_ctx(key, sizeof(key));

    //Encrypted
    //aes_encrypt(ctx, ptext, ctext);
    //    printf("%s",ctext);

    aes_decrypt(ctx,ctext,decptext);
    for(i=0;i<16;i++)
        printf("\n Decrypted data, position in array-> %d data->  %x \n",i, decptext[i]);

    aes_free_ctx(ctx);
    return 0;
}
任何帮助都是非常受欢迎的

谢谢

我正在尝试这样的代码,它可以完美地工作THX ALL:

 #include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>

int main(int argc, unsigned char * argv[])
{
unsigned int aes_uint[16];
unsigned int cipher_uint[16];

int comerce;

uint8_t aes_data[16];
uint8_t ciphertext[16];
char temp[16][3]; // For 5 hex values if you want more char temp[10][5] for 10 values
char temp2[16][3]; // For 5 hex values if you want more char temp[10][5] for 10 values

char tempcomerce[2][3]; // For 5 hex values if you want more char temp[10][5] for 10 values

int i,j,k,c;

 for(i=0,c=0;argv[1][i];i++,c++){         // To parse the input argument to diff hex values
            for(j=i,k=0; j<(i+2) ;j++,k++){
            temp[c][k]=argv[1][j];
            }
            i=j-1;
            temp[c][k] = '\0';
    }

    for(i=0;i<c;i++) // where c stores the number hex values you have passed through argument
    {
            sscanf(temp[i],"%x",&aes_uint[i]);
            aes_data[i] = (unsigned char)aes_uint[i];
            printf("AES pos %d data->%x\n",i,aes_data[i]);
    }

    for(i=0,c=0;argv[2][i];i++,c++){         // To parse the input argument to diff hex values
            for(j=i,k=0; j<(i+2) ;j++,k++){
            temp2[c][k]=argv[2][j];
            }
            i=j-1;
            temp2[c][k] = '\0';
    }

    for(i=0;i<c;i++) // where c stores the number hex values you have passed through argument
    {
            sscanf(temp2[i],"%x",&cipher_uint[i]);
            ciphertext[i] = (unsigned char)cipher_uint[i];
            printf("Cipther pos %d data->%x\n",i,ciphertext[i]);
    }

    sscanf(argv[3],"%d",&comerce);
    printf("Comerce-->%d\n",comerce );



    // do your stuff        


return 0;
}
我得到:

    AES pos 0 data->52
AES pos 1 data->87
AES pos 2 data->1d
AES pos 3 data->6b
AES pos 4 data->9d
AES pos 5 data->60
AES pos 6 data->0
AES pos 7 data->46
AES pos 8 data->d7
AES pos 9 data->21
AES pos 10 data->c9
AES pos 11 data->79
AES pos 12 data->90
AES pos 13 data->ef
AES pos 14 data->42
AES pos 15 data->fa
Cipther pos 0 data->52
Cipther pos 1 data->87
Cipther pos 2 data->1d
Cipther pos 3 data->6b
Cipther pos 4 data->9d
Cipther pos 5 data->60
Cipther pos 6 data->0
Cipther pos 7 data->46
Cipther pos 8 data->d7
Cipther pos 9 data->20
Cipther pos 10 data->d4
Cipther pos 11 data->73
Cipther pos 12 data->90
Cipther pos 13 data->ee
Cipther pos 14 data->42
Cipther pos 15 data->bc
Comerce-->56748
伊万戈麦斯

使用sscanf-

解析:如果您将十六进制值作为单个参数传递

    unsigned char key[KEY_128];
    unsigned int key_uint[KEY_128];
    char temp[5][5]; // For 5 hex values if you want more char temp[10][5] for 10 values
    int i,j,k,c;

    for(i=0,c=0;argv[1][i];i++,c++){         // To parse the input argument to diff hex values
            for(j=i,k=0; j<(i+4) ;j++,k++){
            temp[c][k]=argv[1][j];
            }
            i=j-1;
            temp[c][k] = '\0';
    }

    // do your stuff        

    for(i=0;i<c;i++) // where c stores the number hex values you have passed through argument
    {
            sscanf(temp[i],"%x",&key_uint[i]);
            key[i] = (unsigned char)key_uint[i];
            printf("Key pos %d data->%u\n",i,key[i]);
    }

我认为sscanf是你最好的选择。此外,您不需要使用无符号字符argv,因为您总是在参数中获得&expect字符串。希望下面的代码示例对您有所帮助

for(i=0;i<16;i++) {
    if (sscan(argv[i+1], "%x", &key[i]) != 1) {
        // throw error
    }

    printf("Key pos %d data->%s",i,key[i]);
}
重写:

如果sscanargv[i+1]、%x和键[i]!=一,{

致:


如果sscanargv[1][i]、%x和键[i]!=1{

我使用密钥作为无符号字符,因为解密和加密函数需要该数据类型。我将尝试您的代码段。非常感谢您。我现在就尝试。再次感谢。它工作得很好。您知道接收不同的参数是否容易吗,现在我接收十六进制,但我是否也可以得到一个字符串作为last参数?@user1688663我想您已经在传递字符串,并且正在将其转换为十六进制!您是指十六进制数据转换为字符串?我的意思是,当我使用./test 0x01 0xFF…调用该程序时,例如,如果我尝试将其作为一个整体使用。/test 0x010xFF…我应该解析它,它将是一个字符串,与现在一样,但在argv[1]之前它是0x01,现在只有0,不是吗?所以我应该解析计数并将计数从argv[1]保存到argv[4]中,然后保存到char temp[4],使用sscanf将其存储在密钥单元中,然后像我以前做的那样将其转换为密钥?我在做这些事情时失败了,我不知道如何处理这些情况。谢谢如果您传递了多个参数,请使用各自的参数来解析它!我想您正在解析第一个参数两次。请检查第一个和第二个参数的输出相同!解析第一个参数后,解析第二个参数!fori=0,c=0;argv[2][i];i++,c++{forj=i,k=0;jI编辑了您的解决方案尝试一下!您能解释一下OP为什么要重写这行代码吗?
    for(i=0;i<16;i++)
    {
        //key[i]=argv[i+1]; 
        sscanf(argv[i+1],"%x",&key[i]); // It will work for you!

        printf("Key pos %d data->%u",i,key[i]);
    }
unsigned char key[KEY_128];
unsigned int key_uint[KEY_128];
// your stuff

for(i=0;i<16;i++)
{
        //key[i]=argv[i+1]; 
        sscanf(argv[i+1],"%x",&key_uint[i]);
        key[i] = (unsigned char)key_uint[i];
        printf("Key pos %d data->%u",i,key[i]);
}
    unsigned char key[KEY_128];
    unsigned int key_uint[KEY_128];
    char temp[5][5]; // For 5 hex values if you want more char temp[10][5] for 10 values
    int i,j,k,c;

    for(i=0,c=0;argv[1][i];i++,c++){         // To parse the input argument to diff hex values
            for(j=i,k=0; j<(i+4) ;j++,k++){
            temp[c][k]=argv[1][j];
            }
            i=j-1;
            temp[c][k] = '\0';
    }

    // do your stuff        

    for(i=0;i<c;i++) // where c stores the number hex values you have passed through argument
    {
            sscanf(temp[i],"%x",&key_uint[i]);
            key[i] = (unsigned char)key_uint[i];
            printf("Key pos %d data->%u\n",i,key[i]);
    }
for(i=0;i<16;i++) {
    if (sscan(argv[i+1], "%x", &key[i]) != 1) {
        // throw error
    }

    printf("Key pos %d data->%s",i,key[i]);
}