码迷,mamicode.com
首页 > 其他好文 > 详细

把代码加密成乱码?不如让代码隐身吧!

时间:2014-10-03 21:20:45      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   strong   

 

先声明,娱乐向,至于有没有实用价值就不知道了。。。轻喷哈。

 

今天在网上乱逛的时候看到了陈浩大牛的这篇博客:http://coolshell.cn/articles/1142.html

感觉里面说的WhiteSpace语言很有意思。维基百科: http://zh.wikipedia.org/wiki/Whitespace

一般的编程语言都会忽略空白字符,而WhiteSpace却只识别空白字符,优点竟然是“不能把代码打印带走”。。。

 

虽然不知道有没有人真的用这个语言做项目开发,但是这给了我们一个思路:把代码加密成空白字符。

然后我就用C语言写了一个,功能很简陋,主要是:

1、加密和解密任何文本

2、执行加密的Lua代码

 

比如Lua版的Hello World加密后是这样的:

bubuko.com,布布扣
 1                                                                                                                 
 2   
 3                                     
 4      
 5       
 6                                                                                                                                                                                                                                                                                                                 
 7                         
 8                                       
 9                              
10        
11 
12    
13                                                                                                                                                                                                                                                                                                                             
14                                                        
15                         
16    
17                         
18                                 
19                                                                                                                                                                                                                                                                         
20        
View Code

你说你看不见?当然,笨蛋是看不见的。(开个玩笑,by 《皇帝的新装》。。。)

 

把程序暂时命名为White,用法就是在控制台输入:

加密:

White -encode [源文件] [输出文件]

解密:

White -decode [源文件] [输出文件]

执行加密后的文件:

White -run [源文件]

 

最后附上我刚写的C语言代码,写的很简陋,函数的返回值都是bool的,当执行失败的时候返回false,显然我没写一行错误处理,直接返回true了,这样更精简。。。

bubuko.com,布布扣
  1 #define _CRT_SECURE_NO_WARNINGS
  2 
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 extern "C" {
  7     #include <lua.h>
  8     #include <lauxlib.h>
  9     #include <lualib.h>
 10 }
 11 
 12 // 从文件中读取内容
 13 bool readFromFile(const char* fileName, char*& content)
 14 {
 15     auto file = fopen(fileName, "r");
 16     fseek(file, 0, SEEK_END);
 17     auto fileSize = ftell(file);
 18     fseek(file, 0, SEEK_SET);
 19     content = (char *)malloc(fileSize * sizeof(char));
 20     fread(content, sizeof(char), fileSize, file);
 21     fclose(file);
 22     content[fileSize] = 0;
 23     return true;
 24 }
 25 
 26 // 将内容写入到文件
 27 bool writeToFile(const char* fileName, const char* content)
 28 {
 29     auto file = fopen(fileName, "w");
 30     int fileSize = 0;
 31     while (content[fileSize] != 0) {
 32         fileSize++;
 33     }
 34     fwrite(content, sizeof(char), fileSize, file);
 35     fclose(file);
 36     return true;
 37 }
 38 
 39 // 加密
 40 bool encode(const char* src, char* tar)
 41 {
 42     int count = 0;
 43     char pointer = 0;
 44     for (int i = 0; src[i] != 0; i++) {
 45         char cur = src[i];
 46         for (; pointer < cur; pointer++) {
 47             tar[count++] =  ;
 48         }
 49         for (; pointer > cur; pointer--) {
 50             tar[count++] = \t;
 51         }
 52         tar[count++] = \n;
 53     }
 54     tar[count] = 0;
 55     return true;
 56 }
 57 
 58 // 解密
 59 bool decode(const char* src, char* tar)
 60 {
 61     int count = 0;
 62     char ch = 0;
 63     for (int i = 0; src[i] != 0; i++) {
 64         switch (src[i]) {
 65         case  :
 66             ch++; break;
 67         case \t:
 68             ch--; break;
 69         case \n:
 70             tar[count++] = ch;
 71         }
 72     }
 73     tar[count] = 0;
 74     return true;
 75 }
 76 
 77 int main(int argc, char* argv[])
 78 {
 79     if (strcmp(argv[1], "-encode") == 0) {
 80         char* content = nullptr;
 81         char ch_encode[1024 * 8];
 82         readFromFile(argv[2], content);
 83         encode(content, ch_encode);
 84         writeToFile(argv[3], ch_encode);
 85     }
 86     else if (strcmp(argv[1], "-decode") == 0) {
 87         char* content = nullptr;
 88         char ch_decode[1024 * 8];
 89         readFromFile(argv[2], content);
 90         decode(content, ch_decode);
 91         writeToFile(argv[3], ch_decode);
 92     }
 93     else if (strcmp(argv[1], "-run") == 0) {
 94         char* content = nullptr;
 95         char ch_decode[1024 * 8];
 96         readFromFile(argv[2], content);
 97         decode(content, ch_decode);
 98         lua_State *L = luaL_newstate();
 99         luaL_openlibs(L);
100         luaL_dostring(L, ch_decode);
101         lua_close(L);
102     }
103     return 0;
104 }
View Code

 

补充一点,Lua的源码需要写在一行里,可以ctrl+F替换回车。请大牛指教怎么用luaL_doString()一次执行多行代码。

把代码加密成乱码?不如让代码隐身吧!

标签:style   blog   http   color   io   os   ar   for   strong   

原文地址:http://www.cnblogs.com/wolfred7464/p/4005186.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!