求高手把一段javascript代码转成 C++代码

我不懂javascript.现在我需要用C++实现一个类似的加密解密功能类
一个函数加密,一个函数解密.
或者告诉我哪里有可以实现这种文字简单加密解密的功能.
如题
javascript代码:
var sdf3="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
function textEncrypt(str)
{
var t="";
var a,a1,a2,a3;
var b=sdf3.split("");
for(var x=0;x<str.length;x++)
{
a=str.charCodeAt(x);
a1=a%41;
a=(a-a1)/41;
a2=a%41;
a=(a-a2)/41;
a3=a%41;
t+=b[a3]+b[a2]+b[a1];
}
a="z";
for(var x=0;x<t.length;x+=3){
if(t.charAt(x)!="A") return t;
a+=t.substr(x+1,2);
}
return a;
}

function textDecryption(str)
{
var a1,a2,a3,b=sdf3,d=0,t,a;
if(str.charAt(0)=="z") {
t=new Array(Math.floor((str.length-1)/2));
a=t.length;
for(var x=0;x<a;x++) {
d++;
a2=b.indexOf(str.charAt(d));
d++;
a3=b.indexOf(str.charAt(d));
t[x] = a2 * 41 + a3;
}
}
else {
t = new Array(Math.floor(str.length / 3));
a = t.length;
for (var x = 0; x < a; x++) {
a1 = b.indexOf(str.charAt(d));
d++;
a2 = b.indexOf(str.charAt(d));
d++;
a3 = b.indexOf(str.charAt(d));
d++;
t[x] = a1 * 1681 + a2 * 41 + a3;
}
}
a = eval("String.fromCharCode(" + t.join(',') + ")");
return a;
}

先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;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-03-23
如果是简单加密的话,直接位运算一下就可以了
char encode(char content, char key){
return (char)(content ^ key);
}

char decode(char cryptograph, char key){
return (char)(cryptograph ^ key);
}追问

按位反破解太简单了.怎么也要稍微复杂点

第2个回答  2011-03-24
按这个js脚本的算法,写了一个类,提供静态方法完成加密解密。假定了待加密内容是unicode编码。加密后为sbcs。主函数是测试的,后面的代码是为了输出unicode字符串进行的转换,和加/解密无关。
#include<tchar.h>
#include<string>
#include<iostream.h>
#include<afx.h>
class Translator{
private:
static const char code[];
public:
static char *textEncrypt(TCHAR *source,char result[]);
static TCHAR *textDecrypt(char *source,TCHAR result[]);
};
const char Translator::code[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcde";
char *Translator::textEncrypt(TCHAR *source,char result[]){
int l=wcslen(source);
char *temp=new char[l*3+1];
for(int i=0;i<l;i++){
unsigned short ch=source[i];
for(int j=0;j<3;j++){
temp[i*3+2-j]=code[ch%41];
ch/=41;
}
}
temp[l*3]=0;
for(i=l*3-3;i>=0;i-=3){
if(temp[i]!='A'){
break;
}
}
if(i>=0){
strcpy(result,temp);
}
else{
result[0]='z';
for(i=0;i<l;i++){
result[i*2+1]=temp[i*3+1];
result[i*2+2]=temp[i*3+2];
}
result[i]=0;
}
delete [] temp;
return result;
}
TCHAR *Translator::textDecrypt(char *source,TCHAR result[]){
char *temp,ch;
int l,i;
if(source[0]=='z'){
l=(strlen(source)-1)/2;
temp=new char[l*3];
for(i=0;i<l;i++){
ch=source[i*2+1];
temp[i*3+1]=ch>='A'?(ch>='a'?ch-'a'+36:ch-'A'+10):ch-'0';
ch=source[i*2+2];
temp[i*3+2]=ch>='A'?(ch>='a'?ch-'a'+36:ch-'A'+10):ch-'0';
temp[i*3]=10;
}
}
else{
l=strlen(source)/3;
temp=new char[l*3];
for(i=0;i<3*l;i++){
ch=source[i];
temp[i]=ch>='A'?(ch>='a'?ch-'a'+36:ch-'A'+10):ch-'0';
}
}
for(i=0;i<l;i++){
result[i]=(temp[i*3]*41+temp[i*3+1])*41+temp[i*3+2];
}
result[i]=0;
delete [] temp;
return result;
}
void main(){
char encrypted[100];
TCHAR decrypted[20];
Translator::textEncrypt(L"我试验unicode效果",encrypted);
cout<<encrypted<<endl;
Translator::textDecrypt(encrypted,decrypted);
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,decrypted,-1,NULL,0,NULL,FALSE);
char *psText;
psText = new char[dwNum];
if(!psText){
delete []psText;
}
WideCharToMultiByte(CP_OEMCP,NULL,decrypted,-1,psText,dwNum,NULL,FALSE);
cout<<psText<<endl;
delete []psText;
}