Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

encrypt

encryptライブラリサンプルコード

#ifndef _ENCRYPT_STRING_H_
#define _ENCRYPT_STRING_H_

#include <stdio.h> 
#include <unistd.h>
#include <string.h>

void byte2bit(const char byte[1], char bit[8]);
void bit2byte(const char bit[8], char byte[1]);
void byte2bit64(const char byte[8], char bit[64]);
void bit2byte64(const char bit[64], char byte[8]);
void setkey_byte(const char key[8]);
void encrypt_byte(char buf[8], int edflag);
void encrypt_string(char *buf, int edflag, int num_block);

#endif

/**
 * Convert a character representing a byte to 8 characters representing 8 bits (0 or 1). 
 *
 * @param byte A character representing a byte.
 * @param bit 8 characters representing 8 bits
 */
void byte2bit(const char byte[1], char bit[8])
{
  int j;
  for (j = 0; j < 8; j++) { 
    bit[j] = byte[0] >> j & 0x01; 
  } 
}

/**
 * Convert 8 characters representing 8 bits (0 or 1) to a character representing a byte.
 *
 * @param bit 8 characters representing 8 bits.
 * @param byte A character representing a byte.
 */
void bit2byte(const char bit[8], char byte[1])
{
  byte[0] = 0;
  int j;
  for (j = 0; j < 8; j++) {  
    byte[0] |= bit[j] << j;  
  }
}

/**
 * Convert 8 characters representing 8 bytes to 64 characters representing 64 bits. 
 *
 * @param byte 8 character representing 8 byte.
 * @param bit 64 characters representing 64 bits
 */
void byte2bit64(const char byte[8], char bit[64])
{
  static int num_bytes = 8; // 64 / 8
  int i;
  for (i = 0; i < num_bytes; i++) {
    byte2bit(byte + i, bit + i * 8);
  }
}

/**
 * Convert 64 characters representing 64 bits to 8 characters representing 8 bytes. 
 *
 * @param bit 64 characters representing 64 bits
 * @param byte 8 character representing 8 byte.
 */
void bit2byte64(const char bit[64], char byte[8])
{
  static int num_bytes = 8; // 64 / 8
  int i;
  for (i = 0; i < num_bytes; i++) {
    bit2byte(bit + i * 8, byte + i);
  }
}

/**
 * setkey() by 8 characters.
 * 
 * Original setkey() receives 64 characters where each element represents a bit (0 or 1).
 * This method accepts 8 characters where each element represents a byte. 
 *
 * @param key 8 bytes key.
 */
void setkey_byte(const char key[8])
{
  char bitkey[64];
  byte2bit64(key, bitkey);
  setkey(bitkey);
}

/**
 * encrypt() by 8 characters.
 * 
 * Original encrypt() receives 64 characters where each element represents a bit (0 or 1).
 * This method accepts 8 characters where each element represents a byte. 
 *
 * @param buf 8 bytes data to be encrypted.
 * @param edflag Encrypt (0) or decrypt (1).
 */
void encrypt_byte(char buf[8], int edflag)
{
  char bitbuf[64];
  byte2bit64(buf, bitbuf);
  encrypt(bitbuf, edflag);
  bit2byte64(bitbuf, buf);
}

/**
 * encrypt() a string.
 *
 * @param buf A character string, whose length must be a multiple of 8 (bytes).
 * @param edflag Encrypt(0) or decrypt(1).
 * @param num_block The number of blocks. 8 characters per a block. strlen(buf) / 8.
 */
void encrypt_string(char *buf, int edflag, int num_block)
{
  int i;
  for (i = 0; i < num_block; i++) {
    encrypt_byte(buf + i * 8, edflag);
  }
}

int main(void) { 
  char key[64]; 
  char orig[9] = "eggplant"; 
  char buf[64]; 
  char txt[9]; 
  int i, j; 
  for (i = 0; i < 64; i++) { 
    key[i] = rand() & 1; 
  }
  for (i = 0; i < 8; i++) { 
    for (j = 0; j < 8; j++) { 
      buf[i * 8 + j] = orig[i] >> j & 1; 
    } 
    setkey(key); 
  } 
  printf("Before encrypting: %s\n", orig); 
  encrypt(buf, 0); 
  for (i = 0; i < 8; i++) { 
    for (j = 0, txt[i] = '\0'; j < 8; j++) { 
      txt[i] |= buf[i * 8 + j] << j;  
    }  
    txt[8] = '\0';  
  }  
  printf("After encrypting: %s\n", txt);  
  
  encrypt(buf, 1);  
  for (i = 0; i < 8; i++) {  
    for (j = 0, txt[i] = '\0'; j < 8; j++) {  
      txt[i] |= buf[i * 8 + j] << j;  
    }  
    txt[8] = '\0';  
  }  
  printf("After decrypting: %s\n", txt);
 
  //char salt[8] = "fdsa";
  //setkey_byte(salt);
  setkey(key);
  char passwd[9] = "eggplant";
  encrypt_byte(passwd, 0);
  puts(passwd);
  encrypt_byte(passwd, 1);
  puts(passwd);
 
 
  char salt[8] = "nseodd";
  setkey_byte(salt);
  char strpasswd[16] = "vkdke987dfad";
  encrypt_string(strpasswd, 0, 16 / 8);
  printf("%d %s\n", strlen(strpasswd), strpasswd);
  encrypt_string(strpasswd, 1, 16 / 8);
  printf("%d %s\n", strlen(strpasswd), strpasswd);
 
  return 0; 
}