关闭

一个简单的c++加解密程序

标签: 加密
450人阅读 评论(1) 收藏 举报
分类:

第一步: 生成一个加密数组 其实就是一个打乱的ascii码表

unsigned char Encry[] = {
0x3e, 0x13, 0x25, 0x18, 0x6e, 0x15, 0x7b, 0x46,
0x61, 0x10, 0x4f, 0x6f, 0x43, 0x5e, 0x0f, 0x72,
0x39, 0x0d, 0x29, 0x4e, 0x74, 0x6b, 0x40, 0x5b,
0x44, 0x4c, 0x00, 0x68, 0x2e, 0x1c, 0x75, 0x26,
0x14, 0x73, 0x07, 0x34, 0x12, 0x7e, 0x30, 0x23,
0x69, 0x42, 0x59, 0x20, 0x0c, 0x27, 0x58, 0x38,
0x71, 0x19, 0x0a, 0x5f, 0x28, 0x45, 0x21, 0x78,
0x7a, 0x22, 0x3c, 0x24, 0x5c, 0x2b, 0x04, 0x60,
0x77, 0x54, 0x2f, 0x1d, 0x5a, 0x16, 0x64, 0x2c,
0x3f, 0x08, 0x65, 0x2a, 0x09, 0x48, 0x5d, 0x47,
0x7d, 0x7c, 0x1b, 0x1a, 0x55, 0x63, 0x31, 0x49,
0x3a, 0x32, 0x70, 0x4b, 0x62, 0x0b, 0x67, 0x76,
0x01, 0x17, 0x11, 0x3b, 0x79, 0x33, 0x35, 0x4a,
0x6c, 0x4d, 0x41, 0x57, 0x6a, 0x37, 0x03, 0x52,
0x06, 0x6d, 0x1e, 0x05, 0x7f, 0x2d, 0x66, 0x56,
0x53, 0x0e, 0x1f, 0x02, 0x3d, 0x36, 0x51, 0x50
};


生成加密数组方法很多 常见的有一下几种

1

void swap(int& a, int& b) 

    a = a^b; 
    b = a^b; 
    a = a^b; 



size_t shuffle2(int s[], int n) 

    size_t t=0;//计算循环次数 
    for (int i=0; i<n; i++) 
    { 
        t++; 
        s[i] = i; 
    } 
    for (i=0; i<n; i++) 
    { 
        t++; 
        int num = rand()%n; 
        if (num != i) 
        { 
            swap(s[num],s[i]); 
        } 
    } 
    return t; 


2

size_t shuffle22(int s[], int n) 

    size_t t=0;//计算循环次数 
    for (int i=0; i<n; i++) 
    { 
        t++; 
        s[i] = i; 
    } 
    for (i=n-1; i>0; --i) 
    { 
        t++; 
        int num = rand()%(i+1); 
        if (num != i) 
        { 
            swap(s[num],s[i]); 
        } 
    } 
    return t; 
}

3



srand (unsigned(time(NULL)));
vector<int> myvector;
vector<int>::iterator it;


for (int i=0; i<128; ++i)
{
myvector.push_back(i);
}


random_shuffle(myvector.begin(),myvector.end(),p_myrandom);


for (it = myvector.begin(),i=0; it != myvector.end(); ++it,++i)
{
if (i%8 == 0 && i != 0)
{
cout<<endl;
}
cout<<"0x";
cout.width(2);
cout.fill('0');
cout<<hex<<*it<<", ";
}
cout<<endl;


然后根据加密数组生成相对应的解密数组

for (int i = 0; i < 128; ++i)
{
for (int j = 0; j < 128; j++)
{
if (i == Encry[j])
{
if (i%8 == 0 && i != 0)
{
cout<<endl;
}
cout<<"0x";
cout.width(2);
cout.fill('0');
cout<<hex<<(int)j<<", ";
}
}

}
cout<<endl;


unsigned char Decry[] = {
0x1a, 0x60, 0x7b, 0x6e, 0x3e, 0x73, 0x70, 0x22,
0x49, 0x4c, 0x32, 0x5d, 0x2c, 0x11, 0x79, 0x0e,
0x09, 0x62, 0x24, 0x01, 0x20, 0x05, 0x45, 0x61,
0x03, 0x31, 0x53, 0x52, 0x1d, 0x43, 0x72, 0x7a,
0x2b, 0x36, 0x39, 0x27, 0x3b, 0x02, 0x1f, 0x2d,
0x34, 0x12, 0x4b, 0x3d, 0x47, 0x75, 0x1c, 0x42,
0x26, 0x56, 0x59, 0x65, 0x23, 0x66, 0x7d, 0x6d,
0x2f, 0x10, 0x58, 0x63, 0x3a, 0x7c, 0x00, 0x48,
0x16, 0x6a, 0x29, 0x0c, 0x18, 0x35, 0x07, 0x4f,
0x4d, 0x57, 0x67, 0x5b, 0x19, 0x69, 0x13, 0x0a,
0x7f, 0x7e, 0x6f, 0x78, 0x41, 0x54, 0x77, 0x6b,
0x2e, 0x2a, 0x44, 0x17, 0x3c, 0x4e, 0x0d, 0x33,
0x3f, 0x08, 0x5c, 0x55, 0x46, 0x4a, 0x76, 0x5e,
0x1b, 0x28, 0x6c, 0x15, 0x68, 0x71, 0x04, 0x0b,
0x5a, 0x30, 0x0f, 0x21, 0x14, 0x1e, 0x5f, 0x40,
0x37, 0x64, 0x38, 0x06, 0x51, 0x50, 0x25, 0x74
};

#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
#include <ctime>

using namespace std;

// random generator function:  
ptrdiff_t myrandom (ptrdiff_t i) 
{ 
	return rand()%i;
}   

// pointer object to it:  
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;

void swap(int& a, int& b) 
{ 
    a = a^b; 
    b = a^b; 
    a = a^b; 
} 

size_t shuffle2(int s[], int n) 
{ 
    size_t t=0;//计算循环次数 
    for (int i=0; i<n; i++) 
    { 
        t++; 
        s[i] = i; 
    } 
    for (i=0; i<n; i++) 
    { 
        t++; 
        int num = rand()%n; 
        if (num != i) 
        { 
            swap(s[num],s[i]); 
        } 
    } 
    return t; 
} 

void printCards2(int s[], int n) 
{ 
    for (int i=0; i<n; i++) 
    { 
        cout << s[i] << " "; 
    } 
    cout << endl; 
}

size_t shuffle22(int s[], int n) 
{ 
    size_t t=0;//计算循环次数 
    for (int i=0; i<n; i++) 
    { 
        t++; 
        s[i] = i; 
    } 
    for (i=n-1; i>0; --i) 
    { 
        t++; 
        int num = rand()%(i+1); 
        if (num != i) 
        { 
            swap(s[num],s[i]); 
        } 
    } 
    return t; 
}

unsigned char Encry[] = {
	0x3e, 0x13, 0x25, 0x18, 0x6e, 0x15, 0x7b, 0x46,
	0x61, 0x10, 0x4f, 0x6f, 0x43, 0x5e, 0x0f, 0x72,
	0x39, 0x0d, 0x29, 0x4e, 0x74, 0x6b, 0x40, 0x5b,
	0x44, 0x4c, 0x00, 0x68, 0x2e, 0x1c, 0x75, 0x26,
	0x14, 0x73, 0x07, 0x34, 0x12, 0x7e, 0x30, 0x23,
	0x69, 0x42, 0x59, 0x20, 0x0c, 0x27, 0x58, 0x38,
	0x71, 0x19, 0x0a, 0x5f, 0x28, 0x45, 0x21, 0x78,
	0x7a, 0x22, 0x3c, 0x24, 0x5c, 0x2b, 0x04, 0x60,
	0x77, 0x54, 0x2f, 0x1d, 0x5a, 0x16, 0x64, 0x2c,
	0x3f, 0x08, 0x65, 0x2a, 0x09, 0x48, 0x5d, 0x47,
	0x7d, 0x7c, 0x1b, 0x1a, 0x55, 0x63, 0x31, 0x49,
	0x3a, 0x32, 0x70, 0x4b, 0x62, 0x0b, 0x67, 0x76,
	0x01, 0x17, 0x11, 0x3b, 0x79, 0x33, 0x35, 0x4a,
	0x6c, 0x4d, 0x41, 0x57, 0x6a, 0x37, 0x03, 0x52,
	0x06, 0x6d, 0x1e, 0x05, 0x7f, 0x2d, 0x66, 0x56,
	0x53, 0x0e, 0x1f, 0x02, 0x3d, 0x36, 0x51, 0x50
};

unsigned char Decry[] = {
	0x1a, 0x60, 0x7b, 0x6e, 0x3e, 0x73, 0x70, 0x22,
	0x49, 0x4c, 0x32, 0x5d, 0x2c, 0x11, 0x79, 0x0e,
	0x09, 0x62, 0x24, 0x01, 0x20, 0x05, 0x45, 0x61,
	0x03, 0x31, 0x53, 0x52, 0x1d, 0x43, 0x72, 0x7a,
	0x2b, 0x36, 0x39, 0x27, 0x3b, 0x02, 0x1f, 0x2d,
	0x34, 0x12, 0x4b, 0x3d, 0x47, 0x75, 0x1c, 0x42,
	0x26, 0x56, 0x59, 0x65, 0x23, 0x66, 0x7d, 0x6d,
	0x2f, 0x10, 0x58, 0x63, 0x3a, 0x7c, 0x00, 0x48,
	0x16, 0x6a, 0x29, 0x0c, 0x18, 0x35, 0x07, 0x4f,
	0x4d, 0x57, 0x67, 0x5b, 0x19, 0x69, 0x13, 0x0a,
	0x7f, 0x7e, 0x6f, 0x78, 0x41, 0x54, 0x77, 0x6b,
	0x2e, 0x2a, 0x44, 0x17, 0x3c, 0x4e, 0x0d, 0x33,
	0x3f, 0x08, 0x5c, 0x55, 0x46, 0x4a, 0x76, 0x5e,
	0x1b, 0x28, 0x6c, 0x15, 0x68, 0x71, 0x04, 0x0b,
	0x5a, 0x30, 0x0f, 0x21, 0x14, 0x1e, 0x5f, 0x40,
	0x37, 0x64, 0x38, 0x06, 0x51, 0x50, 0x25, 0x74
};

int Hex2Int(char c)
{
	if (c >= '0' && c <= '9') return c - '0';
    if (c >= 'A' && c <= 'F') return c - 'A' + 10;
    if (c >= 'a' && c <= 'f') return c - 'a' + 10;
    return -1;
}

int char2int(char a[2])
{
	int m,n; 
    m = Hex2Int(a[0]);
    n = Hex2Int(a[1]);
    return m*16+n;
}

void charstr2byte(char* desstr,char* srcstr)
{
	int len = strlen(srcstr);
	memset(desstr,0,(len/2)+1);
    for (int i = 0; i < len; i+=2)
    {
        desstr[i/2] = char2int(srcstr+i);
    }
}


void main()
{
 
  	char str2[36] = "1f6c17034a051703";
  	char Temp[18] = "0";
 
  	charstr2byte(Temp,str2);
  
  	int len1 = strlen(Temp);
   	for (int i = 0; i < len1; ++i)
   	{
   		cout<<Decry[Temp[i]];
   	}
  	cout<<endl;

//    	char str1[18] = "zhangsan";
//    	cout<<str1<<endl;
//    	int len = strlen(str1);
//    	for (int i = 0; i < len; ++i)
//    	{
//  		cout.width(2);
//  		cout.fill('0');
//    		cout<<hex<<(int)Encry[str1[i]];
//    	}
//    	cout<<endl;
//   
//  	for (i = 0; i < len; ++i)
//  	{
//  		cout<<Decry[Encry[str1[i]]];
//  	}
//  	cout<<endl;


	for (int i = 0; i < 128; ++i)
	{
		for (int j = 0; j < 128; j++)
		{
			if (i == Encry[j])
			{
				if (i%8 == 0 && i != 0)
				{
					cout<<endl;
				}
				cout<<"0x";
				cout.width(2);
		 		cout.fill('0');
				cout<<hex<<(int)j<<", ";
			}
		}
		
	}
	cout<<endl;


	srand (unsigned(time(NULL)));
	vector<int> myvector;
	vector<int>::iterator it;

	for (int i=0; i<128; ++i)
	{
		myvector.push_back(i);
	}

	random_shuffle(myvector.begin(),myvector.end(),p_myrandom);

	for (it = myvector.begin(),i=0; it != myvector.end(); ++it,++i)
	{
		if (i%8 == 0 && i != 0)
		{
			cout<<endl;
		}
		cout<<"0x";
		cout.width(2);
		cout.fill('0');
		cout<<hex<<*it<<", ";
	}
	cout<<endl;


}


任意一个ascii字符串根据加密数组生成一个解密码,解密的时候将界密码在解密数组中找到相应的原字符串


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:95509次
    • 积分:1240
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:74篇
    • 译文:0篇
    • 评论:6条
    最新评论