c - argv 获取十六进制数据以便解密。字符串到无符号字符

标签 c string casting unsigned-char

我正在编写一个程序,该程序应该获取 key 和加密数据(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;
}

这次我只是想拿到 key 。如您所见,我也尝试使用 unsigned char *argv[] 因为我认为我会得到一个 unsigned char 但打印我从 argv 得到的内容的唯一方法是使用 %s,所以它是一个字符串,我可以'找到将其传递给 unsigned char key[16] 的方法(128 位 = 16*8 位每个十六进制)。 要执行程序,我是这样做的:

./prueba 0x0f 0x15 0x71 0xc9 0x47 0xd9 0xe8 0xf3 0x0f 0x15 0x71 0xc9 0x47 0xd9 0xe8 0xf3

我们非常欢迎任何帮助。

谢谢。

我正在尝试像这样使用您的代码,它运行完美 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;
}

我介绍:

./prueba 52871D6B9D600046D721C97990EF42FA 52871D6B9D600046D720D47390EE42BC 56748

我得到:

    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-

    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]);
    }

但是当你在 sscanf 中使用 %x 时,它需要第三个参数作为 unsigned int 类型!但是 key[KEY_128];unsigned char,所以声明 key[KEY_128];unsigned int 而不是无符号字符!

编辑: 声明另一个类型为 unsigned int 的数组,并将字符串转换为 unsigned int,然后再次转换回 unsigned char。试试这个-

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]);
    }

关于c - argv 获取十六进制数据以便解密。字符串到无符号字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25547924/

相关文章:

将小数转换为整数?

将两个整数合并为 1?

c - 读取文件到字符串

c++ - 如何查找字符串中两个连续空格的位置

Java:无法在枚举中使用 EnumSet:初始化错误:Tech Research Talent Tree 示例

C语言Osx下的彩色文本[Xcode]

python - 将 C++ 缓冲区复制到 Python 的最佳方法

java - for 循环打印 - 与字长一样多?

java - Java中的惰性参数类型?

swift - 挑战 Swift 中的类型转换