c語言雙軌加密演算法-九游会j9娱乐平台
a. 用c語言設計了一個加密演算法:用a代替z,用b代替y,用c代替x,……,用z代替a。
#include
int main()
{
char s[100],*p;
printf("請輸入字元串 : ");
gets(s);
p = s;
while(*p)
{
if((*p >= 'a') && (*p <= 'z')) /*處理小寫*/
{
*p ='z' - *p 'a';
}
if((*p >= 'a') && (*p <= 'z')) /*處理大寫,同理處理數字亦一樣..自己例推*/
{
*p ='z' - *p 'a';
}
p ;
}
printf("轉換後的字元串為 : %s\n\n",s);
return 0;
}
b. c 語言常用的加密演算法——md5
在c語言中,常用的加密演算法主要包括aes、rsa、md5、sha-1及base64編碼。
aes加密演算法是一種對稱加密演算法,廣泛應用於數據加密。通過openssl庫在c語言中實現aes加密。
rsa加密演算法為非對稱加密演算法,使用兩把不同的密鑰進行加密與解密,是最成熟且常用的非對稱加密演算法。同樣使用openssl庫在c語言中實現rsa加密。
md5演算法作為加密散列函數,產生128位散列值,廣泛應用於c語言中。通過openssl庫實現md5加密。
sha-1演算法全稱為secure hash algorithm 1,用於數字簽名、驗證、消息摘要等,c語言中通過openssl庫實現sha-1加密。
base64編碼雖非加密演算法,但用於隱藏信息,c語言中通過openssl庫進行base64編碼與解碼。
實現這些演算法時通常利用openssl庫,因其提供了高效實現,避免了重復開發。
md5演算法在c語言中的實現示例如下:
#include
#include md5.h>
int main() {
char *str = "hello, world!";
unsigned char digest[md5_digest_length];
md5((unsigned char*) str, strlen(str), digest);
printf("md5 hash of \"%s\": ", str);
for (int i = 0; i < md5_digest_length; i ) {
printf("x", digest[i]);
}
printf(" ");
return 0;
}
通過openssl庫中的md5函數,傳入字元串與長度,將結果存儲在數組中,然後列印數組內容即為md5散列值。
c. 利用c語言實現移位加密和解密演算法
呵呵
這兩天剛編了一個
#include
#include
#include
void main()
{
char a[500];
gets(a);
int n,i;
n = strlen(a);
for(i=0;i
a[i]='a';
else if(a[i]=='z')
a[i]='b';
else if (isalpha(a[i]))
a[i] = 2;
printf("%s",a );
}
這個是往後推2的解密。
g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle.
解密試試吧~~
加密的話
改動三個地方就行 自己試試吧 哈哈
d. 關於c語言的加密
'a'的asc碼是97
'b'的asc碼是98
'c'的asc碼是99
... ...
'z'的asc碼是122
明文和密文相對應
如明文:"abc"
密文:"zyw"
當ch1 = 'a'時,ch2 = 'z'
ch1 - 97 = 97 25 - ch2
97 - 97 = 97 25 - 122 = 0
當ch1 = 'b'時,ch2 = 'y'
ch1 - 97 = 97 25 - ch2
98 - 97 = 97 25 - 121 = 1
當ch1 = 'c'時,ch2 = 'w'
ch1 - 97 = 97 25 - ch2
99 - 97 = 97 25 - 120 = 2
ch1 - 97 = 97 25 - ch2這實際上是明文與密文的對應關系,也就是他的演算法
你可以對明文去設定一個對應關系,就可以得到不同的密文
e. aes加密演算法c代碼
完整的!
#include "stdio.h"
#include "memory.h"
#include "time.h"
#include "stdlib.h"
#define plain_file_open_error -1
#define key_file_open_error -2
#define cipher_file_open_error -3
#define ok 1
typedef char elemtype;
/*初始置換表ip*/
int ip_table[64] = { 57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7,
56,48,40,32,24,16,8,0,
58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6};
/*逆初始置換表ip^-1*/
int ip_1_table[64] = {39,7,47,15,55,23,63,31,
38,6,46,14,54,22,62,30,
37,5,45,13,53,21,61,29,
36,4,44,12,52,20,60,28,
35,3,43,11,51,19,59,27,
34,2,42,10,50,18,58,26,
33,1,41,9,49,17,57,25,
32,0,40,8,48,16,56,24};
/*擴充置換表e*/
int e_table[48] = {31, 0, 1, 2, 3, 4,
3, 4, 5, 6, 7, 8,
7, 8,9,10,11,12,
11,12,13,14,15,16,
15,16,17,18,19,20,
19,20,21,22,23,24,
23,24,25,26,27,28,
27,28,29,30,31, 0};
/*置換函數p*/
int p_table[32] = {15,6,19,20,28,11,27,16,
0,14,22,25,4,17,30,9,
1,7,23,13,31,26,2,8,
18,12,29,5,21,10,3,24};
/*s盒*/
int s[8][4][16] =
/*s1*/
{{{14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7},
{0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8},
{4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0},
{15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13}},
/*s2*/
{{15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10},
{3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5},
{0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15},
{13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9}},
/*s3*/
{{10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8},
{13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1},
{13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7},
{1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12}},
/*s4*/
{{7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15},
{13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9},
{10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4},
{3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14}},
/*s5*/
{{2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9},
{14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6},
{4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14},
{11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3}},
/*s6*/
{{12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11},
{10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8},
{9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6},
{4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13}},
/*s7*/
{{4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1},
{13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6},
{1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2},
{6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12}},
/*s8*/
{{13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7},
{1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2},
{7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8},
{2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11}}};
/*置換選擇1*/
int pc_1[56] = {56,48,40,32,24,16,8,
0,57,49,41,33,25,17,
9,1,58,50,42,34,26,
18,10,2,59,51,43,35,
62,54,46,38,30,22,14,
6,61,53,45,37,29,21,
13,5,60,52,44,36,28,
20,12,4,27,19,11,3};
/*置換選擇2*/
int pc_2[48] = {13,16,10,23,0,4,2,27,
14,5,20,9,22,18,11,3,
25,7,15,6,26,19,12,1,
40,51,30,36,46,54,29,39,
50,44,32,46,43,48,38,55,
33,52,45,41,49,35,28,31};
/*對左移次數的規定*/
int move_times[16] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
int bytetobit(elemtype ch,elemtype bit[8]);
int bittobyte(elemtype bit[8],elemtype *ch);
int char8tobit64(elemtype ch[8],elemtype bit[64]);
int bit64tochar8(elemtype bit[64],elemtype ch[8]);
int des_makesubkeys(elemtype key[64],elemtype subkeys[16][48]);
int des_pc1_transform(elemtype key[64], elemtype tempbts[56]);
int des_pc2_transform(elemtype key[56], elemtype tempbts[48]);
int des_rol(elemtype data[56], int time);
int des_ip_transform(elemtype data[64]);
int des_ip_1_transform(elemtype data[64]);
int des_e_transform(elemtype data[48]);
int des_p_transform(elemtype data[32]);
int des_sbox(elemtype data[48]);
int des_xor(elemtype r[48], elemtype l[48],int count);
int des_swap(elemtype left[32],elemtype right[32]);
int des_encryptblock(elemtype plainblock[8], elemtype subkeys[16][48], elemtype cipherblock[8]);
int des_decryptblock(elemtype cipherblock[8], elemtype subkeys[16][48], elemtype plainblock[8]);
int des_encrypt(char *plainfile, char *keystr,char *cipherfile);
int des_decrypt(char *cipherfile, char *keystr,char *plainfile);
/*位元組轉換成二進制*/
int bytetobit(elemtype ch, elemtype bit[8]){
int cnt;
for(cnt = 0;cnt < 8; cnt ){
*(bit cnt) = (ch>>cnt)&1;
}
return 0;
}
/*二進制轉換成位元組*/
int bittobyte(elemtype bit[8],elemtype *ch){
int cnt;
for(cnt = 0;cnt < 8; cnt ){
*ch |= *(bit cnt)<
return 0;
}
/*將長度為8的字元串轉為二進制位串*/
int char8tobit64(elemtype ch[8],elemtype bit[64]){
int cnt;
for(cnt = 0; cnt < 8; cnt ){
bytetobit(*(ch cnt),bit (cnt<<3));
}
return 0;
}
/*將二進制位串轉為長度為8的字元串*/
int bit64tochar8(elemtype bit[64],elemtype ch[8]){
int cnt;
memset(ch,0,8);
for(cnt = 0; cnt < 8; cnt ){
bittobyte(bit (cnt<<3),ch cnt);
}
return 0;
}
/*生成子密鑰*/
int des_makesubkeys(elemtype key[64],elemtype subkeys[16][48]){
elemtype temp[56];
int cnt;
des_pc1_transform(key,temp);/*pc1置換*/
for(cnt = 0; cnt < 16; cnt ){/*16輪跌代,產生16個子密鑰*/
des_rol(temp,move_times[cnt]);/*循環左移*/
des_pc2_transform(temp,subkeys[cnt]);/*pc2置換,產生子密鑰*/
}
return 0;
}
/*密鑰置換1*/
int des_pc1_transform(elemtype key[64], elemtype tempbts[56]){
int cnt;
for(cnt = 0; cnt < 56; cnt ){
tempbts[cnt] = key[pc_1[cnt]];
}
return 0;
}
/*密鑰置換2*/
int des_pc2_transform(elemtype key[56], elemtype tempbts[48]){
int cnt;
for(cnt = 0; cnt < 48; cnt ){
tempbts[cnt] = key[pc_2[cnt]];
}
return 0;
}
/*循環左移*/
int des_rol(elemtype data[56], int time){
elemtype temp[56];
/*保存將要循環移動到右邊的位*/
memcpy(temp,data,time);
memcpy(temp time,data 28,time);
/*前28位移動*/
memcpy(data,data time,28-time);
memcpy(data 28-time,temp,time);
/*後28位移動*/
memcpy(data 28,data 28 time,28-time);
memcpy(data 56-time,temp time,time);
return 0;
}
/*ip置換*/
int des_ip_transform(elemtype data[64]){
int cnt;
elemtype temp[64];
for(cnt = 0; cnt < 64; cnt ){
temp[cnt] = data[ip_table[cnt]];
}
memcpy(data,temp,64);
return 0;
}
/*ip逆置換*/
int des_ip_1_transform(elemtype data[64]){
int cnt;
elemtype temp[64];
for(cnt = 0; cnt < 64; cnt ){
temp[cnt] = data[ip_1_table[cnt]];
}
memcpy(data,temp,64);
return 0;
}
/*擴展置換*/
int des_e_transform(elemtype data[48]){
int cnt;
elemtype temp[48];
for(cnt = 0; cnt < 48; cnt ){
temp[cnt] = data[e_table[cnt]];
}
memcpy(data,temp,48);
return 0;
}
/*p置換*/
int des_p_transform(elemtype data[32]){
int cnt;
elemtype temp[32];
for(cnt = 0; cnt < 32; cnt ){
temp[cnt] = data[p_table[cnt]];
}
memcpy(data,temp,32);
return 0;
}
/*異或*/
int des_xor(elemtype r[48], elemtype l[48] ,int count){
int cnt;
for(cnt = 0; cnt < count; cnt ){
r[cnt] ^= l[cnt];
}
return 0;
}
/*s盒置換*/
int des_sbox(elemtype data[48]){
int cnt;
int line,row,output;
int cur1,cur2;
for(cnt = 0; cnt < 8; cnt ){
cur1 = cnt*6;
cur2 = cnt<<2;
/*計算在s盒中的行與列*/
line = (data[cur1]<<1) data[cur1 5];
row = (data[cur1 1]<<3) (data[cur1 2]<<2)
(data[cur1 3]<<1) data[cur1 4];
output = s[cnt][line][row];
/*化為2進制*/
data[cur2] = (output&0x08)>>3;
data[cur2 1] = (output&0x04)>>2;
data[cur2 2] = (output&0x02)>>1;
data[cur2 3] = output&0x01;
}
return 0;
}
/*交換*/
int des_swap(elemtype left[32], elemtype right[32]){
elemtype temp[32];
memcpy(temp,left,32);
memcpy(left,right,32);
memcpy(right,temp,32);
return 0;
}
/*加密單個分組*/
int des_encryptblock(elemtype plainblock[8], elemtype subkeys[16][48], elemtype cipherblock[8]){
elemtype plainbits[64];
elemtype right[48];
int cnt;
char8tobit64(plainblock,plainbits);
/*初始置換(ip置換)*/
des_ip_transform(plainbits);
/*16輪迭代*/
for(cnt = 0; cnt < 16; cnt ){
memcpy(right,plainbits 32,32);
/*將右半部分進行擴展置換,從32位擴展到48位*/
des_e_transform(right);
/*將右半部分與子密鑰進行異或操作*/
des_xor(right,subkeys[cnt],48);
/*異或結果進入s盒,輸出32位結果*/
des_sbox(right);
/*p置換*/
des_p_transform(right);
/*將明文左半部分與右半部分進行異或*/
des_xor(plainbits,right,32);
if(cnt != 15){
/*最終完成左右部的交換*/
des_swap(plainbits,plainbits 32);
}
}
/*逆初始置換(ip^1置換)*/
des_ip_1_transform(plainbits);
bit64tochar8(plainbits,cipherblock);
return 0;
}
/*解密單個分組*/
int des_decryptblock(elemtype cipherblock[8], elemtype subkeys[16][48],elemtype plainblock[8]){
elemtype cipherbits[64];
elemtype right[48];
int cnt;
char8tobit64(cipherblock,cipherbits);
/*初始置換(ip置換)*/
des_ip_transform(cipherbits);
/*16輪迭代*/
for(cnt = 15; cnt >= 0; cnt--){
memcpy(right,cipherbits 32,32);
/*將右半部分進行擴展置換,從32位擴展到48位*/
des_e_transform(right);
/*將右半部分與子密鑰進行異或操作*/
des_xor(right,subkeys[cnt],48);
/*異或結果進入s盒,輸出32位結果*/
des_sbox(right);
/*p置換*/
des_p_transform(right);
/*將明文左半部分與右半部分進行異或*/
des_xor(cipherbits,right,32);
if(cnt != 0){
/*最終完成左右部的交換*/
des_swap(cipherbits,cipherbits 32);
}
}
/*逆初始置換(ip^1置換)*/
des_ip_1_transform(cipherbits);
bit64tochar8(cipherbits,plainblock);
return 0;
}
/*加密文件*/
int des_encrypt(char *plainfile, char *keystr,char *cipherfile){
file *plain,*cipher;
int count;
elemtype plainblock[8],cipherblock[8],keyblock[8];
elemtype bkey[64];
elemtype subkeys[16][48];
if((plain = fopen(plainfile,"rb")) == null){
return plain_file_open_error;
}
if((cipher = fopen(cipherfile,"wb")) == null){
return cipher_file_open_error;
}
/*設置密鑰*/
memcpy(keyblock,keystr,8);
/*將密鑰轉換為二進制流*/
char8tobit64(keyblock,bkey);
/*生成子密鑰*/
des_makesubkeys(bkey,subkeys);
while(!feof(plain)){
/*每次讀8個位元組,並返回成功讀取的位元組數*/
if((count = fread(plainblock,sizeof(char),8,plain)) == 8){
des_encryptblock(plainblock,subkeys,cipherblock);
fwrite(cipherblock,sizeof(char),8,cipher);
}
}
if(count){
/*填充*/
memset(plainblock count,'\0',7 - count);
/*最後一個字元保存包括最後一個字元在內的所填充的字元數量*/
plainblock[7] = 8 - count;
des_encryptblock(plainblock,subkeys,cipherblock);
fwrite(cipherblock,sizeof(char),8,cipher);
}
fclose(plain);
fclose(cipher);
return ok;
}
/*解密文件*/
int des_decrypt(char *cipherfile, char *keystr,char *plainfile){
file *plain, *cipher;
int count,times = 0;
long filelen;
elemtype plainblock[8],cipherblock[8],keyblock[8];
elemtype bkey[64];
elemtype subkeys[16][48];
if((cipher = fopen(cipherfile,"rb")) == null){
return cipher_file_open_error;
}
if((plain = fopen(plainfile,"wb")) == null){
return plain_file_open_error;
}
/*設置密鑰*/
memcpy(keyblock,keystr,8);
/*將密鑰轉換為二進制流*/
char8tobit64(keyblock,bkey);
/*生成子密鑰*/
des_makesubkeys(bkey,subkeys);
/*取文件長度 */
fseek(cipher,0,seek_end);/*將文件指針置尾*/
filelen = ftell(cipher); /*取文件指針當前位置*/
rewind(cipher); /*將文件指針重指向文件頭*/
while(1){
/*密文的位元組數一定是8的整數倍*/
fread(cipherblock,sizeof(char),8,cipher);
des_decryptblock(cipherblock,subkeys,plainblock);
times = 8;
if(times < filelen){
fwrite(plainblock,sizeof(char),8,plain);
}
else{
break;
}
}
/*判斷末尾是否被填充*/
if(plainblock[7] < 8){
for(count = 8 - plainblock[7]; count < 7; count ){
if(plainblock[count] != '\0'){
break;
}
}
}
if(count == 7){/*有填充*/
fwrite(plainblock,sizeof(char),8 - plainblock[7],plain);
}
else{/*無填充*/
fwrite(plainblock,sizeof(char),8,plain);
}
fclose(plain);
fclose(cipher);
return ok;
}
int main()
{
clock_t a,b;
a = clock();
des_encrypt("1.txt","key.txt","2.txt");
b = clock();
printf("加密消耗%d毫秒\n",b-a);
system("pause");
a = clock();
des_decrypt("2.txt","key.txt","3.txt");
b = clock();
printf("解密消耗%d毫秒\n",b-a);
getchar();
return 0;
}
f. c語言已知解密演算法求加密演算法
以下是加密演算法
voidpasswdencode(char*outstr,char*instr)
{
intinstr_len=strlen(instr);
intoutstr_len=0;
inti=0;
for(i=0;i{
outstr[outstr_len ]=(instr[i]&0x0f) 'm';
outstr[outstr_len ]=((instr[i]>>4)&0x0f) 'm';
}
outstr[outstr_len]='';
return;
}
不過你的加密演算法有兩處要修改。
voidpasswddecode(char*outstr,char*instr){
inti=0; int j=0;
for(;i{
inth=(instr[i]-'m');
intl=(instr[i 1]-'m');
charc=(l<<4) (h&0xf);
outstr[j]=c;
j ;
}
outstr[j]='';/*最後應該在明文最後補''*/
}
java方面不熟悉,不好意思。
g. 求正確的rsa加密解密演算法c語言的,多謝。
rsa演算法它是第一個既能用於數據加密也能用於數字簽名的演算法。它易於理解和操作,也很流行。演算法的名字以發明者的名字命名:ronrivest,adishamir和leonardadleman。但rsa的安全性一直未能得到理論上的證明。它經歷了各種攻擊,至今未被完全攻破。一、rsa演算法:首先,找出三個數,p,q,r,其中p,q是兩個相異的質數,r是與(p-1)(q-1)互質的數p,q,r這三個數便是privatekey接著,找出m,使得rm==1mod(p-1)(q-1)這個m一定存在,因為r與(p-1)(q-1)互質,用輾轉相除法就可以得到了再來,計算n=pqm,n這兩個數便是publickey編碼過程是,若資料為a,將其看成是一個大整數,假設a=n的話,就將a表成s進位(s因為rm==1mod(p-1)(q-1),所以rm=k(p-1)(q-1) 1,其中k是整數因為在molo中是preserve乘法的(x==ymodzan==vmodz=>xu==yvmodz),所以,c==b^r==(a^m)^r==a^(rm)==a^(k(p-1)(q-1) 1)modpq1.如果a不是p的倍數,也不是q的倍數時,則a^(p-1)==1modp(費馬小定理)=>a^(k(p-1)(q-1))==1modpa^(q-1)==1modq(費馬小定理)=>a^(k(p-1)(q-1))==1modq所以p,q均能整除a^(k(p-1)(q-1))-1=>pq|a^(k(p-1)(q-1))-1即a^(k(p-1)(q-1))==1modpq=>c==a^(k(p-1)(q-1) 1)==amodpq2.如果a是p的倍數,但不是q的倍數時,則a^(q-1)==1modq(費馬小定理)=>a^(k(p-1)(q-1))==1modq=>c==a^(k(p-1)(q-1) 1)==amodq=>q|c-a因p|a=>c==a^(k(p-1)(q-1) 1)==0modp=>p|c-a所以,pq|c-a=>c==amodpq3.如果a是q的倍數,但不是p的倍數時,證明同上4.如果a同時是p和q的倍數時,則pq|a=>c==a^(k(p-1)(q-1) 1)==0modpq=>pq|c-a=>c==amodpqq.e.d.這個定理說明a經過編碼為b再經過解碼為c時,a==cmodn(n=pq)但我們在做編碼解碼時,限制0intcandp(inta,intb,intc){intr=1;b=b 1;while(b!=1){r=r*a;r=r%c;b--;}printf("%d\n",r);returnr;}voidmain(){intp,q,e,d,m,n,t,c,r;chars;printf("pleaseinputthep,q:");scanf("%d%d",&p,&q);n=p*q;printf("thenis=\n",n);t=(p-1)*(q-1);printf("thetis=\n",t);printf("pleaseinputthee:");scanf("%d",&e);if(et){printf("eiserror,pleaseinputagain:");scanf("%d",&e);}d=1;while(((e*d)%t)!=1)d ;printf("thencaculateoutthatthedis%d\n",d);printf("thecipherpleaseinput1\n");printf("theplainpleaseinput2\n");scanf("%d",&r);switch(r){case1:printf("inputthem:");/*輸入要加密的明文數字*/scanf("%d",&m);c=candp(m,e,n);printf("thecipheris%d\n",c);break;case2:printf("inputthec:");/*輸入要解密的密文數字*/scanf("%d",&c);m=candp(c,d,n);printf("thecipheris%d\n",m);break;}getch();}
h. 用c語言設計一個簡單地加密算,解密演算法,並說明其中的原理
恰巧這兩天剛看的一種思路,很簡單的加密解密演算法,我說一下吧。
演算法原理很簡單,假設你的原密碼是a,用a與數b按位異或後得到c,c就是加密後的密碼,用c再與數b按位異或後能得回a。即(a異或b)異或b=a。用c實現很簡單的。
這就相當於,你用原密碼a和特定數字b產生加密密碼c,別人拿到這個加密的密碼c,如果不知道特定的數字b,他是無法解密得到原密碼a的。
對於密碼是數字的情況可以用下面的代碼:
#include
#define birthday 19880314
int main()
{
long a, b;
scanf("%ld", &a);
printf("原密碼:%ld\n", a);
b = birthday;
a ^= b;
printf("加密密碼:%ld\n", a);
a ^= b; printf("解密密碼:%ld\n", a);
return 0;
}
如果密碼是字元串的話,最簡單的加密演算法就是對每個字元重新映射,只要加密解密雙方共同遵守同一個映射規則就行啦。