加密及解密算法(利用C语言)

合集下载

RSA加密解密算法C语言代码

RSA加密解密算法C语言代码

#include<stdio.h>#include<string.h>#include <stdlib.h>#include <time.h>#include <math.h>#include <malloc.h>#define MAX 100#define LEN sizeof(struct slink)void sub(int a[MAX],int b[MAX] ,int c[MAX] );struct slink{int bignum[MAX];/*bignum[98]用来标记正负号,1正,0负bignum[99]来标记实际长度*/struct slink *next;};/*/--------------------------------------自己建立的大数运算库-------------------------------------*/void print( int a[MAX] ){int i;for(i=0;i<a[99];i++)printf("%d",a[a[99]-i-1]);printf("\n\n");return;}int cmp(int a1[MAX],int a2[MAX]){ int l1, l2;int i;l1=a1[99];l2=a2[99];if (l1>l2)return 1;if (l1<l2)return -1;for(i=(l1-1);i>=0;i--){if (a1[i]>a2[i])return 1 ;if (a1[i]<a2[i])return -1;}}void mov(int a[MAX],int *b){int j;for(j=0;j<MAX;j++)b[j]=a[j];return ;}void mul(int a1[MAX],int a2[MAX],int *c) {int i,j;int y;int x;int z;int w;int l1, l2;l1=a1[MAX-1];l2=a2[MAX-1];if (a1[MAX-2]=='-'&& a2[MAX-2]=='-') c[MAX-2]=0;else if (a1[MAX-2]=='-')c[MAX-2]='-';else if (a2[MAX-2]=='-')c[MAX-2]='-';for(i=0;i<l1;i++){for(j=0;j<l2;j++){x=a1[i]*a2[j];y=x/10;z=x%10;w=i+j;c[w]=c[w]+z;c[w+1]=c[w+1]+y+c[w]/10;c[w]=c[w]%10;}}w=l1+l2;if(c[w-1]==0)w=w-1;c[MAX-1]=w;return;}void add(int a1[MAX],int a2[MAX],int *c) {int i,l1,l2;int len,temp[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if((a1[MAX-2]=='-')&&(a2[MAX-2]=='-')) {c[MAX-2]='-';}else if (a1[MAX-2]=='-'){mov(a1,temp);temp[MAX-2]=0;sub(a2,temp,c);return;}else if (a2[MAX-2]=='-'){mov(a2,temp);temp[98]=0;sub(a1,temp,c);return;}if(l1<l2)len=l1;else len=l2;for(i=0;i<len;i++){c[i]=(a1[i]+a2[i]+k)%10;k=(a1[i]+a2[i]+k)/10;}if(l1>len){for(i=len;i<l1;i++){c[i]=(a1[i]+k)%10;k=(a1[i]+k)/10;}if(k!=0){c[l1]=k;len=l1+1;}}else{for(i=len;i<l2;i++){c[i]=(a2[i]+k)%10;k=(a2[i]+k)/10;}if(k!=0){c[l2]=k;len=l2+1;}else len=l2;}c[99]=len;return;}void sub(int a1[MAX],int a2[MAX],int *c) {int i,l1,l2;int len,t1[MAX],t2[MAX];int k=0;l1=a1[MAX-1];l2=a2[MAX-1];if ((a1[MAX-2]=='-') && (a2[MAX-2]=='-')) {mov(a1,t1);mov(a2,t2);t1[MAX-2]=0;t2[MAX-2]=0;sub(t2,t1,c);return;}else if( a2[MAX-2]=='-'){mov(a2,t2);t2[MAX-2]=0;add(a1,t2,c);return;}else if (a1[MAX-2]=='-'){mov(a2,t2);add(a1,t2,c);return;}if(cmp(a1,a2)==1){len=l2;for(i=0;i<len;i++){if ((a1[i]-k-a2[i])<0){c[i]=(a1[i]-a2[i]-k+10)%10;k=1;}else{c[i]=(a1[i]-a2[i]-k)%10;k=0;}}for(i=len;i<l1;i++){if ((a1[i]-k)<0){c[i]=(a1[i]-k+10)%10;k=1;}else{c[i]=(a1[i]-k)%10;k=0;}}if(c[l1-1]==0)/*使得数组C中的前面所以0字符不显示了,如1000-20=0980--->显示为980了*/ {len=l1-1;i=2;while (c[l1-i]==0)/*111456-111450=00006,消除0后变成了6;*/{len=l1-i;i++;}else{len=l1;}}elseif(cmp(a1,a2)==(-1)){c[MAX-2]='-';len=l1;for(i=0;i<len;i++){if ((a2[i]-k-a1[i])<0){c[i]=(a2[i]-a1[i]-k+10)%10;k=1;}else{c[i]=(a2[i]-a1[i]-k)%10;k=0;}}for(i=len;i<l2;i++){if ((a2[i]-k)<0){c[i]=(a2[i]-k+10)%10;k=1;}else{c[i]=(a2[i]-k)%10;k=0;}}if(c[l2-1]==0){len=l2-1;i=2;while (c[l1-i]==0){len=l1-i;i++;}}else len=l2;}else if(cmp(a1,a2)==0){len=1;c[len-1]=0;}c[MAX-1]=len;return;}void mod(int a[MAX],int b[MAX],int *c)/*/c=a mod b//注意:经检验知道此处A和C的数组都改变了。

C语言中的数据加密与解密技术

C语言中的数据加密与解密技术

C语言中的数据加密与解密技术数据加密与解密技术在现代信息安全领域中起着至关重要的作用,能够保护数据的隐私性和完整性,防止数据被非法窃取和篡改。

在C语言中,我们可以利用各种加密算法实现数据加密和解密操作,确保数据在传输和存储过程中的安全性。

在C语言中,常用的数据加密算法包括对称加密算法和非对称加密算法。

对称加密算法使用相同的密钥进行加密和解密操作,简单高效,适用于小规模数据加密。

常见的对称加密算法有DES、AES等。

非对称加密算法需要一对公钥和私钥,公钥用于加密数据,私钥用于解密数据,安全性更高,适用于加密大规模数据。

常见的非对称加密算法有RSA、ECC等。

在C语言中,我们可以使用openssl库或者自行实现上述加密算法来实现数据加密和解密。

下面以AES对称加密算法为例进行介绍。

首先,我们需要在C语言中引入openssl库,通过以下方式进行安装和引入:```sudo apt-get install libssl-dev``````c#include <openssl/aes.h>#include <openssl/rand.h>```接下来,我们可以定义一个函数来实现数据的加密和解密操作:```cvoid encrypt_decrypt_data(unsigned char *data, int data_len, unsigned char *key, unsigned char *iv, int encrypt) {AES_KEY aes_key;AES_set_encrypt_key(key, 128, &aes_key);if (encrypt) {AES_cbc_encrypt(data, data, data_len, &aes_key, iv, AES_ENCRYPT);} else {AES_cbc_encrypt(data, data, data_len, &aes_key, iv, AES_DECRYPT);}}```在上面的函数中,我们使用AES算法对数据进行加密或解密操作,其中data 为待加密或解密的数据,data_len为数据长度,key为密钥,iv为初始化向量,encrypt为加密标志。

C语言字符串加密和解密算法

C语言字符串加密和解密算法

C语言字符串加密和解密算法在本实例中要求设计一个加密和解密算法。

在对一个指定的字符串加密之后,利用解密函数能够对密文解密,显示明文信息。

加密的方式是将字符串中每个字符加上它在字符串中的位置和一个偏移值5。

以字符串“mrsoft”为例,第一个字符“m”在字符串中的位置为0,那么它对应的密文是“'m'+0+5",即r。

算法思想在main() 函数中使用while 语句设计一个无限循环,并定义两个字符数组,用来保存,明文和密文字符串,在首次循环中要求用户输入字符串,进行将明文加密成密文的操作,之后的操作则是根据用户输入的命令字符进行判断,输入1 加密新的明文,输入2 对刚加密的密文进行解密,输入3 退出系统。

程序代码1.#include<stdio.h>2.#include<string.h>3.int main()4.{5.int result=1;6.int i;7.int count=0;8.char Text[128]={'\0'};9.char cryptograph[128]={'\0'};10.while(1)11.{12.if(result==1)13.{14.printf("请输入要加密的明文:\n");15.scanf("%s",&Text);16. count=strlen(Text);17.for(i=0;i<count;i++)18.{19. cryptograph[i]=Text[i]+i+5;20.}21. cryptograph[i]='\0';22.printf("加密后的密文是:%s\n",cryptograph);23.}24.else if(result==2)25.{26. count=strlen(Text);27.for(i=0;i<count;i++)28.{29. Text[i]=cryptograph[i]-i-5;30.}31. Text[i]='\0';32.printf("解密后的明文是:%s\n",Text);33.}34.else if(result==3)35.{36.break;37.}38.else39.{40.printf("请输入正确的命令符:\n");41.}42.printf("输入1加密新的明文,输入2对刚加密的密文进行解密,输入3退出系统:\n");43.printf("请输入命令符:\n");44.scanf("%d",&result);45.}46.return0;47.}调试运行结果本实例中,输入了I love Xichang College 字符串作为例证,因此输出是多个加密后的字符串,结果如下所示:。

用C语言实现DES加密和解密

用C语言实现DES加密和解密

用C语言实现DES加密与解密#include<iostream.h>int IP[64] = {58,50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,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};int IP_1[64] = {40,8,48,16,56,24,64,32,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};int E[48] = {32,1, 2, 3, 4, 5,4, 5, 6, 7, 8, 9,8, 9, 10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1};int P[32]={16 ,7 , 20 , 21 , 29,12 ,28 , 17 ,1, 15 ,23 , 26 ,5, 18 ,31 , 10 ,2, 8 , 24 , 14 ,32,27, 3 , 9 ,19,13, 30 , 6 ,22,11 ,4 , 25 };19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4},PC2[48]={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};Char_to_Int(in,Msg_Int,8);//正确O_to_B(Msg_Int,Msg_Bin,8);//正确cout<<endl;Convert(Msg_Bin,Msg_Bin_PC1,PC1,56); Divide(Msg_Bin_PC1,C[0],D[0],56);for(i=0;i<16;i++){if(i==0){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==1){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==8){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else if(i==15){RLC1(C[i],C[i+1],28);RLC1(D[i],D[i+1],28);}else{RLC2(C[i],C[i+1],28);RLC2(D[i],D[i+1],28);Combine(C[i+1],D[i+1],Key_Bin,56); Convert(Key_Bin,out[i],PC2,48);}//生成子密钥正确void S_box(int in[],int out[]){int i;int SBox[8][64] ={{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 },{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 },{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 },{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 },{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, },{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 },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},{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}};int s1,s3,s2[8];for(i=0;i<8;i++){s1=in[i*6]*2+in[i*6+5]*1;s3=in[i*6+1]*8+in[i*6+2]*4+in[i*6+3]*2+in[i*6+4]*1; s2[i]=SBox[i][s1*16+s3];}int j;for(i=0;i<8;i++)for(j=i*4+3;j>=i*4;j--){out[j]=s2[i]%2; s2[i]=s2[i]/2;void Encode(unsigned char in[],unsigned char Final_H[],int K[16][48]) {int i;intORINT[8],ORBYTE[64],FinalBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],Fi nalINT[8],L[17][32],R[17][32];char ORH[16];void DeCode(unsigned char in[],unsigned char Final_H[],int K[16][48]) {int i;intORINT[8],ORBYTE[64],LR[64],R48[48],S_in[48],S_out[32],F_RL[32],FinalBYTE[64],Fi nalINT[8],L[17][32],R[17][32];char ORH[16];Char_to_Int(in,ORINT,8);cout<<"!!!!!"<<endl;O_to_B(ORINT,ORBYTE,8);Convert(ORBYTE,LR,IP,64);Divide(LR,L[0],R[0],64);for(i=1;i<17;i++){COPY(R[i-1],L[i],32);Convert(R[i-1],R48,E,48);XOR(R48,K[16-i],S_in,48);S_box(S_in,S_out);Convert(S_out,F_RL,P,32);XOR(L[i-1],F_RL,R[i],32);}Combine(R[16],L[16],LR,64);Convert(LR,FinalBYTE,IP_1,64);B_to_H(FinalBYTE,Final_H);}void main(){int i;unsigned char Msg[8],UnCodeMsg[16],CodeMsg[16],Key[8],Msg_H[16],CodeChar[8];int SKey[16][48];int OrByte[64],OrInt[8],FinalByte[64],FinalInt[8];cout<<"请输入明文:"<<endl;for(i=0;i<8;i++)cin>>Msg[i];cout<<"请输入初始密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);Encode(Msg,CodeMsg,SKey);cout<<"十六进制的密文:"<<endl;for(i=0;i<16;i++)cout<<CodeMsg[i];cout<<endl;cout<<"字符型密文:"<<endl;H_to_B(CodeMsg,FinalByte);B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8); for(i=0;i<8;i++)cout<<CodeChar[i];cout<<endl;cout<<"请输入十六进制的密文:"<<endl;for(i=0;i<16;i++)cin>>CodeMsg[i];B_to_O(FinalByte,FinalInt,8);Int_to_Char(FinalInt,CodeChar,8); cout<<"请输入解密密钥:"<<endl;for(i=0;i<8;i++)cin>>Key[i];GetKey(Key,SKey);DeCode(CodeChar,Msg_H,SKey);//for(i=0;i<16;i++)//cout<<Msg_H[i];cout<<endl;H_to_B(Msg_H,OrByte);B_to_O(OrByte,OrInt,8);Int_to_Char(OrInt,Msg,8);cout<<"明文是:"<<endl;for(i=0;i<8;i++)cout<<Msg[i];cout<<endl;}欢迎您的下载,资料仅供参考!。

C语言实现加密解密功能

C语言实现加密解密功能

C语⾔实现加密解密功能加密主要是通过⼀种算法对原内容进⾏处理,使原来内容不直观可见。

解密过程通常要知道加密的算法,然后对加密后的内容进⾏逆处理,从⽽实现解密功能。

当然解密也有⼀些暴⼒破解的⽅法。

接下来以 c 语⾔为例讲解⼀种简单的加密解密以及暴⼒破解字符串的⽅法,带您⾛进加密解密技术的⼤门。

先讲⼀下凯撒加密,凯撒密码相传是古罗马凯撒⼤帝⽤来保护重要军情的加密系统。

它是⼀种置换密码,通过将字母顺序推后起到加密作⽤。

如字母顺序推后 3 位,字母 A 将被推作字母 D,字母 B 将被推作字母 E。

本实例类似于凯撒加密。

加密算法:⼤(⼩)写字母加密后还为⼤(⼩)写字母。

⼤(⼩)写字母向后推 n 位,n 由⽤户输⼊,如果超出⼤(⼩)写字母的 ASCII 范围,则返回⾄第⼀个⼤(⼩)写字母继续循环。

解密算法(与加密算法正好相反):⼤(⼩)写字母解密后还为⼤(⼩)写字母。

⼤(⼩)写字母向前推 n 位,n 由⽤户输⼊,如果超出⼤(⼩)写字母的 ASCII 范围,则返回⾄最后⼀个⼤(⼩)写字母继续循环。

代码如下:/*字符串加密解密程序凯撒加密*/#include <stdio.h>#include <stdlib.h>#include <string.h>//函数encode()将字母顺序推后n位,实现⽂件加密功能void encode(char str[],int n){char c;int i;for(i=0;i<strlen(str);++i){ //遍历字符串c=str[i];if(c>='a' && c<='z'){ //c是⼩写字母if(c+n%26<='z'){ //若加密后不超出⼩写字母范围str[i]=(char)(c+n%26); //加密函数}else{ //加密后超出⼩写字母范围,从头开始循环⼩写字母str[i]=(char)(c+n%26-26);}}else if(c>='A' && c<='Z'){ //c为⼤写字母if(c + n%26 <= 'Z'){ //加密后不超出⼤写字母范围str[i]=(char)(c+n%26);}else{ //加密后超出⼤写字母范围,从头开始循环⼤写字母str[i]=(char)(c+n%26-26);}}else{ //不是字母,不加密str[i]=c;}}printf("\nAfter encode: \n");puts(str); //输出加密后的字符串}//decode()实现解密功能,将字母顺序前移n位void decode(char str[],int n){char c;int i;//遍历字符串for(i=0;i<strlen(str);++i){c=str[i];//c为⼩写字母if(c>='a' && c<='z'){//解密后还为⼩写字母,直接解密if(c-n%26>='a'){str[i]=(char)(c-n%26);}else{//解密后不为⼩写字母了,通过循环⼩写字母处理为⼩写字母str[i]=(char)(c-n%26+26);}}else if(c >= 'A' && c<='Z'){ //c为⼤写字母if(c-n%26>='A'){ //解密后还为⼤写字母str[i]=(char)(c-n%26);}else{ //解密后不为⼤写字母了,循环⼤写字母,处理为⼤写字母str[i]=(char)(c-n%26+26);}}else{ //⾮字母不处理str[i]=c;}}printf("\nAfter decode: \n");puts(str); //输出解密后的字符串}//该函数代码有冗余,读者可改进int main(){char str[50];int k=0,n=0,i=1;printf("\nPlease input strings: ");scanf("%s",str); //输⼊加密解密字符串//打印菜单printf("-----------------\n");printf("1: Encryption\n");printf("2: Decryption\n");printf("3: Violent Crack\n"); //暴⼒破解printf("-----------------\n");printf("\nPlease choose: ");scanf("%d",&k);if(k==1){ //加密printf("\nPlease input number: ");scanf("%d",&n);encode(str,n);}else if(k==2){ //解密printf("\nPlease input number: ");scanf("%d",&n);decode(str,n);}else{for(i=1;i<=25;++i){ //尝试所有可能的n值进⾏暴⼒破解printf("%d ",i);decode(str,i);}}return 0;}测试运⾏如下:成功实现加密解密功能,读者可以稍加改造完成对⽂件的加密解密以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

C语言加密与解密常用的加密算法和协议

C语言加密与解密常用的加密算法和协议

C语言加密与解密常用的加密算法和协议在计算机网络和信息安全领域,加密是一种广泛应用的技术,可以保护敏感数据的机密性和完整性。

C语言作为一种广泛使用的编程语言,提供了丰富的加密算法和协议的库函数,本文将介绍一些常用的加密算法和协议。

一、对称加密算法对称加密算法是指加密和解密使用相同密钥的算法,它的运算速度较快,适用于加密大量数据。

以下是几个常用的对称加密算法:1. DES(Data Encryption Standard)DES是一种基于对称密钥的加密算法,使用56位密钥进行加密和解密。

DES算法已经被证明在保护数据的机密性方面是有效的,但由于其较短的密钥长度,现在已经逐渐被更安全的算法取代。

2. AES(Advanced Encryption Standard)AES是一种高级加密标准算法,使用128、192或256位密钥进行加密和解密。

AES算法被广泛应用于各种领域,包括数据传输、硬盘加密和无线网络安全等。

它的安全性和性能都得到了广泛认可。

3. RC4(Rivest Cipher 4)RC4是一种流密码算法,它能够根据密钥流生成伪随机的密钥序列,并将明文与密钥序列进行异或操作,从而实现加密和解密功能。

尽管RC4算法在过去被广泛使用,但由于其存在一些安全漏洞,现在已经不推荐使用。

二、非对称加密算法非对称加密算法是指加密和解密使用不同密钥的算法,它能够提供更高的安全性,但性能较对称加密算法要低。

以下是几个常用的非对称加密算法:1. RSA(Rivest-Shamir-Adleman)RSA是一种基于大数因子分解的算法,广泛应用于数字签名、密钥交换和数据加密等领域。

它的安全性基于大数分解问题的困难性,目前被认为是非常安全的加密算法。

2. ECC(Elliptic Curve Cryptography)ECC是一种基于椭圆曲线的加密算法,它通过找到椭圆曲线上的一个点来生成公钥和私钥。

相较于RSA算法,ECC算法在提供相同安全性的情况下使用更短的密钥长度,从而提高了加密和解密的效率。

用C语言简单加密解密

用C语言简单加密解密

⽤C语⾔简单加密解密使⽤char表⽰的字符型数据,在本质上与我们前⾯介绍的整型数据并⽆太⼤的区别,只是char类型占⽤的内存字节数更⼩,能够表⽰的数据范围更⼩⽽已。

在使⽤上,char被专门⽤来表⽰C语⾔的字符集中的各种字符,不要把它当成⼀个整型数据类型来使⽤。

对于字符类型,我们常常利⽤它来处理字符串中的单个字符或者是实现⼀些字符游戏。

例如,我们可以对字符串中的单个字符进⾏运算,实现字符串的简单加密:#include <stdio.h>#include <string.h> // strlen()函数所在的头⽂件#include <ctype.h> // isalpha()函数所在的头⽂件int main(){// 定义⼀个明⽂字符串char msg[] = "This is C program!";int i = 0 ;// 逐个遍历字符串中的字符,对其进⾏处理for(i=0; i<strlen(msg); i++){// 获得字符串中的当前字符char cur = msg[i];// 判断当前字符是否是字母字符if(isalpha(cur))// 对字母字符进⾏简单加密处理,然后重新写回字符串msg[i] = cur +1;}// 输出加密后的字符串printf("the encrypted message is: %s\n",msg);//解码for(i=0; i<strlen(msg); i++){char c =msg[i];if(isalpha(c)){msg[i] = c - 1;}}printf("the decrypted message is: %s\n",msg);return0;}这样实现了C语⾔的简单加密和解密!。

C加密解密算法

C加密解密算法

1、方法一 (不可逆)public string EncryptPassword(string PasswordString,string PasswordFormat ){string encryptPassword = null;if (PasswordFormat="SHA1"){encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(PasswordS tring,"SHA1");}elseif (PasswordFormat="MD5"){ encryptPassword=FormsAuthortication.HashPasswordForStoringInConfigFile(Passwor dString,"MD5");}return encryptPassword ;}2、方法二 (可逆)public interface IBindesh{string encode(string str);string decode(string str);}public class EncryptionDecryption : IBindesh{public string encode(string str){string htext = "";for ( int i = 0; i < str.Length; i++){htext = htext + (char) (str[i] + 10 - 1 * 2);}return htext;public string decode(string str){string dtext = "";for ( int i=0; i < str.Length; i++){dtext = dtext + (char) (str[i] - 10 + 1*2);}return dtext;}3、方法三 (可逆)const string KEY_64 = "VavicApp";//注意了,是8个字符,64位const string IV_64 = "VavicApp";public string Encode(string data){byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();int i = cryptoProvider.KeySize;MemoryStream ms = new MemoryStream();CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey,byIV), CryptoStreamMode.Write);StreamWriter sw = new StreamWriter(cst);sw.Write(data);sw.Flush();cst.FlushFinalBlock();sw.Flush();return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);public string Decode(string data){byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);byte[] byEnc;try{byEnc = Convert.FromBase64String(data);}catch{return null;}DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();MemoryStream ms = new MemoryStream(byEnc);CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey,byIV), CryptoStreamMode.Read);StreamReader sr = new StreamReader(cst);return sr.ReadToEnd();}4、MD5不可逆加密(32位加密)public string GetMD5(string s, string _input_charset){/**//**//**//// <summary>/// 与ASP兼容的MD5加密算法/// </summary>MD5 md5 = new MD5CryptoServiceProvider();byte[] t = puteHash(Encoding.GetEncoding(_input_charset).GetBytes(s));StringBuilder sb = new StringBuilder(32);for (int i = 0; i < t.Length; i++){sb.Append(t[i].ToString("x").PadLeft(2, '0'));}return sb.ToString();}(16位加密)public static string GetMd5Str(string ConvertString){MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();string t2 =BitConverter.ToString(puteHash(UTF8Encoding.Default.GetBytes(ConvertStrin g)), 4, 8);t2 = t2.Replace("-", "");return t2;}5、加解文本文件//加密文件private static void EncryptData(String inName, String outName, byte[] desKey, byte[]desIV){//Create the file streams to handle the input and output files.FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);fout.SetLength(0);//Create variables to help with read and write.byte[] bin = new byte[100]; //This is intermediate storage for the encryption.long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written ata time.DES des = new DESCryptoServiceProvider();CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV),CryptoStreamMode.Write);//Read from the input file, then encrypt and write to the output file.while (rdlen < totlen){len = fin.Read(bin, 0, 100);encStream.Write(bin, 0, len);rdlen = rdlen + len;}encStream.Close();fout.Close();fin.Close();}//解密文件private static void DecryptData(String inName, String outName, byte[] desKey, byte[]desIV){//Create the file streams to handle the input and output files.FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);fout.SetLength(0);//Create variables to help with read and write.byte[] bin = new byte[100]; //This is intermediate storage for the encryption.long rdlen = 0; //This is the total number of bytes written. long totlen = fin.Length; //This is the total length of the input file. int len; //This is the number of bytes to be written at a time.DES des = new DESCryptoServiceProvider();CryptoStream encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV),CryptoStreamMode.Write);//Read from the input file, then encrypt and write to the output file. while (rdlen < totlen){len = fin.Read(bin, 0, 100);encStream.Write(bin, 0, len);rdlen = rdlen + len;}encStream.Close();fout.Close();fin.Close();}6、using System;using System.Collections.Generic;using System.Text;using System.Security.Cryptography;using System.IO;namespace Component{public class Security{public Security(){}//默认密钥向量private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };/**//**//**//**//**//**//**//// <summary>/// DES加密字符串/// </summary>/// <param name="encryptString">待加密的字符串</param>/// <param name="encryptKey">加密密钥,要求为8位</param>/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>public static string EncryptDES(string encryptString, string encryptKey){try{byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));byte[] rgbIV = Keys;byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream();CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey,rgbIV), CryptoStreamMode.Write);cStream.Write(inputByteArray, 0, inputByteArray.Length);cStream.FlushFinalBlock();return Convert.ToBase64String(mStream.ToArray());}catch{return encryptString;}}/**//**//**//**//**//**//**//// <summary>/// DES解密字符串/// </summary>/// <param name="decryptString">待解密的字符串</param>/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns>public static string DecryptDES(string decryptString, string decryptKey) {try{byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);byte[] rgbIV = Keys;byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream();CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey,rgbIV), CryptoStreamMode.Write);cStream.Write(inputByteArray, 0, inputByteArray.Length);cStream.FlushFinalBlock();return Encoding.UTF8.GetString(mStream.ToArray()); }catch{return decryptString;}}}}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档