0

我的代码有奇怪的问题。我正在使用 xtea c 代码和此处提供的 AES 随机数生成器的实现。我正在生成一条随机消息并尝试使用此 xtea 代码进行加密和解密,但大多数时候我得到“中止陷阱:6”,而其他时候它执行得很好,有时会生成错误的答案,有时会生成正确的答案。

算法如下(主要部分,我读取文件的地方在文件末尾的主要部分):

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

/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */

void encrypt(uint32_t v[2], uint32_t const key[4], int rounds) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}


// algorith to decipher the encrypted message 
void decrypt(uint32_t v[2], uint32_t const key[4], int rounds) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*rounds;
    for (i=0; i < rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}


//rotate an char array to the left one time
char* leftRotateByOne(char *arr){
    int i, size = strlen(arr);
    char tmp = arr[0];
    char *arr_tmp = malloc(strlen(arr));

    for(i=0;i<(size-1);i++){
        arr_tmp[i] = arr[i+1];
    }

    arr_tmp[size-1] = tmp;
    return arr_tmp;
}

//rotate all the elements to the left rotation times
char* leftRotate(char *word, int rotation){
    int i;

    for(i=0;i<rotation;i++){
        word = leftRotateByOne(word);
    }
    return word;
}


char* encrypt_msg(char* message, char* key, long msg_size, int round){
   //ARGUMENTS  
  //==========================

  char *enc_msg = malloc(strlen(message)); //will be returned with encrypted message
  char * message_part = malloc(2*sizeof(uint32_t)); //will hold the parts of the message
  int i,j; //iterator key

  //LOGIC PART
  //============================

  //printf("Mesage size: %ld", msg_size);
  //loop for the whole message
  for(j=1;j<=(msg_size/8);j++){

      //get 8 char at time to the message
      memcpy(message_part, leftRotate(message, (j-1)*8),  8);

      //transform char to uint32_t to be processed by the functions 
      uint32_t * message_int = (uint32_t *)message_part;
      uint32_t * key_int = (uint32_t *)key;

      encrypt(message_int, key_int, round);

      char * message_char = (char*)message_int;
      strcat(enc_msg, message_char);
  }

  return enc_msg;
}


char* decrypt_msg(char* message, char* key, long msg_size, int round){
   //ARGUMENTS  
  //==========================

  char *enc_msg = malloc(strlen(message)); //will be returned with encrypted message
  char * message_part = malloc(2*sizeof(uint32_t)); //will hold the parts of the message
  int i,j; //iterator key


  //LOGIC PART
  //============================

  //loop for the whole message

  for(j=1;j<=(msg_size/8);j++){

      //get 8 char at time to the message
      memcpy(message_part, leftRotate(message, (j-1)*8),  8);

      //transform char to uint32_t to be processed by the functions 
      uint32_t * message_int = (uint32_t *)message_part;
      uint32_t * key_int = (uint32_t *)key;

      decrypt(message_int, key_int, round);

      char * message_char = (char*)message_int;
      strcat(enc_msg, message_char);

  }

  return enc_msg;
}



int main(int argc, char** argv){
    char * key = argv[2];

    FILE *fp;
    long lSize;
    char *buffer;

    fp = fopen ( argv[1] , "rb" );
    if( !fp ) perror(argv[1]),exit(1);

    fseek( fp , 0L , SEEK_END);
    lSize = ftell( fp );
    rewind( fp );

    /* allocate memory for entire content */
    buffer = calloc( 1, lSize+1 );
    if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1);

    /* copy the file into the buffer */
    if( 1!=fread( buffer, lSize, 1 , fp) )
      fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1);



    char * enc_msg = encrypt_msg(buffer, key, lSize, atoi(argv[3]));
    //printf("%s", enc_msg);

    char * dec_msg = decrypt_msg(enc_msg, key, lSize, atoi(argv[3]));
    printf("%s", dec_msg);  

    fclose(fp);
    free(buffer);


    return 0;


}
4

0 回答 0