亚欧洲精品在线观看,窝窝影院午夜看片,久久国产成人午夜av影院宅,午夜91,免费国产人成网站,ts在线视频,欧美激情在线一区

C語(yǔ)言

C語(yǔ)言如何使用異或(xor)加密或解密文件

時(shí)間:2025-03-26 13:45:19 C語(yǔ)言 我要投稿
  • 相關(guān)推薦

C語(yǔ)言如何使用異或(xor)加密或解密文件

  C語(yǔ)言使用異或(xor)函數(shù)可以加密或解密文件你知道嗎?你知道C語(yǔ)言如何使用異或(xor)加密或解密文件嗎?下面是小編為大家?guī)?lái)的關(guān)于C語(yǔ)言如何使用異或(xor)加密或解密文件的知識(shí),歡迎閱讀。

  C語(yǔ)言如何使用異或(xor)加密或解密文件

  xor_encrypt.c

  /** XOR 加密/解密文件 */

  #define TRUE 1

  #define FALSE 0

  #include

  #include

  #include

  #include // 如果在/usr/include/找不到,可以在/usr/include/sys/復(fù)制過(guò)去

  // 輸出信息

  void msg_log(char *str);

  // 判斷文件是否存在

  int file_exists(char *filename);

  // 主函數(shù)

  //更多精彩內(nèi)容:http://www.bianceng.cn/Programming/C/

  int main(int argc, char *argv[]){

  int keylen, index=0;

  char *source, *dest, *key, fBuffer[1], tBuffer[20], ckey;

  FILE *fSource, *fDest;

  source = argv[1]; // 原文件

  dest = argv[2]; // 目的文件

  key = argv[3]; // 加密字串

  // 檢查參數(shù)

  if(source==NULL || dest==NULL || key==NULL){

  msg_log("param error usage:xor_encrypt source dest key e.g ./xor_encrypt o.txt d.txt 123456");

  exit(0);

  }

  // 判斷原文件是否存在

  if(file_exists(source)==FALSE){

  sprintf(tBuffer,"%s not exists",source);

  msg_log(tBuffer);

  exit(0);

  }

  // 獲取key長(zhǎng)度

  keylen = strlen(key);

  fSource = fopen(source, "rb");

  fDest = fopen(dest, "wb");

  while(!feof(fSource)){

  fread(fBuffer, 1, 1, fSource); // 讀取1字節(jié)

  if(!feof(fSource)){

  ckey = key[index%keylen]; // 循環(huán)獲取key

  *fBuffer = *fBuffer ^ ckey; // xor encrypt

  fwrite(fBuffer, 1, 1, fDest); // 寫入文件

  index ++;

  }

  }

  fclose(fSource);

  fclose(fDest);

  msg_log("success");

  exit(0);

  }

  //輸出信息

  void msg_log(char *str){

  printf("%s ", str);

  }

  // 判斷文件是否存在

  int file_exists(char *filename){

  return (access(filename, 0)==0);

  }

  這張圖如果使用php來(lái)處理需要 2秒 左右,但用C處理只需要 130毫秒。

  fdipzone@ubuntu:~/C$ gcc -o xor_encrypt xor_encrypt.c

  fdipzone@ubuntu:~/C$ time ./xor_encrypt 1280.jpg 1280en.jpg '@#$%^&*()_DFGHJKadsklfjasdf'

  success

  real 0m0.139s

  user 0m0.060s

  sys 0m0.070s


【C語(yǔ)言如何使用異或(xor)加密或解密文件】相關(guān)文章:

C語(yǔ)言文件的使用方法08-01

PHP如何使用AES加密算法進(jìn)行數(shù)據(jù)加密和解密06-07

C語(yǔ)言EOF如何使用08-29

C語(yǔ)言文件08-28

C語(yǔ)言中如何使用sscanf08-27

如何使用C語(yǔ)言數(shù)組指針09-14

BIOS設(shè)置的加密與解密07-04

C語(yǔ)言頭文件如何避免重復(fù)包含06-01

C語(yǔ)言的文件概念07-18