哪里有c++加密的源码,最好是可以用 g++编译的

作者&投稿:通庆 (若有异议请与网页底部的电邮联系)
c++源码加密~

不加密都看不明白。。。

*

运行 gcc/egcs
*

gcc/egcs 的主要选项
*

gdb
*

gdb 的常用命令
*

gdb 使用范例
*

其他程序/库工具 (ar, objdump, nm, size, strings, strip, ...)
* 创建和使用静态库
* 创建和使用共享库
* 使用高级共享库特性

1.7.1 运行 gcc/egcs

Linux 中最重要的软件开发工具是 GCC。GCC 是 GNU 的 C 和 C++ 编译器。实际上,GCC 能够编译三种语言:C、C++ 和 Object C(C 语言的一种面向对象扩展)。利用 gcc 命令可同时编译并连接 C 和 C++ 源程序。

#DEMO#: hello.c

如果你有两个或少数几个 C 源文件,也可以方便地利用 GCC 编译、连接并生成可执行文件。例如,假设你有两个源文件 main.c 和 factorial.c 两个源文件,现在要编译生成一个计算阶乘的程序。

-----------------------
清单 factorial.c
-----------------------
#include
#include

int factorial (int n)
{
if (n <= 1)
return 1;

else
return factorial (n - 1) * n;
}
-----------------------

-----------------------
清单 main.c
-----------------------
#include
#include

int factorial (int n);

int main (int argc, char **argv)
{
int n;

if (argc < 2) {
printf ("Usage: %s n
", argv [0]);
return -1;
}
else {
n = atoi (argv[1]);
printf ("Factorial of %d is %d.
", n, factorial (n));
}

return 0;
}
-----------------------

利用如下的命令可编译生成可执行文件,并执行程序:
$ gcc -o factorial main.c factorial.c
$ ./factorial 5
Factorial of 5 is 120.

GCC 可同时用来编译 C 程序和 C++ 程序。一般来说,C 编译器通过源文件的后缀名来判断是 C 程序还是 C++ 程序。在 Linux 中,C 源文件的后缀名为 .c,而 C++ 源文件的后缀名为 .C 或 .cpp。

但是,gcc 命令只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库连接。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。假设我们有一个如下的 C++ 源文件(hello.C):

#include

void main (void)
{
cout << "Hello, world!" << endl;
}

则可以如下调用 g++ 命令编译、连接并生成可执行文件:

$ g++ -o hello hello.C
$ ./hello
Hello, world!

1.7.2 gcc/egcs 的主要选项

表 1-3 gcc 命令的常用选项
选项 解释
-ansi 只支持 ANSI 标准的 C 语法。这一选项将禁止 GNU C 的某些特色,
例如 asm 或 typeof 关键词。
-c 只编译并生成目标文件。
-DMACRO 以字符串“1”定义 MACRO 宏。
-DMACRO=DEFN 以字符串“DEFN”定义 MACRO 宏。
-E 只运行 C 预编译器。
-g 生成调试信息。GNU 调试器可利用该信息。
-IDIRECTORY 指定额外的头文件搜索路径DIRECTORY。
-LDIRECTORY 指定额外的函数库搜索路径DIRECTORY。
-lLIBRARY 连接时搜索指定的函数库LIBRARY。
-m486 针对 486 进行代码优化。
-o FILE 生成指定的输出文件。用在生成可执行文件时。
-O0 不进行优化处理。
-O 或 -O1 优化生成代码。
-O2 进一步优化。
-O3 比 -O2 更进一步优化,包括 inline 函数。
-shared 生成共享目标文件。通常用在建立共享库时。
-static 禁止使用共享连接。
-UMACRO 取消对 MACRO 宏的定义。
-w 不生成任何警告信息。
-Wall 生成所有警告信息。

#DEMO#

MiniGUI 的编译选项
1.7.3 gdb

GNU 的调试器称为 gdb,该程序是一个交互式工具,工作在字符模式。在 X Window 系统中,
有一个 gdb 的前端图形工具,称为 xxgdb。gdb 是功能强大的调试程序,可完成如下的调试
任务:
* 设置断点;
* 监视程序变量的值;
* 程序的单步执行;
* 修改变量的值。
在可以使用 gdb 调试程序之前,必须使用 -g 选项编译源文件。可在 makefile 中如下定义
CFLAGS 变量:
CFLAGS = -g
运行 gdb 调试程序时通常使用如下的命令:
gdb progname

在 gdb 提示符处键入help,将列出命令的分类,主要的分类有:
* aliases:命令别名
* breakpoints:断点定义;
* data:数据查看;
* files:指定并查看文件;
* internals:维护命令;
* running:程序执行;
* stack:调用栈查看;
* statu:状态查看;
* tracepoints:跟踪程序执行。
键入 help 后跟命令的分类名,可获得该类命令的详细清单。

#DENO#
1.7.4 gdb 的常用命令

表 1-4 常用的 gdb 命令
命令 解释
break NUM 在指定的行上设置断点。
bt 显示所有的调用栈帧。该命令可用来显示函数的调用顺序。
clear 删除设置在特定源文件、特定行上的断点。其用法为:clear FILENAME:NUM。
continue 继续执行正在调试的程序。该命令用在程序由于处理信号或断点而
导致停止运行时。
display EXPR 每次程序停止后显示表达式的值。表达式由程序定义的变量组成。
file FILE 装载指定的可执行文件进行调试。
help NAME 显示指定命令的帮助信息。
info break 显示当前断点清单,包括到达断点处的次数等。
info files 显示被调试文件的详细信息。
info func 显示所有的函数名称。
info local 显示当函数中的局部变量信息。
info prog 显示被调试程序的执行状态。
info var 显示所有的全局和静态变量名称。
kill 终止正被调试的程序。
list 显示源代码段。
make 在不退出 gdb 的情况下运行 make 工具。
next 在不单步执行进入其他函数的情况下,向前执行一行源代码。
print EXPR 显示表达式 EXPR 的值。



1.7.5 gdb 使用范例

-----------------
清单 一个有错误的 C 源程序 bugging.c
-----------------
#include
#include

static char buff [256];
static char* string;
int main ()
{

printf ("Please input a string: ");
gets (string);

printf ("
Your string is: %s
", string);
}
-----------------
上面这个程序非常简单,其目的是接受用户的输入,然后将用户的输入打印出来。该程序使用了
一个未经过初始化的字符串地址 string,因此,编译并运行之后,将出现 Segment Fault 错误:
$ gcc -o test -g test.c
$ ./test
Please input a string: asfd
Segmentation fault (core dumped)
为了查找该程序中出现的问题,我们利用 gdb,并按如下的步骤进行:
1.运行 gdb bugging 命令,装入 bugging 可执行文件;
2.执行装入的 bugging 命令;
3.使用 where 命令查看程序出错的地方;
4.利用 list 命令查看调用 gets 函数附近的代码;
5.唯一能够导致 gets 函数出错的因素就是变量 string。用 print 命令查看 string 的值;
6.在 gdb 中,我们可以直接修改变量的值,只要将 string 取一个合法的指针值就可以了,为
此,我们在第 11 行处设置断点;
7.程序重新运行到第 11 行处停止,这时,我们可以用 set variable 命令修改 string 的取值;
8.然后继续运行,将看到正确的程序运行结果。



#DEMO#


1.7.6 其他程序/库工具

strip:

nm:

size:

string:


1.7.7 创建和使用静态库


创建一个静态库是相当简单的。通常使用 ar 程序把一些目标文件(.o)组合在一起,成为一个单独的库,然后运行 ranlib,以给库加入一些索引信息。


1.7.8 创建和使用共享库

特殊的编译和连接选项

-D_REENTRANT 使得预处理器符号 _REENTRANT 被定义,这个符号激活一些宏特性。
-fPIC 选项产生位置独立的代码。由于库是在运行的时候被调入,因此这个
选项是必需的,因为在编译的时候,装入内存的地址还不知道。如果
不使用这个选项,库文件可能不会正确运行。
-shared 选项告诉编译器产生共享库代码。
-Wl,-soname -Wl 告诉编译器将后面的参数传递到连接器。而 -soname 指定了
共享库的 soname。



# 可以把库文件拷贝到 /etc/ld.so.conf 中列举出的任何目录中,并以
root 身份运行 ldconfig;或者
# 运行 export LD_LIBRARY_PATH='pwd',它把当前路径加到库搜索路径中去。



1.7.9 使用高级共享库特性

1. ldd 工具

ldd 用来显示执行文件需要哪些共享库, 共享库装载管理器在哪里找到了需要的共享库.


2. soname

共享库的一个非常重要的,也是非常难的概念是 soname——简写共享目标名(short for shared object name)。这是一个为共享库(.so)文件而内嵌在控制数据中的名字。如前面提到的,每一个程序都有一个需要使用的库的清单。这个清单的内容是一系列库的 soname,如同 ldd 显示的那样,共享库装载器必须找到这个清单。


soname 的关键功能是它提供了兼容性的标准。当要升级系统中的一个库时,并且新库的 soname 和老的库的 soname 一样,用旧库连接生成的程序,使用新的库依然能正常运行。这个特性使得在 Linux 下,升级使用共享库的程序和定位错误变得十分容易。



在 Linux 中,应用程序通过使用 soname,来指定所希望库的版本。库作者也可以通过保留或者改变 soname 来声明,哪些版本是相互兼容的,这使得程序员摆脱了共享库版本冲突问题的困扰。


查看/usr/local/lib 目录,分析 MiniGUI 的共享库文件之间的关系


3. 共享库装载器

当程序被调用的时候,Linux 共享库装载器(也被称为动态连接器)也自动被调用。它的作用是保证程序所需要的所有适当版本的库都被调入内存。共享库装载器名字是 ld.so 或者是 ld-linux.so,这取决于 Linux libc 的版本,它必须使用一点外部交互,才能完成自己的工作。然而它接受在环境变量和配置文件中的配置信息。


文件 /etc/ld.so.conf 定义了标准系统库的路径。共享库装载器把它作为搜索路径。为了改变这个设置,必须以 root 身份运行 ldconfig 工具。这将更新 /etc/ls.so.cache 文件,这个文件其实是装载器内部使用的文件之一。


可以使用许多环境变量控制共享库装载器的操作(表1-4+)。

表 1-4+ 共享库装载器环境变量
变量 含义
LD_AOUT_LIBRARY_PATH 除了不使用 a.out 二进制格式外,与 LD_LIBRARY_PATH 相同。
LD_AOUT_PRELOAD 除了不使用 a.out 二进制格式外,与 LD_PRELOAD 相同。
LD_KEEPDIR 只适用于 a.out 库;忽略由它们指定的目录。
LD_LIBRARY_PATH 将其他目录加入库搜索路径。它的内容应该是由冒号
分隔的目录列表,与可执行文件的 PATH 变量具有相同的格式。
如果调用设置用户 ID 或者进程 ID 的程序,该变量被忽略。
LD_NOWARN 只适用于 a.out 库;当改变版本号是,发出警告信息。
LD_PRELOAD 首先装入用户定义的库,使得它们有机会覆盖或者重新定义标准库。
使用空格分开多个入口。对于设置用户 ID 或者进程 ID 的程序,
只有被标记过的库才被首先装入。在 /etc/ld.so.perload 中指定
了全局版本号,该文件不遵守这个限制。



4. 使用 dlopen

另外一个强大的库函数是 dlopen()。该函数将打开一个新库,并把它装入内存。该函数主要用来加载库中的符号,这些符号在编译的时候是不知道的。比如 Apache Web 服务器利用这个函数在运行过程中加载模块,这为它提供了额外的能力。一个配置文件控制了加载模块的过程。这种机制使得在系统中添加或者删除一个模块时,都不需要重新编译了。


可以在自己的程序中使用 dlopen()。dlopen() 在 dlfcn.h 中定义,并在 dl 库中实现。它需要两个参数:一个文件名和一个标志。文件名可以是我们学习过的库中的 soname。标志指明是否立刻计算库的依赖性。如果设置为 RTLD_NOW 的话,则立刻计算;如果设置的是 RTLD_LAZY,则在需要的时候才计算。另外,可以指定 RTLD_GLOBAL,它使得那些在以后才加载的库可以获得其中的符号。


当库被装入后,可以把 dlopen() 返回的句柄作为给 dlsym() 的第一个参数,以获得符号在库中的地址。使用这个地址,就可以获得库中特定函数的指针,并且调用装载库中的相应函数。

/* des: duplicate the NBS Data Encryption Standard in software.
* usage: des <file>
* prompts for the password
* If the filename ends in ".n" it will be decrypted with the key;
* otherwise it will be encrypted.
*
* Permutation algorithm:
* The permutation is defined by its effect on each of the 16 nibbles
* of the 64-bit input. For each nibble we give an 8-byte bit array
* that has the bits in the input nibble distributed correctly. The
* complete permutation involves ORing the 16 sets of 8 bytes designated
* by the 16 input nibbles. Uses 16*16*8 = 2K bytes of storage for
* each 64-bit permutation. 32-bit permutations (P) and expansion (E)
* are done similarly, but using bytes instead of nibbles.
* Should be able to use long ints, adding the masks, at a
* later pass. Tradeoff: can speed 64-bit perms up at cost of slowing
* down expansion or contraction operations by using 8K tables here and
* decreasing the size of the other tables.
* The compressions are pre-computed in 12-bit chunks, combining 2 of the
* 6->4 bit compressions.
* The key schedule is also precomputed.
* Compile with VALIDATE defined to run the NBS validation suite.
*
* Jim Gillogly, May 1977
* Modified 8/84 by Jim Gillogly and Lauren Weinstein to compile with
* post-1977 C compilers and systems
*
* This program is now officially in the public domain, and is available for
* any non-profit use as long as the authorship line is retained.
*/

/*#define VALIDATE */ /* define to check the NBS validation suite */
/*#define DEBUG */
/*#define LATTICE */ /* define for Lattice C on IBM PC */

#include <stdio.h>

#ifndef LATTICE
#include <sgtty.h>
#include <signal.h>
#include <sys/types.h> /* for local timer */
#include <sys/timeb.h> /* ditto */

struct sgttyb ttybuf; /* for gtty/stty */
int bye(); /* for caught interrupts */

#endif

char iperm[16][16][8],fperm[16][16][8]; /* inital and final permutations*/
char s[4][4096]; /* S1 thru S8 precomputed */
char p32[4][256][4]; /* for permuting 32-bit f output*/
char kn[16][6]; /* key selections */

endes(inblock,outblock) /* encrypt 64-bit inblock */
char *inblock, *outblock;
{ char iters[17][8]; /* workspace for each iteration */
char swap[8]; /* place to interchange L and R */
register int i;
register char *s, *t;

permute(inblock,iperm,iters[0]);/* apply initial permutation */
for (i=0; i<16; i++) /* 16 churning operations */
iter(i,iters[i],iters[i+1]);
/* don't re-copy to save space */
s = swap; t = &iters[16][4]; /* interchange left */
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
t = &iters[16][0]; /* and right */
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
permute(swap,fperm,outblock); /* apply final permutation */
}

dedes(inblock,outblock) /* decrypt 64-bit inblock */
char *inblock,*outblock;
{ char iters[17][8]; /* workspace for each iteration */
char swap[8]; /* place to interchange L and R */
register int i;
register char *s, *t;

permute(inblock,iperm,iters[0]);/* apply initial permutation */
for (i=0; i<16; i++) /* 16 churning operations */
iter(15-i,iters[i],iters[i+1]);
/* reverse order from encrypting*/
s = swap; t = &iters[16][4]; /* interchange left */
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
t = &iters[16][0]; /* and right */
*s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
permute(swap,fperm,outblock); /* apply final permutation */
}

permute(inblock,perm,outblock) /* permute inblock with perm */
char *inblock, *outblock; /* result into outblock,64 bits */
char perm[16][16][8]; /* 2K bytes defining perm. */
{ register int i,j;
register char *ib, *ob; /* ptr to input or output block */
register char *p, *q;

for (i=0, ob = outblock; i<8; i++)
*ob++ = 0; /* clear output block */
ib = inblock;
for (j = 0; j < 16; j += 2, ib++) /* for each input nibble */
{ ob = outblock;
p = perm[j][(*ib >> 4) & 017];
q = perm[j + 1][*ib & 017];
for (i = 0; i < 8; i++) /* and each output byte */
*ob++ |= *p++ | *q++; /* OR the masks together*/
}
}

char ip[] /* initial permutation P */
= { 58, 50, 42, 34, 26, 18, 10, 2,
60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6,
64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7 };

char fp[] /* final permutation F */
= { 40, 8, 48, 16, 56, 24, 64, 32,
39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30,
37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28,
35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26,
33, 1, 41, 9, 49, 17, 57, 25 };

/* expansion operation matrix */ /* rwo: unused */
/* char ei[] = { 32, 1, 2, 3, 4, 5,
4, 5, 6, 7, 8, 9,
8, 9, 10, 11, 12, 13,
12, 13, 14, 15, 16, 17,
16, 17, 18, 19, 20, 21,
20, 21, 22, 23, 24, 25,
24, 25, 26, 27, 28, 29,
28, 29, 30, 31, 32, 1 }; */

char pc1[] /* permuted choice table (key) */
= { 57, 49, 41, 33, 25, 17, 9,
1, 58, 50, 42, 34, 26, 18,
10, 2, 59, 51, 43, 35, 27,
19, 11, 3, 60, 52, 44, 36,

63, 55, 47, 39, 31, 23, 15,
7, 62, 54, 46, 38, 30, 22,
14, 6, 61, 53, 45, 37, 29,
21, 13, 5, 28, 20, 12, 4 };

char totrot[] /* number left rotations of pc1 */
= { 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };

char pc1m[56]; /* place to modify pc1 into */
char pcr[56]; /* place to rotate pc1 into */

char pc2[] /* permuted choice key (table) */
= { 14, 17, 11, 24, 1, 5,
3, 28, 15, 6, 21, 10,
23, 19, 12, 4, 26, 8,
16, 7, 27, 20, 13, 2,
41, 52, 31, 37, 47, 55,
30, 40, 51, 45, 33, 48,
44, 49, 39, 56, 34, 53,
46, 42, 50, 36, 29, 32 };

char si[8][64] /* 48->32 bit compression tables*/
= { /* S[1] */
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
/* S[2] */
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
/* S[3] */
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
/* S[4] */
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
/* S[5] */
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
/* S[6] */
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
/* S[7] */
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
/* S[8] */
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 };

char p32i[] /* 32-bit permutation function */
= { 16, 7, 20, 21,
29, 12, 28, 17,
1, 15, 23, 26,
5, 18, 31, 10,
2, 8, 24, 14,
32, 27, 3, 9,
19, 13, 30, 6,
22, 11, 4, 25 };

desinit(key) /* initialize all des arrays */
char *key;
{
#ifdef DEBUG
/*deb*/ printf("Initial perm init.\n");
#endif
perminit(iperm,ip); /* initial permutation */
#ifdef DEBUG
/*deb*/ printf("Final perm init.\n");
#endif
perminit(fperm,fp); /* final permutation */
#ifdef DEBUG
/*deb*/ printf("Key sched init.\n");
#endif
kinit(key); /* key schedule */
#ifdef DEBUG
/*deb*/ printf("Compression init.\n");
#endif
sinit(); /* compression functions */

#ifdef DEBUG
/*deb*/ printf("32-bit perm init.\n");
#endif
p32init(); /* 32-bit permutation in f */
#ifdef DEBUG
/*deb*/ printf("End init.\n");
#endif
}

int bytebit[] /* bit 0 is left-most in byte */
= { 0200,0100,040,020,010,04,02,01 };

int nibblebit[] = { 010,04,02,01 };

sinit() /* initialize s1-s8 arrays */
{ register int i,j;

for (i=0; i<4; i++) /* each 12-bit position */
for (j=0; j<4096; j++) /* each possible 12-bit value */
s[i][j]=(getcomp(i*2,j>>6)<<4) |
(017&getcomp(i*2+1,j&077));
/* store 2 compressions per char*/
}

getcomp(k,v) /* 1 compression value for sinit*/
int k,v;
{ register int i,j; /* correspond to i and j in FIPS*/

i=((v&040)>>4)|(v&1); /* first and last bits make row */
j=(v&037)>>1; /* middle 4 bits are column */
return (int) si[k][(i<<4)+j]; /* result is ith row, jth col */
}

kinit(key) /* initialize key schedule array*/
char *key; /* 64 bits (will use only 56) */
{ register int i,j,l;
int m;

for (j=0; j<56; j++) /* convert pc1 to bits of key */
{ l=pc1[j]-1; /* integer bit location */
m = l & 07; /* find bit */
pc1m[j]=(key[l>>3] & /* find which key byte l is in */
bytebit[m]) /* and which bit of that byte */
? 1 : 0; /* and store 1-bit result */
}
for (i=0; i<16; i++) /* for each key sched section */
for (j=0; j<6; j++) /* and each byte of the kn */
kn[i][j]=0; /* clear it for accumulation */
for (i=0; i<16; i++) /* key chunk for each iteration */
{ for (j=0; j<56; j++) /* rotate pc1 the right amount */
pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
/* rotate left and right halves independently */
for (j=0; j<48; j++) /* select bits individually */
if (pcr[pc2[j]-1]) /* check bit that goes to kn[j] */
{ l= j & 07;
kn[i][j>>3] |= bytebit[l];
} /* mask it in if it's there */
}
}

p32init() /* initialize 32-bit permutation*/
{ register int l, j, k;
int i,m;

for (i=0; i<4; i++) /* each input byte position */
for (j=0; j<256; j++) /* all possible input bytes */
for (k=0; k<4; k++) /* each byte of the mask */
p32[i][j][k]=0; /* clear permutation array */
for (i=0; i<4; i++) /* each input byte position */
for (j=0; j<256; j++) /* each possible input byte */
for (k=0; k<32; k++) /* each output bit position */
{ l=p32i[k]-1; /* invert this bit (0-31) */
if ((l>>3)!=i) /* does it come from input posn?*/
continue; /* if not, bit k is 0 */
if (!(j&bytebit[l&07]))
continue; /* any such bit in input? */
m = k & 07; /* which bit is it? */
p32[i][j][k>>3] |= bytebit[m];
}
}

perminit(perm,p) /* initialize a perm array */
char perm[16][16][8]; /* 64-bit, either init or final */
char p[64];
{ register int l, j, k;
int i,m;

for (i=0; i<16; i++) /* each input nibble position */
for (j=0; j<16; j++) /* all possible input nibbles */
for (k=0; k<8; k++) /* each byte of the mask */
perm[i][j][k]=0;/* clear permutation array */
for (i=0; i<16; i++) /* each input nibble position */
for (j = 0; j < 16; j++)/* each possible input nibble */
for (k = 0; k < 64; k++)/* each output bit position */
{ l = p[k] - 1; /* where does this bit come from*/
if ((l >> 2) != i) /* does it come from input posn?*/
continue; /* if not, bit k is 0 */
if (!(j & nibblebit[l & 3]))
continue; /* any such bit in input? */
m = k & 07; /* which bit is this in the byte*/
perm[i][j][k>>3] |= bytebit[m];
}
}

iter(num,inblock,outblock) /* 1 churning operation */
int num; /* i.e. the num-th one */
char *inblock, *outblock; /* 64 bits each */
{ char fret[4]; /* return from f(R[i-1],key) */
register char *ib, *ob, *fb;
/* register int i; */ /* rwo: unused */

ob = outblock; ib = &inblock[4];
f(ib, num, fret); /* the primary transformation */
*ob++ = *ib++; /* L[i] = R[i-1] */
*ob++ = *ib++;
*ob++ = *ib++;
*ob++ = *ib++;
ib = inblock; fb = fret; /* R[i]=L[i] XOR f(R[i-1],key) */
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
*ob++ = *ib++ ^ *fb++;
}

f(right,num,fret) /* critical cryptographic trans */
char *right, *fret; /* 32 bits each */
int num; /* index number of this iter */
{ register char *kb, *rb, *bb; /* ptr to key selection &c */
char bigright[6]; /* right expanded to 48 bits */
char result[6]; /* expand(R) XOR keyselect[num] */
char preout[4]; /* result of 32-bit permutation */

kb = kn[num]; /* fast version of iteration */
bb = bigright;
rb = result;
expand(right,bb); /* expand to 48 bits */
*rb++ = *bb++ ^ *kb++; /* expanded R XOR chunk of key */
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
*rb++ = *bb++ ^ *kb++;
contract(result,preout); /* use S fns to get 32 bits */
perm32(preout,fret); /* and do final 32-bit perm */
}

perm32(inblock,outblock) /* 32-bit permutation at end */
char *inblock,*outblock; /* of the f crypto function */
{ register int j;
/* register int i; */ /* rwo: unused */
register char *ib, *ob;
register char *q;

ob = outblock; /* clear output block */
*ob++ = 0; *ob++ = 0; *ob++ = 0; *ob++ = 0;
ib=inblock; /* ptr to 1st byte of input */
for (j=0; j<4; j++, ib++) /* for each input byte */
{ q = p32[j][*ib & 0377];
ob = outblock; /* and each output byte */
*ob++ |= *q++; /* OR the 16 masks together */
*ob++ |= *q++;
*ob++ |= *q++;
*ob++ |= *q++;
}
}

expand(right,bigright) /* 32 to 48 bits with E oper */
char *right,*bigright; /* right is 32, bigright 48 */
{
register char *bb, *r, r0, r1, r2, r3;

bb = bigright;
r = right; r0 = *r++; r1 = *r++; r2 = *r++; r3 = *r++;
*bb++ = ((r3 & 0001) << 7) | /* 32 */
((r0 & 0370) >> 1) | /* 1 2 3 4 5 */
((r0 & 0030) >> 3); /* 4 5 */
*bb++ = ((r0 & 0007) << 5) | /* 6 7 8 */
((r1 & 0200) >> 3) | /* 9 */
((r0 & 0001) << 3) | /* 8 */
((r1 & 0340) >> 5); /* 9 10 11 */
*bb++ = ((r1 & 0030) << 3) | /* 12 13 */
((r1 & 0037) << 1) | /* 12 13 14 15 16 */
((r2 & 0200) >> 7); /* 17 */
*bb++ = ((r1 & 0001) << 7) | /* 16 */
((r2 & 0370) >> 1) | /* 17 18 19 20 21 */
((r2 & 0030) >> 3); /* 20 21 */
*bb++ = ((r2 & 0007) << 5) | /* 22 23 24 */
((r3 & 0200) >> 3) | /* 25 */
((r2 & 0001) << 3) | /* 24 */
((r3 & 0340) >> 5); /* 25 26 27 */
*bb++ = ((r3 & 0030) << 3) | /* 28 29 */
((r3 & 0037) << 1) | /* 28 29 30 31 32 */
((r0 & 0200) >> 7); /* 1 */
}

contract(in48,out32) /* contract f from 48 to 32 bits*/
char *in48,*out32; /* using 12-bit pieces into bytes */
{ register char *c;
register char *i;
register int i0, i1, i2, i3, i4, i5;

i = in48;
i0 = *i++; i1 = *i++; i2 = *i++; i3 = *i++; i4 = *i++; i5 = *i++;
c = out32; /* do output a byte at a time */
*c++ = s[0][07777 & ((i0 << 4) | ((i1 >> 4) & 017 ))];
*c++ = s[1][07777 & ((i1 << 8) | ( i2 & 0377 ))];
*c++ = s[2][07777 & ((i3 << 4) | ((i4 >> 4) & 017 ))];
*c++ = s[3][07777 & ((i4 << 8) | ( i5 & 0377 ))];
}

/* End of DES algorithm (except for calling desinit below) */

#ifndef VALIDATE
char *inname, *outname;
FILE *infile, *outfile;

int encrypting;
char buf[512];
char keyx[9], keyy[9];

char *malloc(), *strcpy(), *strcat();

main(argc, argv)
int argc; char *argv[];
{ register char *u;
char *filename;

if (argc < 2) /* filenames given? */
{ fprintf(stderr, "Usage: des file ...\n");
exit(1);
}

for (++argv; --argc; ++argv)
{ inname = *argv;
outname = filename = malloc((unsigned) strlen(inname) + 3);
strcpy(filename, inname);
u = &filename[strlen(filename) - 2]; /* check last 2 chars */

encrypting = (strcmp(".n", u) != 0);
if (!encrypting) *u = 0; /* strip .n from output filename */
else strcat(filename, ".n"); /* or add .n to output file */

if ((infile = fopen(inname, "rb")) == NULL)
{ fprintf(stderr,"Can't read %s.\n", inname);
exit(1);
}
if ((outfile = fopen(outname, "rb")) != NULL)
{ fprintf(stderr, "%s would be overwritten.\n",outname);
exit(1);
}
if ((outfile = fopen(outname, "wb")) == NULL)
{ fprintf(stderr,"Can't write %s.\n", outname);
exit(1);
}

key_get("Type password for ");
for (;;)
{ strcpy(keyx, keyy);
key_get("Verify password for ");
if (strcmp(keyx, keyy) == 0) break;
}
desinit(keyx); /* set up tables for DES */

if (pfile() == 0) unlink(inname);
else fprintf(stderr,
"%s: I/O Error -- File unchanged\n", inname);

fclose(outfile);
fclose(infile);
}
exit(0);
}

key_get(mes) /* get file key */
char *mes;
{ register int i, j;
char linebuf[256];
int count;

for (i=0; i<14; i++) keyy[i]=0;

#ifdef LATTICE
#else
gtty(0, &ttybuf);
ttybuf.sg_flags &= ~ECHO; /* turn off echoing */
signal(SIGINT, bye); /* catch ints */
stty(0, &ttybuf);
#endif

printf("%s%s: ", mes, inname);
fflush(stdout);

count = read(0, linebuf, 256); /* read input line */
printf("\n");

#ifndef LATTICE
ttybuf.sg_flags |= ECHO; /* restore echo */
stty(0, &ttybuf);
#endif

linebuf[count] = 0; /* null terminate */
if (linebuf[count-1] == '\n') /* ignore any terminating newline */
{ linebuf[count-1] = 0;
count--;
}
if (count > 8) count = 8; /* only use 8 chars */
for (i = j = 0; count--;)
keyy[i++] = linebuf[j++];
}

pfile() /* process the file */
{ register int m, nsave;
register char *b;
int j;

while (m = fread(buf, 1, 512, infile))
{
if ((nsave = m) < 0) /* read error */
return(-1);
for (b=buf; m>0; /* encrypt/decrypt 1 buffer-full*/
m -= 8, b += 8) /* 8-byte blocks */
{ if (encrypting)
{ if (m<8) /* don't have a full 64 bits */
{ for (j=0; j<8-m; j++)
b[m+j]=garbage(); /* fill block with trash */
nsave += 8-m; /* complete the block */
}
else j=0 /* number of nulls in last block*/
endes(b,b); /* don't need diff input, output*/
}
else /* decrypting */
{ if (m < 8) deout(b, 1); /* last byte in file: count */
else
{ dedes(b, b); /* decrypt and output block */
deout(b, 0);
}
}
}
if (encrypting) if (fwrite(buf, 1, nsave, outfile) != nsave)
return(-1);
}
/* have now encrypted/decrypted the whole file;
* need to append the byte count for the last block if encrypting.
*/
if (encrypting) fputc(8 - j, outfile); /* how many good bytes? */
return(0);
}

int outcount = 0; /* see when caught up with delay*/

deout(block,flag) /* 1-block delay on output */
char *block,flag; /* 64-bit block, last block flag*/
{ static char last[8]; /* previous input block */
register int i;
/* register char *c,*j; */ /* rwo: unused */

if (flag) /* output the last few bytes */
{
fwrite(last, 1, block[0] & 0377, outfile);
return;
}
if (outcount++)

有个开源的项目。几乎有所有的加密算法。是C++开发的。

G++当然可以编译啦。

自己找一下吧。


公司C源码怎样进行加密
源代码加密防泄密软件推荐使用 深圳德人合科技有限公司 的加密软件,是一套从源头上保障数据安全和使用安全的软件系统。采用的是文件透明加密模块,对平常办公使用是没有影响的。而且支持与SVN等源代码管理工具无缝结合。如果企业内部SVN服务器采取透明模式,即加密文件是可以存放在SVN服务器上的,需要达到的...

c语言文本文件加密
源代码文件加密后,不影响软件的正常编译,合法用户正常双击打开,在授权范围内使用。源代码加密软件推荐使用德人合科技的透明加密防泄密软件系统,是一套从源头上保障数据安全和使用安全的软件系统。采用的是文件透明加密模块,对平常办公使用是没有影响的。而且支持与SVN等源代码管理工具无缝结合。如果企业内...

跪求c语言,按位取反文件加密的源代码。最好有注释
然后再执行程序 输入刚才得到的加密文件以及新的目标文件名 进行解密 include <stdio.h>int main(){char name[100];int c;FILE *fp1, *fp2;printf("input source file name:");scanf("%s",name);fp1 = fopen(name, "rb");if(fp1 == NULL) {printf("can not open file %s\\n", name...

求C语言编写的DES加密解密源代码
}\/* 加密文件 *\/int DES_Encrypt(char *plainFile, char *keyStr,char *cipherFile){ FILE *plain,*cipher; int count; ElemType plainBlock[8],cipherBlock[8],keyBlock[8]; ElemType bKey[64]; ElemType subKeys[16][48]; if((plain = fopen(plainFile,"rb")) == NULL){ return PLAIN_FILE_OP...

求32位MD5加密c语言源码
FF (d, a, b, c, x[ 1], 12, 0xe8c7b756); \/**\/\/* 2 *\/ FF (c, d, a, b, x[ 2], 17, 0x242070db); \/**\/\/* 3 *\/ FF (b, c, d, a, x[ 3], 22, 0xc1bdceee); \/**\/\/* 4 *\/ FF (a, b, c, d, x[ 4], 7, 0xf57c0faf); \/**\/\/* 5 ...

c 语言常用的加密算法——MD5
MD5算法作为加密散列函数,产生128位散列值,广泛应用于C语言中。通过OpenSSL库实现MD5加密。SHA-1算法全称为Secure Hash Algorithm 1,用于数字签名、验证、消息摘要等,C语言中通过OpenSSL库实现SHA-1加密。Base64编码虽非加密算法,但用于隐藏信息,C语言中通过OpenSSL库进行Base64编码与解码。实现这些算法...

维吉利亚加密算法 求C或C++源代码 !!急
include <string> using namespace std;void encrypt(char *m, char *k, char *c) \/\/加密算法 { int i = 0,j=0;while(m[i] != '\\0'){ if(m[i] >= 'a' && m[i] <= 'z'){ c[i] = (m[i] - 'a' + k[i%4] - 'a') % 26 + 'a';i++;} else { c[i...

加密文件 c语言代码 要完整代码 能运行的
include<stdio.h>#include<string.h>#include<stdlib.h>void findKey(char* key, char* mark){int len = strlen(key);int i = 0;int j = 0;memset(mark,0,26);for (i = 0; i < len;i++)if (mark[key[i] - 97] == 0){mark[key[i] - 97] = 1;key[j] = key[i];j...

绿色盾牌 里面是一个像C的形状图标是什么加密软件?
看着有点像绿盾,但是加密软件推荐透明加密软件,例如红线隐私保护系统,账号电脑都是需要授权绑定,在账号登录可以随意随加密的文件进行二次编辑修改,省去需要输入密码解密后再查看编辑的麻烦。退出账号后即文件完全被加密,打开既是密文。

哪里有c语言的sha512加密算法的包
libcrypto库里就有API,看OpenSSL文档。

江安县13536637354: 求助一个加密、解密的C++源代码 -
资施人参: ch=getchar(); num; switch(ch)//加密 { case 'a':num=17;break; case 'b':num=33;break;.........}//解密 num=scanf("2d",&num);//取两个字符作为输入,具体格式忘了,你可以查书 switch(num) { case 17:ch='a';break; case 33:ch='b';break;.........}

江安县13536637354: 谁知道哪里有AES算法加密,解密c++/C语言代码? -
资施人参: 我有写好的,肿么给你?贴上来吧.#ifndef aes_h_#define aes_h_#include <iostream>#include <string> using namespace std; typedef unsigned char uint8; class aes { public: /// 构造函数 aes(); /// 析构函数 ~aes(); /// 加密,默认256位...

江安县13536637354: 加解密除了DES AES还有那些算法,哪里能下载到c,c++的加密解密算法源代码?谢谢啊. -
资施人参: RSA,DSA等等,算法很多.源代码建议你使用成熟的开源库,如:OpenSSL http://www.openssl.org/ crypto++ http://www.cryptopp.com/ 不知道你的需求,可能后者更能满足你的需要.

江安县13536637354: 急求用c/c++对字符串加密解密的简单程序(有注释更好) -
资施人参: #include <stdio.h> #include <stdlib.h> #include <string.h> Encrypt(char* cSrc,char* cDest) {char c;int i,h,l,j=0;for (i=0;i<(int)strlen(cSrc);i++){c=cSrc[i];h=(c>>4)&0xf;l=c&0xf;cDest[j]=h+'x';cDest[j+1]=l+'z';j+=2;}cDest[j]='\0';return 0; } ...

江安县13536637354: 跪求AES256位的加解密代码,最好是C++,C#,JAVA写的.
资施人参: 建议使用 Crypto++ 开源库主流加密方式都有 如果需要单独的某种 可到源码去提取Crypto++ Library 5.6.2 - a Free C++ Class Library of ...free C++ library for cryptography: includes ciphers, message authentication codes, one-way hash functions, public-key cryptosystems, key agreement schemes, ...www.cryptopp.com/

江安县13536637354: 跪求VC++编写的文件夹加密软件的源代码 -
资施人参: // AesCodeDlg.cpp : implementation file // #include "stdafx.h" #include "AesCode.h" #include "AesCodeDlg.h" #include "Aes.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ...

江安县13536637354: 跪求VC++编写的文件夹加密软件的源代码
资施人参: //nbsp;AesCodeDlg.cppnbsp;:nbsp;implementationnbsp;file//#includenbsp;“stdafx.h“#includenbsp;“AesCode.h“#includenbsp;“AesCodeDlg.h“#includenbsp;“Aes.h“#ifdefnbsp;_DEBUG#definenbsp;newnbsp;DEBUG_NEW#undefnbsp...

江安县13536637354: 跪求通过VC编程实现文件夹加密(最好有实现原理和代码)~~简单点就好 -
资施人参: 难度非常大,这个你要做的是内核级的挂钩,说专业点就是FSD挂钩或文件过滤驱动,当然SSDT挂钩也能实现,反正呢,就是要编写驱动程序,如果你没写过的话,建议你还是不要做了,写驱动是很困难的一件事.

江安县13536637354: C++代码 加密解密“恺撒密码” 要做界面 -
资施人参: 纯手工写,下面的我写的两个加密与解密的函数,LZ只要放到你的button按钮代码的同一文件里就可以了,LZ可以在点击加密的时候先获取textbox1的值,再调用encoder函数,然后把返回值写到textbox2里,解密同理.我这里MFC跑不起来#...

江安县13536637354: 求c++进程隐藏源码
资施人参: 可以参考window核心编程的随书源代码,里面有! 一般在NT核心在的思路就是,进程注入! 首先要找到某进程的handle,比如explorer,可以用GetModuleHandle等等 然后要用VirtualAlloc申请一块远程进程的内存空间,然后将你的代码copy到申请的空间,然后要用createremotethread创建新线程,这样就算隐藏了!但是还是有很多问题的,比如地址转换的问题!对内核不了解还是不要做的好!

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网