anonymous No title
No License C
2019年07月11日
Copy
#include <openssl/des.h>
#include <sys/time.h> // For time measures
#include <string.h>
#include <ctype.h>

// Encryption/Decryption switches
#define ENC 1
#define DEC 0

// From char to DES_LONG (be aware that c is shifted)
// char→DES型
#define c2l(c,l)    (l =((DES_LONG)(*((c)++))), \
                     l|=((DES_LONG)(*((c)++)))<< 8L, \
                     l|=((DES_LONG)(*((c)++)))<<16L, \
                     l|=((DES_LONG)(*((c)++)))<<24L)

// From DES_LONG to char (be aware that c is shifted)
// DES→char
#define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
                     *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
                     *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
                     *((c)++)=(unsigned char)(((l)>>24L)&0xff))

void write_output(const char *filename, const unsigned char *in)
{
    // Now... Open the file binary with writing capabilities
    FILE *fp = fopen(filename, "wb");

    // If it can't be open, then return an error message
    if (fp == NULL) {fputs ("File error", stderr); exit (1);}

    // Write the in-array to specificed file-location
    fwrite(in, sizeof(unsigned char), strlen((const char *)in), fp);

    // Close the it
    fclose(fp);
}

const unsigned char *read_inputtext(const char *filename)
{
    // Total number of bytes
    unsigned long fsize;

    // Result of reading the file
    size_t result;

    // Now... Open the file binary with reading capabilities
    FILE *fp = fopen(filename, "rb");

    // If it can't be open, then return an error message
    if (fp == NULL) {fputs ("File error",stderr); exit (1);}

    /* Find out the number of bytes */
    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);      /* Get the size of the file */
    rewind(fp);             /* Go back to the start */

    // Allocate the buffer + 1 for termination
    unsigned char* buffer = malloc(fsize * sizeof *buffer + 1);

    // Test that everything went as we expected
    if(buffer == NULL) { fputs("Memory error!", stderr); exit(2); }

    // Read the buffer
    result = fread(buffer, 1, fsize * sizeof *buffer, fp);

    // Something went wrong when we read the file; sizes to not match
    if (result != fsize) {fputs ("Reading error", stderr); exit (3);}

    // Terminate the str
    buffer[fsize] = '\0';

    // Close the file
    fclose(fp);

    // Return the pointer to the dynamic allocated array
    return buffer;
}

void str2DES_cblock(const char *str, DES_cblock *out)
{
    // Make a char pointer and point it at the start of the array
    unsigned char *o;
    o = out[0];

    // Read the string
    int i;
    for (i = 0; i < 8; i++)
        sscanf(&(str[i*2]),"%2hhx", o++);
}

void my_des_cbc_encrypt(unsigned char *input, unsigned char *output, long length, DES_key_schedule ks, DES_cblock *ivec, int env){
  /*
    Assume that the input length (in byte) is a multiple of 8
    Try to undestand the macros l2c and c2l. They are important in implementation of CBC
    入力は8の倍数byte
  */

  unsigned char *iv;            // Initialization vector
  long l = length;

  DES_LONG xor0, xor1;
  DES_LONG in0, in1;
  DES_LONG data[2];
  /*
     Addtional variables
  */

  iv = ivec[0];

  //Initialize XOR-variables
  c2l(iv, xor0);
  c2l(iv, xor1);

  //Handling 8 bytes of input data each time inside the for loop.
  for(l = -8; l >= 0; l = -8){
    /*
      Your implementation of DES in CBC mode.
      Using des_encrypt1().
    */
    
  }
}

int main(int argc, char *argv[])
{
    int k;
    des_key_schedule key;
    DES_cblock iv, cbc_key;

    /*
      Other variables
    */
    const unsigned char *in;
    unsigned char *inp,*out;
    unsigned char *des_out;
    long s;
    FILE *fp; // ファイルポインタの作成
    char ch;
    char str[256];
    //DES_cblock in,out;
    /*
      Check number of command line arguments
    */
    if(argc != 5){
      printf("Not enough or too many arguments!\n");
      exit(1);
    }

    /*
      Check key and initialization vector validities. (comprise of Hexadicimal digits or not)
    */
    //16進数かを判定する関数
    if(isxdigit(*argv[1])!=1 || isxdigit(*argv[2])!=1) {
      if(isxdigit(*argv[1])!=1) printf("iv is not hexadecimal.\n");
      if(isxdigit(*argv[2])!=1) printf("key is not hexadecimal.\n");
      exit(2);
    }
    /*
      Convert key and initialization vector from string to DES_cblock
    */
    str2DES_cblock(argv[1],&iv);
    str2DES_cblock(argv[2],&cbc_key);
    //str2DES_cblock(argv[3],&in);
    //str2DES_cblock(argv[4],&out);
    /*
      read_inputtext();
    */
    fp = fopen(argv[3],"r");
    while(fgets(str, 256, fp) != NULL) {}
    fclose(fp);
    s = strlen(str) - 1;

    in = read_inputtext(argv[3]);
    inp = in;
    printf("%s",inp);

    /*
      my_des_cbc_encrypt();
    */

    //void my_des_cbc_encrypt(unsigned char *input, unsigned char *output, long length, DES_key_schedule ks, DES_cblock *ivec, int env){
    //my_des_cbc_encrypt(inp,out,s,key,&iv,ENC);
    //printf("%s\n", out);
    /*
      write_output();
    */
    //write_output(argv[4],x);

    //Compare the resutl with that using built-in funtion des_cbc_encrypt(). Details of des_cbc_encrypt() can be seen at http://web.mit.edu/macdev/Development/MITKerberos/MITKerberosLib/DESLib/Documentation/api.html
    /*
      des_cbc_encrypt();
    */
    des_cbc_encrypt(in,out,s,key,&iv,ENC);

    /*
     Print out ciphertexts from  my_des_cbc_encrypt() and  des_cbc_encrypt() to compare
    */
    printf("Plain text: ");
    fp = fopen(argv[3],"r");
    while( ( ch = fgetc(fp) ) != EOF ) {
      printf("%c", ch);
    }
    //printf("\n");
    fclose(fp);

    printf("Cipher text: ");
    fp = fopen(argv[4],"r");
    while( ( ch = fgetc(fp) ) != EOF ) {
      printf("%c", ch);
    }
    printf("\n");
    fclose(fp);

    return 0;
}
#include <openssl/des.h>
#include <sys/time.h> // For time measures
#include <string.h>
#include <ctype.h>

// Encryption/Decryption switches
#define ENC 1
#define DEC 0

// From char to DES_LONG (be aware that c is shifted)
// char→DES型
#define c2l(c,l)    (l =((DES_LONG)(*((c)++))), \
                     l|=((DES_LONG)(*((c)++)))<< 8L, \
                     l|=((DES_LONG)(*((c)++)))<<16L, \
                     l|=((DES_LONG)(*((c)++)))<<24L)

// From DES_LONG to char (be aware that c is shifted)
// DES→char
#define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
                     *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
                     *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
                     *((c)++)=(unsigned char)(((l)>>24L)&0xff))

void write_output(const char *filename, const unsigned char *in)
{
    // Now... Open the file binary with writing capabilities
    FILE *fp = fopen(filename, "wb");

    // If it can't be open, then return an error message
    if (fp == NULL) {fputs ("File error", stderr); exit (1);}

    // Write the in-array to specificed file-location
    fwrite(in, sizeof(unsigned char), strlen((const char *)in), fp);

    // Close the it
    fclose(fp);
}

const unsigned char *read_inputtext(const char *filename)
{
    // Total number of bytes
    unsigned long fsize;

    // Result of reading the file
    size_t result;

    // Now... Open the file binary with reading capabilities
    FILE *fp = fopen(filename, "rb");

    // If it can't be open, then return an error message
    if (fp == NULL) {fputs ("File error",stderr); exit (1);}

    /* Find out the number of bytes */
    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);      /* Get the size of the file */
    rewind(fp);             /* Go back to the start */

    // Allocate the buffer + 1 for termination
    unsigned char* buffer = malloc(fsize * sizeof *buffer + 1);

    // Test that everything went as we expected
    if(buffer == NULL) { fputs("Memory error!", stderr); exit(2); }

    // Read the buffer
    result = fread(buffer, 1, fsize * sizeof *buffer, fp);

    // Something went wrong when we read the file; sizes to not match
    if (result != fsize) {fputs ("Reading error", stderr); exit (3);}

    // Terminate the str
    buffer[fsize] = '\0';

    // Close the file
    fclose(fp);

    // Return the pointer to the dynamic allocated array
    return buffer;
}

void str2DES_cblock(const char *str, DES_cblock *out)
{
    // Make a char pointer and point it at the start of the array
    unsigned char *o;
    o = out[0];

    // Read the string
    int i;
    for (i = 0; i < 8; i++)
        sscanf(&(str[i*2]),"%2hhx", o++);
}

void my_des_cbc_encrypt(unsigned char *input, unsigned char *output, long length, DES_key_schedule ks, DES_cblock *ivec, int env){
  /*
    Assume that the input length (in byte) is a multiple of 8
    Try to undestand the macros l2c and c2l. They are important in implementation of CBC
    入力は8の倍数byte
  */

  unsigned char *iv;            // Initialization vector
  long l = length;

  DES_LONG xor0, xor1;
  DES_LONG in0, in1;
  DES_LONG data[2];
  /*
     Addtional variables
  */

  iv = ivec[0];

  //Initialize XOR-variables
  c2l(iv, xor0);
  c2l(iv, xor1);

  //Handling 8 bytes of input data each time inside the for loop.
  for(l = -8; l >= 0; l = -8){
    /*
      Your implementation of DES in CBC mode.
      Using des_encrypt1().
    */
    
  }
}

int main(int argc, char *argv[])
{
    int k;
    des_key_schedule key;
    DES_cblock iv, cbc_key;

    /*
      Other variables
    */
    const unsigned char *in;
    unsigned char *inp,*out;
    unsigned char *des_out;
    long s;
    FILE *fp; // ファイルポインタの作成
    char ch;
    char str[256];
    //DES_cblock in,out;
    /*
      Check number of command line arguments
    */
    if(argc != 5){
      printf("Not enough or too many arguments!\n");
      exit(1);
    }

    /*
      Check key and initialization vector validities. (comprise of Hexadicimal digits or not)
    */
    //16進数かを判定する関数
    if(isxdigit(*argv[1])!=1 || isxdigit(*argv[2])!=1) {
      if(isxdigit(*argv[1])!=1) printf("iv is not hexadecimal.\n");
      if(isxdigit(*argv[2])!=1) printf("key is not hexadecimal.\n");
      exit(2);
    }
    /*
      Convert key and initialization vector from string to DES_cblock
    */
    str2DES_cblock(argv[1],&iv);
    str2DES_cblock(argv[2],&cbc_key);
    //str2DES_cblock(argv[3],&in);
    //str2DES_cblock(argv[4],&out);
    /*
      read_inputtext();
    */
    fp = fopen(argv[3],"r");
    while(fgets(str, 256, fp) != NULL) {}
    fclose(fp);
    s = strlen(str) - 1;

    in = read_inputtext(argv[3]);
    inp = in;
    printf("%s",inp);

    /*
      my_des_cbc_encrypt();
    */

    //void my_des_cbc_encrypt(unsigned char *input, unsigned char *output, long length, DES_key_schedule ks, DES_cblock *ivec, int env){
    //my_des_cbc_encrypt(inp,out,s,key,&iv,ENC);
    //printf("%s\n", out);
    /*
      write_output();
    */
    //write_output(argv[4],x);

    //Compare the resutl with that using built-in funtion des_cbc_encrypt(). Details of des_cbc_encrypt() can be seen at http://web.mit.edu/macdev/Development/MITKerberos/MITKerberosLib/DESLib/Documentation/api.html
    /*
      des_cbc_encrypt();
    */
    des_cbc_encrypt(in,out,s,key,&iv,ENC);

    /*
     Print out ciphertexts from  my_des_cbc_encrypt() and  des_cbc_encrypt() to compare
    */
    printf("Plain text: ");
    fp = fopen(argv[3],"r");
    while( ( ch = fgetc(fp) ) != EOF ) {
      printf("%c", ch);
    }
    //printf("\n");
    fclose(fp);

    printf("Cipher text: ");
    fp = fopen(argv[4],"r");
    while( ( ch = fgetc(fp) ) != EOF ) {
      printf("%c", ch);
    }
    printf("\n");
    fclose(fp);

    return 0;
}

年末年始は機械学習・深層学習を勉強しませんか?
No one still commented. Please first comment.
年末年始は機械学習・深層学習を勉強しませんか?
広告
未経験から最短でエンジニアへの転職を目指すなら