導航:首頁 > 文檔加密 > c語言怎樣給字元串加密

c語言怎樣給字元串加密

發布時間:2024-05-04 13:16:27

⑴ 編寫函數完成字元串的加密與解密(c語言)

C語言代碼和運行結果如下:

輸出符合示例,加解密均正確,望採納~

源碼鏈接:字元串加解密

⑵ [高分]C語言對字元串的加密和解密

char
ch,name[30],over;
FILE
*fp;
printf("請輸入要加密的文件名(正確的做法是:先把解密的數不知道,你想要什麼樣的加密演算法
AES不錯。不過AES是對16個位元組長度加密,要是不是16的倍數,處理有點麻煩據保存到字元串里,全部結束之後,一次性把解密

⑶ C語言怎麼實現對一長串字元進行MD5加密

#include<stdio.h>
#include<stdlib.h>

#defineARR_LEN100

#defineF(x,y,z)((x&y)|(~x&z))
#defineG(x,y,z)((x&z)|(y&~z))
#defineH(x,y,z)(x^y^z)
#defineI(x,y,z)(y^(x|~z))
#defineROTATE_LEFT(x,n)((x<<n)|(x>>(32-n)))
#defineFF(a,b,c,d,x,s,ac)
{
a+=F(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineGG(a,b,c,d,x,s,ac)
{
a+=G(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineHH(a,b,c,d,x,s,ac)
{
a+=H(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}
#defineII(a,b,c,d,x,s,ac)
{
a+=I(b,c,d)+x+ac;
a=ROTATE_LEFT(a,s);
a+=b;
}

typedefstruct{
unsignedintcount[2];
unsignedintstate[4];
unsignedcharbuffer[64];
}MD5_CTX;

voidMD5Init(MD5_CTX*context);
voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen);
voidMD5Final(MD5_CTX*context,unsignedchardigest[16]);
voidMD5Transform(unsignedintstate[4],unsignedcharblock[64]);
voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen);
voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen);

unsignedcharPADDING[]={
0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

voidMD5Init(MD5_CTX*context){
context->count[0]=0;
context->count[1]=0;
context->state[0]=0x67452301;
context->state[1]=0xEFCDAB89;
context->state[2]=0x98BADCFE;
context->state[3]=0x10325476;
}

voidMD5Update(MD5_CTX*context,unsignedchar*input,unsignedintinputlen){
unsignedinti=0,index=0,partlen=0;
index=(context->count[0]>>3)&0x3F;
partlen=64-index;
context->count[0]+=inputlen<<3;
if(context->count[0]<(inputlen<<3))
context->count[1]++;
context->count[1]+=inputlen>>29;

if(inputlen>=partlen){
memcpy(&context->buffer[index],input,partlen);
MD5Transform(context->state,context->buffer);
for(i=partlen;i+64<=inputlen;i+=64)
MD5Transform(context->state,&input[i]);
index=0;
}
else{
i=0;
}
memcpy(&context->buffer[index],&input[i],inputlen-i);
}

voidMD5Final(MD5_CTX*context,unsignedchardigest[16]){
unsignedintindex=0,padlen=0;
unsignedcharbits[8];
index=(context->count[0]>>3)&0x3F;
padlen=(index<56)?(56-index):(120-index);
MD5Encode(bits,context->count,8);
MD5Update(context,PADDING,padlen);
MD5Update(context,bits,8);
MD5Encode(digest,context->state,16);
}

voidMD5Encode(unsignedchar*output,unsignedint*input,unsignedintlen){
unsignedinti=0,j=0;
while(j<len){
output[j]=input[i]&0xFF;
output[j+1]=(input[i]>>8)&0xFF;
output[j+2]=(input[i]>>16)&0xFF;
output[j+3]=(input[i]>>24)&0xFF;
i++;
j+=4;
}
}

voidMD5Decode(unsignedint*output,unsignedchar*input,unsignedintlen){
unsignedinti=0,j=0;
while(j<len){
output[i]=(input[j])|
(input[j+1]<<8)|
(input[j+2]<<16)|
(input[j+3]<<24);
i++;
j+=4;
}
}

voidMD5Transform(unsignedintstate[4],unsignedcharblock[64]){
unsignedinta=state[0];
unsignedintb=state[1];
unsignedintc=state[2];
unsignedintd=state[3];
unsignedintx[64];
MD5Decode(x,block,64);
FF(a,b,c,d,x[0],7,0xd76aa478);/*1*/
FF(d,a,b,c,x[1],12,0xe8c7b756);/*2*/
FF(c,d,a,b,x[2],17,0x242070db);/*3*/
FF(b,c,d,a,x[3],22,0xc1bdceee);/*4*/
FF(a,b,c,d,x[4],7,0xf57c0faf);/*5*/
FF(d,a,b,c,x[5],12,0x4787c62a);/*6*/
FF(c,d,a,b,x[6],17,0xa8304613);/*7*/
FF(b,c,d,a,x[7],22,0xfd469501);/*8*/
FF(a,b,c,d,x[8],7,0x698098d8);/*9*/
FF(d,a,b,c,x[9],12,0x8b44f7af);/*10*/
FF(c,d,a,b,x[10],17,0xffff5bb1);/*11*/
FF(b,c,d,a,x[11],22,0x895cd7be);/*12*/
FF(a,b,c,d,x[12],7,0x6b901122);/*13*/
FF(d,a,b,c,x[13],12,0xfd987193);/*14*/
FF(c,d,a,b,x[14],17,0xa679438e);/*15*/
FF(b,c,d,a,x[15],22,0x49b40821);/*16*/

/*Round2*/
GG(a,b,c,d,x[1],5,0xf61e2562);/*17*/
GG(d,a,b,c,x[6],9,0xc040b340);/*18*/
GG(c,d,a,b,x[11],14,0x265e5a51);/*19*/
GG(b,c,d,a,x[0],20,0xe9b6c7aa);/*20*/
GG(a,b,c,d,x[5],5,0xd62f105d);/*21*/
GG(d,a,b,c,x[10],9,0x2441453);/*22*/
GG(c,d,a,b,x[15],14,0xd8a1e681);/*23*/
GG(b,c,d,a,x[4],20,0xe7d3fbc8);/*24*/
GG(a,b,c,d,x[9],5,0x21e1cde6);/*25*/
GG(d,a,b,c,x[14],9,0xc33707d6);/*26*/
GG(c,d,a,b,x[3],14,0xf4d50d87);/*27*/
GG(b,c,d,a,x[8],20,0x455a14ed);/*28*/
GG(a,b,c,d,x[13],5,0xa9e3e905);/*29*/
GG(d,a,b,c,x[2],9,0xfcefa3f8);/*30*/
GG(c,d,a,b,x[7],14,0x676f02d9);/*31*/
GG(b,c,d,a,x[12],20,0x8d2a4c8a);/*32*/

/*Round3*/
HH(a,b,c,d,x[5],4,0xfffa3942);/*33*/
HH(d,a,b,c,x[8],11,0x8771f681);/*34*/
HH(c,d,a,b,x[11],16,0x6d9d6122);/*35*/
HH(b,c,d,a,x[14],23,0xfde5380c);/*36*/
HH(a,b,c,d,x[1],4,0xa4beea44);/*37*/
HH(d,a,b,c,x[4],11,0x4bdecfa9);/*38*/
HH(c,d,a,b,x[7],16,0xf6bb4b60);/*39*/
HH(b,c,d,a,x[10],23,0xbebfbc70);/*40*/
HH(a,b,c,d,x[13],4,0x289b7ec6);/*41*/
HH(d,a,b,c,x[0],11,0xeaa127fa);/*42*/
HH(c,d,a,b,x[3],16,0xd4ef3085);/*43*/
HH(b,c,d,a,x[6],23,0x4881d05);/*44*/
HH(a,b,c,d,x[9],4,0xd9d4d039);/*45*/
HH(d,a,b,c,x[12],11,0xe6db99e5);/*46*/
HH(c,d,a,b,x[15],16,0x1fa27cf8);/*47*/
HH(b,c,d,a,x[2],23,0xc4ac5665);/*48*/

/*Round4*/
II(a,b,c,d,x[0],6,0xf4292244);/*49*/
II(d,a,b,c,x[7],10,0x432aff97);/*50*/
II(c,d,a,b,x[14],15,0xab9423a7);/*51*/
II(b,c,d,a,x[5],21,0xfc93a039);/*52*/
II(a,b,c,d,x[12],6,0x655b59c3);/*53*/
II(d,a,b,c,x[3],10,0x8f0ccc92);/*54*/
II(c,d,a,b,x[10],15,0xffeff47d);/*55*/
II(b,c,d,a,x[1],21,0x85845dd1);/*56*/
II(a,b,c,d,x[8],6,0x6fa87e4f);/*57*/
II(d,a,b,c,x[15],10,0xfe2ce6e0);/*58*/
II(c,d,a,b,x[6],15,0xa3014314);/*59*/
II(b,c,d,a,x[13],21,0x4e0811a1);/*60*/
II(a,b,c,d,x[4],6,0xf7537e82);/*61*/
II(d,a,b,c,x[11],10,0xbd3af235);/*62*/
II(c,d,a,b,x[2],15,0x2ad7d2bb);/*63*/
II(b,c,d,a,x[9],21,0xeb86d391);/*64*/
state[0]+=a;
state[1]+=b;
state[2]+=c;
state[3]+=d;
}

intmain(intargc,char*argv[]){
inti;
unsignedcharencrypt[ARR_LEN];
unsignedchardecrypt[16];
MD5_CTXmd5;

printf("【測試1】 ");
printf("加密前:Bai ");
printf("加密後: ");
printf(" ");
printf("【測試2】 ");
printf("加密前:Bihaifeng ");
printf("加密後: ");
printf(" ");
printf("====================================================== ");

printf("請輸入需MD5加密的字元串: ");
gets(encrypt);
printf(" ");
printf("====================================================== ");

MD5Init(&md5);
MD5Update(&md5,encrypt,strlen((char*)encrypt));
MD5Final(&md5,decrypt);

printf("加密前:%s 加密後:",encrypt);
for(i=0;i<16;i++){
printf("%02x",decrypt[i]);
}
printf(" ");

getch();
return0;
}

運行結果

以上源代碼非原創~~

⑷ C語言怎麼加密字元

#include<stdio.h>
#include<string.h>
intmain()
{
charstr[]="00000",str2[]="00000",*p=str,*p2=str2;
printf("輸入5個字母:");
while(*p!=0)
{
scanf("%c",p);
if(*p==' ')
continue;
if(*p<'A'||(*p>'Z'&&*p<'a')||*p>'z')//輸入驗證,必須是字母
{
printf("只能輸入字母,請重新輸入 ");
p=str;
p2=str2;
fflush(stdin);//輸入有錯重新輸入前清空緩沖區。fflush屬於c擴展函數,正常使用沒問題,如需在linuxggc上使用,考慮多次調用getchar函數來清空
}
else
{
*p2=(*p)+4;
if(*p2>90&&*p2<97)//大寫字母加4,最大位不超出
*p2='A'+(*p2-90)-1;
if(*p2>122)//小寫字母加4,最大位不超出
*p2='a'+(*p2-122)-1;
p2++;
p++;
}
}

printf("原字元串為:%s 加密後的字元串為:%s ",str,str2);
return0;
}

⑸ C語言 字元串加密

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intmain(void)
{
charc[100];
intk;
intlen,i,temp;

scanf("%s",c);
scanf("%d",&k);

len=(int)strlen(c);
k=k%26;

for(i=0;i<len;i++)
{
if(c[i]>='a'&&c[i]<='z')
{
if(c[i]+k>'z')
{
temp='z'-c[i];
temp=k-temp-1;
c[i]='a'+temp;
}
else
{
c[i]+=k;
}
}
elseif(c[i]>='A'&&c[i]<='Z')
{
if(c[i]+k>'Z')
{
temp='Z'-c[i];
temp=k-temp-1;
c[i]='A'+temp;
}
else
{
c[i]+=k;
}
}
else
{
/*donothing*/
}
}

printf("%s ",c);

return0;
}

⑹ 用C語言編寫一個對稱加密演算法,對字元串加密

/*本問題的關鍵是如何交換ASCII的二進制位,下面提供簡短演算法,並附上VC++6.0環境下的運行結果截圖。

*/

#include<stdio.h>

charswapbit(charc){

chari,num=0,ch[8];

for(i=0;i<8;i++){

ch[i]=c&1;

c=(c>>1);

}

for(i=0;i<8;i++){

num=2*num+ch[i];

}

returnnum;

}

intmain(){

charch;

for(ch='A';ch<='Z';ch++){

printf("%c=%X:%X ",ch,ch,0XFF&swapbit(ch));

}

return0;

}

⑺ C語言字元串按要求加密 求教

1 子函數的修改。只要減掉24 即可,其餘語句多餘。
void encryp(char *plain,char *cipher)
{
int temp;
while(*plain!='\0')
{
temp=*plain-24;
*cipher=temp;
plain++;
cipher++;
}
*cipher='\0';
}
2 對輸出句的修改。改為按數字格式(知識點)輸出即可。
{ int i=0; // 增加一變數 i =0;
。。。。。。。。
while(ch2[i]!='\0')
printf("%d",ch2[i++]);
return 0;
}

⑻ 用C語言實現任意字元串的加密,其中,字母用凱撒加密方法加密,非字母不變

我盡量用注釋闡述了思路,希望可以幫到你!!

#include<stdio.h>
#include<string.h>
#define N 80 //可加密字元串最大長度

char plaintext[N]={0}; //明文,輸入時輸入字元,參與運算時強制轉換成整數
int ciphertext[N]={0}; //密文,保存成整數,輸出時強制轉換成字元
int k; //後(右)移位數,相當於密鑰

void getPlainText() //獲得明文字元串
{
printf("請輸入明文:");
scanf("%s",plaintext);
printf("\n");
}

void getLength() //獲取後(右)移位數(密鑰)
{
printf("請輸入後移的位數:");
scanf("%d",&k);
k%=26; //因為字母只有26個,所以超過26相當於重復
}

void Caesar_cipher() //凱撒加密,本程序採用的是字母循環後(右)移
{
unsigned int i;

for(i=0;i<strlen(plaintext);i++)
{
//兩個bool類型的變數是為了判斷字元是否是字母(包括大寫和小寫)
bool flag1=plaintext[i]>='a'&&plaintext[i]<='z';
bool flag2=plaintext[i]>='A'&&plaintext[i]<='Z';

if(flag1||flag2){ //如果是字母,加密
ciphertext[i]=(int)plaintext[i]+k; //字母在字母表中後(右)移K位
if(ciphertext[i]>(int)'z'){ //保證是循環後(右)移
ciphertext[i]-=26;
}
}
else //非字母字元,不做處理,原樣保存
ciphertext[i]=(int)plaintext[i];

}

}

void printCipherText() //輸出加密後的密文
{
unsigned int i;
printf("\n加密後的密文是:");
for(i=0;i<strlen(plaintext);i++) //把參與計算後是整數強制轉換成對應的字元
printf("%c",(char)ciphertext[i]);
printf("\n");

}

void main()
{
getPlainText(); //明文
getLength(); //後(右)移位數
Caesar_cipher(); //凱撒加密
printCipherText(); //密文

}

⑼ C語言實現將字元串進行加密處理,每個字元的加密規則是,將其轉換為對應的ASCII碼加3後對應 的字元輸出

輸入的是英文字元的話,直接加3就可以,但是如果是中文字元的話,如果直接高位和地位加3也可以,但是解密的時候就有一個不確定的存在,因為中文字元的ascii編碼是有0xfe這樣的存在,加上3的話就到時候還原就變得不確定。所以如果是中文字元加密的話,應該把兩個ascii碼轉合並為兩個位元組無符號類型,然後再加3。

閱讀全文

與c語言怎樣給字元串加密相關的資料

熱點內容
android組件是什麼 瀏覽:971
安卓手機微信怎麼同步信息 瀏覽:179
小人pdf 瀏覽:804
我的世界伺服器怎麼造好看的建築 瀏覽:305
兄弟連培訓php多少錢 瀏覽:249
1523鋪地面的演算法 瀏覽:746
linux源碼安裝php環境 瀏覽:821
ping命令用法 瀏覽:717
日本海軍pdf 瀏覽:469
哪個app有大臉特效 瀏覽:140
自己生活需要解壓嗎 瀏覽:946
考研究生演算法 瀏覽:528
java撤銷 瀏覽:442
學完單片機做點什麼好 瀏覽:312
編譯後運行不了 瀏覽:404
安卓如何設置手機鍵盤大小 瀏覽:847
室內程序員表白方式 瀏覽:694
全民去哪裡找app 瀏覽:124
表格命令CAD 瀏覽:244
柳傳志pdf 瀏覽:632