本文目錄一覽:
c語言 數據加密
#include stdio.h
void main()
{
int a[5]; /* 存儲各位上的數字 */
int num, temp, encripy; /* num是要輸入的數,temp是交換時用來存儲臨時值,encripy是加密後的數據 */
int i;
do
{
printf(“Please input the number:”);
scanf(“%d”,num);
if(!(num/10000 !=0 num/100000==0))
printf(“Data error!\n”);
}while(!(num/10000 !=0 num/100000==0));
a[0] = num/10000%10; /* 求各位上的數字 */
a[1] = num/1000%10;
a[2] = num/100%10; /* 百位上的數字 */
a[3] = num/10%10; /* 十位上的數字 */
a[4] = num%10; /* 個位上的數字 */
for(i = 0; i 5; ++i) /* 開始加密 */
a[i] = (a[i] + 8)%10;
temp = a[0]; /* 交換位置開始 */
a[0] = a[3];
a[3] = temp;
temp = a[1];
a[1] = a[2];
a[2] = temp; /* 交換位置結束同時加密結束 */
encripy = a[0]*10000 + a[1]*1000 + a[2]*100 + a[3]*10 + a[4]; /* 加密後的數據 */
printf(“\nThe scourse number: %d\n”, num); /* 輸出原數據 */
printf(“\nEncripy the number: %d\n\n”, encripy); /* 輸出加密後的數據 */
}
在VC6.0成功運行,希望對你有幫助!
C語言文件加密
#includestdio.h
int main()
{char ch;
FILE *fp1,*fp2;
fp1=fopen(“d:\\file1.txt”,”r”);
fp2=fopen(“d:\\file2.txt”,”w”);
printf(“加密後的內容:\n”);
while((ch=fgetc(fp1))!=EOF)
{ch^=0x6a; putchar(ch); fputc(ch,fp2);}
fclose(fp1);
fclose(fp2);
printf(“\n解密後的內容:\n”);
fp2=fopen(“d:\\file2.txt”,”r”);
while((ch=fgetc(fp2))!=EOF)
{ch^=0x6a; putchar(ch);}
return 0;
}
C語言怎麼加密字元
我沒注意只要小寫,我寫的是大小寫都可以的,另外附送輸入驗證。
#include stdio.h
#include string.h
int main()
{
char str[]=”00000″,str2[]=”00000″,*p=str,*p2=str2;
printf(“輸入5個字母:”);
while(*p!=0)
{
scanf(“%c”,p);
if(*p==’\n’)
continue;
if(*p’A’||(*p’Z’*p’a’) || *p’z’) //輸入驗證,必須是字母
{
printf(“只能輸入字母,請重新輸入\n”);
p=str;
p2=str2;
fflush(stdin);//輸入有錯重新輸入前清空緩衝區。fflush屬於c擴展函數,正常使用沒問題,如需在linux ggc上使用,考慮多次調用getchar函數來清空
}
else
{
*p2=(*p)+4;
if(*p290 *p297) //大寫字母加4,最大位不超出
*p2=’A’+(*p2-90)-1;
if(*p2122) //小寫字母加4,最大位不超出
*p2=’a’+(*p2-122)-1;
p2++;
p++;
}
}
printf(“原字元串為:%s\n加密後的字元串為:%s\n”,str,str2);
return 0;
}
原創文章,作者:GUIA,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/145772.html