- 论坛徽章:
- 2
|
- int xor_crypt_0(char *text, char key, int len)
- {
- for (; len-- > 0; text++) {
- *text ^= key;
- }
- return 0;
- }
- int xor_crypt_1(char *text, char *key, int len)
- {
- for (; *key != '\0'; key++) {
- xor_crypt_0(text, *key, len);
- }
- return 0;
- }
- int xor_crypt_2(char *text, char *key)
- {
- char dat;
- dat = 0;
- for (; *text != '\0'; text++) {
- dat ^= *text;
- }
- *key = dat;
- return 0;
- }
- #include <ctype.h>
- #include <stdio.h>
- int show_content(const char *buf, int len)
- {
- int i, j, l;
- i = 0;
- while (i < len) {
- l = len - i;
- if (l > 16) {
- l = 16;
- }
- printf("%04X ", i);
- for (j = 0; j < l; j++) {
- printf("%02x ", (unsigned char)buf[j]);
- }
- for (; j < 16; j++) {
- printf(" ");
- }
- printf(" ");
- for (j = 0; j < l; j++) {
- if (buf[j] >= ' ' && buf[j] < 127) {
- printf("%c", buf[j]);
- }else {
- printf(".");
- }
- }
- printf("\n");
- i += l;
- buf += l;
- }
- return 0;
- }
- #define LEN(a) (sizeof(a)/sizeof((a)[0]))
- #include <string.h>
- int main(void)
- {
- char text1[] = "This is my 串";
- char text2[] = "This is my 串";
- char text3[] = " ";
- char key1[] = "mnkey";
- char key2;
- xor_crypt_2(key1, &key2);
- xor_crypt_1(text1, key1, LEN(text1) - 1);
- xor_crypt_0(text2, key2, LEN(text2) - 1);
- show_content(text1, LEN(text1) - 1);
- show_content(text2, LEN(text2) - 1);
- key2 = 0;
- do {
- memcpy(text3, text1, LEN(text1));
- xor_crypt_0(text3, key2, LEN(text3) - 1);
- show_content(text3, LEN(text3) - 1);
- ++key2;
- }while (key2 != 0);
- return 0;
- }
复制代码 |
|