remote_libssh/aes/main.c
2022-05-07 13:18:59 +08:00

125 lines
2.9 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <ctype.h>
#include "aes.h"
#define AES_ENC_MAX_LEN 8192
uint8_t *key;
void from_hex(char *s, int l, char *d)
{
while (l--) {
*(d++) = ((*s > '9' ? (*(s++) + 9) : *(s++)) << 4)
| ((*s > '9' ? (*(s++) + 9) : *(s++)) & 0x0F);
}
}
void StringToByte(char *source, unsigned char *dest, int sourceLen)
{
int i;
unsigned char highByte, lowByte;
for (i = 0; i < sourceLen; i += 2) {
highByte = toupper(source[i]); //转换为大写
lowByte = toupper(source[i + 1]);
if (highByte > 0x39)
highByte -= 0x37;
else
highByte -= 0x30;
if (lowByte > 0x39)
lowByte -= 0x37;
else
lowByte -= 0x30;
dest[i / 2] = (highByte << 4) | lowByte;
}
return;
}
int array_len(char *str)
{
int i = 0;
int Len = strlen(str);
unsigned char out[AES_ENC_MAX_LEN] = { 0 };
StringToByte(str, out, Len);
for (i = 0; i < Len / 2; i++) {
;
//printf("%02X ", out[i]);
}
//printf("%d\n", i);
return i;
}
int main(int argc, char *argv[])
{
key = (uint8_t *)malloc(128);
strcpy(key, "1234567890ABCDEF");
char string[AES_ENC_MAX_LEN];
int opt;
char optstrs[] = ":e:d:k:h?";
while (-1 != (opt = getopt(argc, argv, optstrs))) {
switch (opt) {
case 'k':
strcpy(key, optarg);
break;
case 'e':
{
memset(string, 0, AES_ENC_MAX_LEN);
memcpy(string, optarg, strlen(optarg));
uint16_t i = 0;
uint8_t out[AES_ENC_MAX_LEN];
uint16_t length = strlen(string);
while (length % 16) {
strcat(string, "\0");
length++;
}
//printf("加密数据:\n");
EncryptDataToCipherTxt((uint8_t *) string, out, length);
//printf("密文长度=%d\n", length);
for (i = 0; i < length; i++) {
printf("%02X", out[i]);
}
printf("\n");
;
}
break;
case 'd':
{
memset(string, 0x00, AES_ENC_MAX_LEN);
uint8_t out[AES_ENC_MAX_LEN];
memset(out, 0x00, AES_ENC_MAX_LEN);
from_hex(optarg, array_len(optarg), (char *)out);
//printf("%s\n", out);
DecryptCipherTxtToData(out, (uint8_t *) string, array_len(optarg));
//printf("解密报文长度=%d\n", array_len(optarg));
printf("%s\n", string);
}
break;
case ':':
printf("\nMissing argument after: -%c\n", optopt);
case 'h':
case '?':
default:
;
}
}
free(key);
return 0;
}