0

以下に示すようなコードがあります。ここのコードは、word_size = 64. 同様に、32 と 16 も必要です。encryptすべてのサイズで同じ関数を再利用する方法が見つかりません。さらに、word_size に従って変数も宣言する必要があります。uint_16またはuint_32またはのどちらを使用するかuint_64は、word_size によって異なります。この場合、再利用可能なコードを書くのを手伝ってもらえますか?

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

void encrypt(uint64_t* , uint64_t*, uint64_t*);

int main(){

    int block_size;

    // Get the user inputs
    printf("input the block size: \n");
    scanf("%d", &block_size);   // can be 32, 64 or 128

    int word_size = block_size/2;   // 16,32 or 64

    // Depending on the word_size, I should declare the variables with 
    // corresponding width
    uint64_t plain_text[2] = {0,0};  
    uint64_t cipher_text[2] = {0,0}; 
    uint64_t key_text[2] = {0,0};   

    uint64_t * pt, *ct, *k;
    encrypt(pt, ct,k);
    }

 /*
 * Ecnryption Method
 */
void encrypt(uint64_t* pt, uint64_t* ct, uint64_t* k){

    // Involves bit shifting algorithm which works only on exact sizes i.e eiter 16,32 or 64.
        }

必要に応じて、さらに情報を提供できます。

4

1 に答える 1

0

C でこれを行う方法があります - と を使用structしてunion

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

enum type {
    U64,
    U32,
    U16,
    U8,
};
struct container {
    enum type type;
    union {
        uint64_t u64;
        uint32_t u32;
        uint16_t u16;
        uint8_t  u8;
    } value;
};

int test(struct container container) {
    switch(container.type) {
        case U64:
            printf("Value is  :%" PRIu64 "\n", container.value);
            break;
        case U32:
            printf("Value is  :%" PRIu32 "\n", container.value);
            break;
        case U16:
            printf("Value is  :%" PRIu16 "\n", container.value);
            break;
        case U8:
            printf("Value is  :%" PRIu8 "\n", container.value);
            break;
    }
    return 0;
}

int main(int argc, char **argv) {
    struct container c1, c2;

    c1.type = U64;
    c1.value.u64 = 10000000000ULL;

    c2.type = U8;
    c2.value.u8 = 100;

    test(c1);
    test(c2);
    return 0;
}

生成された出力は次のとおりです。

Value is  :10000000000
Value is  :100
于 2015-03-16T16:40:41.400 に答える