在 C 中使用 libtomcrypt 输出 ecc_decryption_key 时出现问题

问题描述 投票:0回答:1

我有以下简单的代码。

我只是初始化参数,调用 make_key 函数,加密数据并尝试解密数据

#define LTM_DESC 
#include <tomcrypt.h>
int main(void)
{
    ecc_key mykey;
    prng_state prng;
    int err, prng_idx , hash_idx;

    ltc_mp = ltm_desc;

    /* register yarrow */
    if (register_prng(&yarrow_desc) == -1) {
    printf("Error registering Yarrow\n");
    return -1;
    }
    
    if (register_hash(&sha1_desc) == -1) {
        printf("Error registering sha1");
        return EXIT_FAILURE;
    }

    prng_idx = find_prng("yarrow");
    hash_idx = find_hash("sha1");
    /* setup the PRNG */
    if ((err = rng_make_prng(128, find_prng("yarrow"), &prng, NULL))
    != CRYPT_OK) {
    printf("Error setting up PRNG, %s\n", error_to_string(err));
    return -1;
    }
    /* make a 192-bit ECC key */
    if ((err = ecc_make_key(&prng, find_prng("yarrow"), 55, &mykey))
        != CRYPT_OK) {
        printf("Error making key: %s\n", error_to_string(err));
    return -1;
    }

        const unsigned char *plaintext = (const unsigned char *)"Hello, this is a secret message!";
        unsigned long ptlen = sizeof(plaintext);
        unsigned char ciphertext[1024];
        unsigned long ctlen = sizeof(ciphertext);

    if ((err = ecc_encrypt_key(plaintext,                       // const unsinged char *in
                                    ptlen,                             //unsinged long inlen
                                    ciphertext,                       // unsinged *out
                                    &ctlen,                           // unsinged long *outlen
                                    &prng,                               // prng_state *prng
                                    prng_idx,                         // int wprng
                                    hash_idx,                         // int hash
                                    &mykey)) != CRYPT_OK) {           // ecc_key *ley
            printf("Erro ao criptografar o texto: %s\n", error_to_string(err));
            return -1;
}

printf("Text after encrypted: ");
    for (unsigned long i = 0; i < ctlen; ++i) {
        printf("%02x", ciphertext[i]);
    }
    printf("\n");

    unsigned char decryptedtext[2048];
    unsigned long decrypted_len = sizeof(decryptedtext);
    if ((err = ecc_decrypt_key(ciphertext, ctlen, decryptedtext, &decrypted_len, &mykey)) != CRYPT_OK) {
        printf("Erro ao descriptografar o texto: %s\n", error_to_string(err));
        return -1;
    }

    decryptedtext[decrypted_len] = '\0';

    printf("%d",decryptedtext == plaintext);
    printf("\n");
    printf("Text after decrypted: %.*s\n", (int)decrypted_len, decryptedtext);
    printf("Text after decrypted: %s\n", plaintext);

    printf("Text after decryptedo: ");
    for (unsigned long i = 0; i < decrypted_len; ++i) {
        printf("%c", decryptedtext[i]);
    }
    printf("\n");

    // Update decrypted_len based on the actual length of the decrypted text
decrypted_len = strlen((char *)decryptedtext);

// Print the decrypted text
printf("Text after decrypted: %s\n", decryptedtext);

printf("Text after decrypted (Hex): ");
for (unsigned long i = 0; i < decrypted_len; ++i) {
    printf("%02x", decryptedtext[i]);
}
printf("\n");


return 0;
}

在我的终端中,我使用

进行编译

gcc -Wall -O0 -g test2.c -ltomcrypt -o test2

我想看看排队后的情况

printf("Text after decrypted: %s\n", decryptedtext);

Text decrypted: Hello, this is a secret message!

但我看到了

Text decrypted: Hello, t

为什么打印不完整? 我不知道该怎么做才能看到完整的消息被解密。

现在我只需输入任何文字即可完成我必须写的 220 个字母才能发布此内容

“太阳落入地平线以下,为天空涂上了迷人的色彩。当傍晚温柔地拥抱大地时,阴影变得更长。大自然似乎停了下来,仿佛屏住呼吸,等待着夜晚的降临。来了。鸟儿叽叽喳喳地唱着最后的旋律,向这一天告别,沙沙作响的树叶在暮色中低语着秘密。

在这宁静的景象中,一种宁静的感觉笼罩着周围。空气中弥漫着盛开的花朵和潮湿的泥土的芳香,带着宁静的夜晚的希望。远处,城市的灯光开始闪烁,在昏暗的天空下奏响了城市光芒的交响曲。

当星星一颗一颗出现时,它们闪烁着光芒,点缀着天空。宁静的夜幕唤醒了沉思的精神,引发人们对白天的时刻和夜晚等待的梦想的反思。这是拥抱平静并在过渡时期的美丽中寻找安慰的时刻。”

c encryption cryptography elliptic-curve libtomcrypt
1个回答
0
投票

这段代码

const unsigned char *plaintext = (const unsigned char *)"Hello, this is a secret message!";
unsigned long ptlen = sizeof(plaintext);

ptlen
设置为 指针
plaintext
的大小。

这个代码会更好

const unsigned char plaintext[] = "Hello, this is a secret message!";
unsigned long ptlen = sizeof(plaintext);
© www.soinside.com 2019 - 2024. All rights reserved.