首先,我是C语言的新手,尤其是使用像libgcrypt这么大的lib,所以如果它非常简单,请理解它。
所以,我一直在尝试加密一些文本,只是为了从一个启动板,这样,我得到了一些更好的信心,然后才去加密字节,这是一个小应用程序的最终目标之一,如果你想调用它我一直在关注this,但仍在努力让它发挥作用真正让我困惑的是,我正在传递输出缓冲区,它的大小与文本中的大小匹配我确实觉得很奇怪,这个网站告诉我让输出缓冲区的大小与输入的大小相同,即使通常在用AES加密时输出的字节数很大也许我错过了一些更有经验的人可以帮助我的东西,我已经读了至少3-4遍他们的代码,我觉得这一定是我在这里错过的超简单的东西。
编辑
客户c

#include "client.h"

void printGCRYPTError(gcry_error_t err) {
    if (err) {
         {
           fprintf (stderr, "Failure: %s/%s\n",
                    gcry_strsource (err),
                    gcry_strerror (err));
           fprintf (stdout, "Failure: %s/%s\n",
                    gcry_strsource (err),
                    gcry_strerror (err));
         }
    }
}

char* Encrypt(char* in, char* key, char* iv){
    size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
    size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);

    if(strlen(key) != keyLength) {
        printf("Invalid Key Size. Expected %zu got %lu\n", keyLength, strlen(key));
        return NULL;
    }
    if(strlen(iv) != blkLength) {
        printf("Invalid BLK/IV Size. Expected %zu got %lu\n", blkLength, strlen(iv));
        return NULL;
    }

    gcry_cipher_hd_t handle;
    gcry_error_t err = 0;

    size_t inLen = strlen(in)+1;
    char * out = malloc(inLen);

    // Open
    err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_C_MODE, 0);
    printGCRYPTError(err);

    // Set Key
    err = gcry_cipher_setkey(handle, key, keyLength);
    printGCRYPTError(err);

    // Set IV
    err = gcry_cipher_setiv(handle, iv, blkLength);
    printGCRYPTError(err);

    // Enc
    printf("Encrypting...\n");
    err = gcry_cipher_encrypt(handle, out, inLen, in, inLen);
    printGCRYPTError(err);
    printf("Encrypted.\n");
    gcry_cipher_final(handle);


    //Out
    printf("Plaintext Message: %s\n", in);
    printf("Encrypted Message: ");
    int index = 0;
    for (index = 0; index<inLen; index++) {
        printf("%02X", (unsigned char)out[index]);
    }
    printf("\n");

    // Cleanup!
    gcry_cipher_close(handle);

    // Return
    return out;
}

char* Decrypt(char* in, char* key, char* iv){
    size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
    size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);

    if(strlen(key) != keyLength) {
        printf("Invalid Key Size. Expected %zu got %lu\n", keyLength, strlen(key));
        return NULL;
    }
    if(strlen(iv) != blkLength) {
        printf("Invalid BLK/IV Size. Expected %zu got %lu\n", blkLength, strlen(iv));
        return NULL;
    }

    gcry_cipher_hd_t handle;
    gcry_error_t err = 0;

    size_t inLen = strlen(in);
    char * out = malloc(inLen);

    // Open
    err = gcry_cipher_open(&handle, GCRY_CIPHER, GCRY_C_MODE, 0);
    printGCRYPTError(err);

    // Set Key
    err = gcry_cipher_setkey(handle, key, keyLength);
    printGCRYPTError(err);

    // Set IV
    err = gcry_cipher_setiv(handle, iv, blkLength);
    printGCRYPTError(err);

    // Dec
    printf("Decrypting...\n");
    err = gcry_cipher_decrypt(handle, out, inLen, in, inLen);
    printGCRYPTError(err);
    printf("Decrypted.\n");
    gcry_cipher_final(handle);

    //Out
    printf("Encrypted Message: ");
    int index = 0;
    for (index = 0; index<inLen; index++) {
        printf("%02X", (unsigned char)out[index]);
    }
    printf("\n");
    printf("Decrypted Message: %s\n", out);

    // Cleanup!
    gcry_cipher_close(handle);

    // Return
    return out;
}

int main() {
    //aesTest();
    char* encrypted = Encrypt("My test message", "sBK0hcXddz6mIKEsBK0hcXddz6mIKEAS", "sBK0uhSAUSUSHKES");
    char* decrypted = Decrypt(encrypted, "sBK0hcXddz6mIKEsBK0hcXddz6mIKEAS", "sBK0uhSAUSUSHKES");
}

控制台输出
Encrypting...
Encrypted.
Plaintext Message: My test message
Encrypted Message: 153C01DD7821E5113C25D2038F3644F7
Decrypting...
Failure: gcrypt/Invalid length
Failure: gcrypt/Invalid length
Decrypted.
Encrypted Message: 0000000000001000000000000000101400
Decrypted Message:

谢谢你的帮忙!

最佳答案

根据libgcrypt的文件:
某些加密模式要求在
最后一个数据块被传递给这个函数。
所以我想你需要做两件事:
检查gcry_cipher_encrypt中的错误代码结果是否与以前的函数调用一致。
调用encrypt后调用gcry_cipher_final(handle)-假设正在对小于密码块长度的缓冲区执行一次性加密。

08-16 23:53