先MARK下,晚上贴出来给你
下面是代码,自己测试下,在我这是没有问题的
------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//下面的指针指向待释放的内存地址
extern char* g_point;
extern char* g_point1;
extern char* g_point2;
//跟JavaScript中的同名函数功能一样
//返回字符所在的位置
int indexOf(char* str, char c)
{
if(str == NULL)
return -1;
int i = 0;
while(str[i] != '\0')
{
if(str[i] == c)
return i;
i++;
}
//letter not found
return -1;
}
//跟JavaScript中同名函数功能一样
char charAt(char* str, int n)
{
if(str == NULL)
return 0;
if(strlen(str) < n)//n is too large
return 0;
return str[n];
}
char sdf3[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
char* textEncrypt(char* str)
{
char *t = (char*)malloc(100* sizeof(char));
char *a = (char*)malloc(100* sizeof(char));
memset(t, 0, 100);
memset(a, 0, 100);
char a1,a2,a3;
char *b=sdf3;
int x = 0, n = 0;
for(x=0;x<strlen(str);x++,n+=3)
{
a[0]=str[x];
a1=a[0]%41;
a[0]=(a[0]-a1)/41;
a2=a[0]%41;
a[0]=(a[0]-a2)/41;
a3=a[0]%41;
//t+=b[a3]+b[a2]+b[a1];
t[n] = b[a3];
t[n+1] = b[a2];
t[n+2] = b[a1];
}
a[0]='z';
n = 1;
for(x=0;x<strlen(str);x+=3){
if(t[x]!='A') return t;
//a+=t.substr(x+1,2);
a[n++] = t[x+1];
a[n] = t[x+2];
}
g_point = t;
g_point1 = a;
return a;
}
char* textDecryption(char* str)
{
//var a1,a2,a3,b=sdf3,d=0,t,a;
char* b = sdf3;
//printf("str: %s\n", str);
unsigned char* t = NULL;
int a = 0, d = 0,a1, a2, a3;
if(str[0]=='z') {
t=(unsigned char*)malloc(floor((strlen(str)-1)/2)* sizeof(unsigned char) + 1);
memset(t, 0, floor((strlen(str)-1)/2)+1 );
a= floor((strlen(str)-1)/2);
int x = 0;
for(x=0;x<a;x++) {
d++;
// a2=b.indexOf(str.charAt(d));
a2 = indexOf(b, charAt(str, d));
d++;
a3 = indexOf(b, charAt(str, d));
t[x] = a2 * 41 + a3;
}
g_point2 = t;
}
else {
t = (unsigned char*)malloc(floor(strlen(str) / 3)*sizeof(unsigned char) + 1);
memset(t, 0,floor(strlen(str) / 3) +1);
a = floor(strlen(str) / 3);
int x = 0;
for (x = 0; x < a; x++) {
a1 = indexOf(b, charAt(str, d));
d++;
a2 = indexOf(b, charAt(str, d));
d++;
a3 = indexOf(b, charAt(str, d));
d++;
t[x] = a1 * 1681 + a2 * 41 + a3;
}
g_point2 = t;
}
return t;
}
char* g_point = NULL;
char* g_point1 = NULL;
char* g_point2 = NULL;
int main()
{
printf("input the string: \n");
char str[100];
memset(str, 0, 100);
scanf("%s", str);
char* p = textEncrypt(str);
printf("加密后: %s\n",p);
printf("解密后: %s\n", textDecryption(p));
free(g_point);
free(g_point1);
free(g_point2);
return 0;
}
温馨提示:答案为网友推荐,仅供参考