-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.c
More file actions
64 lines (64 loc) · 1.1 KB
/
encrypt.c
File metadata and controls
64 lines (64 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "encrypt.h"
#define LOW_CHAR 32
#define HI_CHAR 127
static char* shuffle(char* s,char* d,int num)
{
srand(num);
strcpy(d,s);
int len = strlen(d);
char* org = d;
while(*d){
int i = rand() % len;
int temp = *d;
*d = *(org + i);
*(org +i) = temp;
d++;
}
return org;
}
static int find(const char* s,char* d,int a)
{
while(*s != a){
s++,d++;
}
return *d;
}
static char* imp(const char* s,char* d,int num,int flag)
{
char org[255]= {0};
char dic[255]= {0} ;
int i ;
char* rs = d;
for(i = 0 ; i < HI_CHAR - LOW_CHAR; ++i){
org[i] = i + LOW_CHAR;
}
org[i] = 0;
shuffle(org,dic,num);
while(*s){
if(flag)
*d = find(org,dic,*s);
else
*d = find(dic,org,*s);
s++,d++;
}
*d = 0;
return rs;
}
char* encrypt(const char* s,char* d)
{
srand((unsigned)time(NULL));
int a = LOW_CHAR + rand() % (HI_CHAR -LOW_CHAR);
int b = LOW_CHAR + rand() % (HI_CHAR - LOW_CHAR);
d[0] = a; d[1] = b;
imp(s,d+2,a* 256 + b,1);
return d;
}
char* decrypt(const char* s,char* d)
{
imp(s+2,d,s[0]*256 + s[1],0);
return d;
}