PHP使用異或加密解密文件

PHP使用異或加密解密文件

  1. 原理:將文件的每一個字節與密鑰的單位做異或處理
  2.  <?php
     
     /**
      * 文件加密解密方法
      * @param string $input 源文件
      * @param string $ouput 加密文件
      * @param string $key 密鑰
      */
     function file_crypt ( $input , $ouput , $key ) {
     
         if (!file_exists($input)) {
             throw new Exception("Input Source Error", 1);
         }
         if (!$ouput || !$key) {
             throw new Exception("Ouput Or Key Error", 1);
         }
         // 加密後的字符串
         $content = '';
         // 密鑰長度
         $keylen = strlen($key);
         // 索引
         $sublen = $keylen - 1;
         // 索引
         $index = 0;
     
         $fp = fopen($input, 'rb');
     
         while (!feof($fp)) {
             // 讀取一個字節
             $tmp = fread($fp, 1);
             // 內容與密鑰的字節做異或處理
             $content .= $tmp ^ substr($key, $index % $keylen, 1);
             // 索引規律變化 (防止數量遞增)
             if ($index < $sublen) {
                 $index ++;
             } else {
                 $index = 0;
             }
         }
     
         // 關閉資源
         fclose($fp);
     
         return file_put_contents($ouput, $content, true);
     }
     
     file_crypt('./tupian.jpg', './jiami.jpg', 'abcdefg');
    
    

C語言寫的版本

  1.  #define TRUE 1
     #define FALSE 0
      
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     #include <io.h>     // 如果在/usr/include/找不到,可以在/usr/include/sys/複製過去
      
      
     // 輸出信息
     void msg_log(char *str);
      
     // 判斷文件是否存在
     int file_exists(char *filename);
      
      
     // 主函數
     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];    // 加密字串
      
         // 檢查參數
         if(source==NULL || dest==NULL || key==NULL){
             msg_log("param error\nusage:xor_encrypt source dest key\ne.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長度
         keylen = strlen(key);
      
         fSource = fopen(source, "rb");
         fDest = fopen(dest, "wb");
      
         while(!feof(fSource)){
             
             fread(fBuffer, 1, 1, fSource);    // 讀取1字節
             
             if(!feof(fSource)){
                 ckey = key[index%keylen];     // 循環獲取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\n", str);
     }
      
     // 判斷文件是否存在
     int file_exists(char *filename){
         return (access(filename, 0)==0);
     }
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章