導航:首頁 > 文檔加密 > 加密和解密語言

加密和解密語言

發布時間:2025-08-02 16:24:38

『壹』 如何用C語言對文件進行加密和解密

對於加密要求不高的完全可以自己定義規則來進行加密。這種加密是很簡單很自由的,例如你在存文件的時候可以將文件中的每個字元都加上一個數,然後讀取該文件的時候再每個字元相應地減去那尺念個數,即可實現就簡單的加密,這樣你儲存的文件看上去就是亂碼了。只是這個規則太簡單,規則你可以自己定,加密與解密對著來就行了。
下面程序用異或操作對文件進行加密和解密
/******************設計思路******************/
//根據用戶輸入的加密/機密密碼,
//每次都拿原文灶困滾件和密碼等長度的一個字元串和密碼
//對應元素異或進行加密/解密
//另外因為是用異或方法,所以加密和解密就是同一個程序
//即按照同樣的加密即是對文件的解密

#include
#include
#include
#include
#include

charfilename[256];//原文件
charpassword[256];//加密/解密密碼
constcharfilenametemp[]="temp15435255435325432543.temp";//加密/解密中間文件

voidinputpass(char*pass);//密碼輸入以"******"顯示

voidmain(){

FILE*fp;//加密/解密的文件
FILE*fptemp;//加密/解密過程臨時文件
intpwdlen;//密碼長度
inti=0;//計數器
charch=0;//讀入的字元

printf("請輸入要加密/解密的文件名(全路徑名):\n");
gets(filename);
if((fp=fopen(filename,"rb"))==NULL){
printf("找不到文件%s\n",filename);
exit(1);
}//if

printf("請輸入要加密/解密的密碼:\n");
inputpass(password);
pwdlen=strlen(password);
if(pwdlen==0){
printf("密碼不能為空,加密/解密失敗\n");
exit(1);
}//if

fptemp=fopen(filenametemp,"wb");//打開中間文件
while(1){
ch=fgetc(fp);//從原文件讀入一個字元
if(feof(fp)){//已經讀到文件尾
break;//退出循環
}
ch^=password[i++];//對原字元和密碼進行異或操作
fputc(ch,fptemp);//將異或結果寫入中間文件
if(i==pwdlen){//使得原文件每和密碼長度相同的固定長度異或加密
i=0;
}
}//while

fclose(fp);//關閉打開原文件
fclose(fptemp);//關閉打開中間文件

remove(filename);//刪除原文件
rename(filenametemp,filename);//將隱余中間文件重命名為原文件
printf("加密/解密成功\n");//至此加密/解密成功

}

//密碼輸入以"******"顯示
voidinputpass(char*pass){
inti=0;
charc;
while(isprint(c=getch())){
pass[i++]=c;
//printf("*");
}
pass[i]='\0'
printf("\n");
}

『貳』 將凱撒密碼X的加密、解密過程用C語言編程實現

1、在密碼學中,愷撒密碼(或稱愷撒加密、愷撒變換、變換加密)是一種最簡單且最廣為人知的加密技術。它是一種替換加密的技術,明文中的所有字母都在字母表上向後(或向前)按照一個固定數目進行偏移後被替換成密文。例如,當偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推。這個加密方法是以愷撒的名字命名的,當年愷撒曾用此方法與其將軍們進行聯系。愷撒密碼通常被作為其他更復雜的加密方法中的一個步驟,例如維吉尼爾密碼。愷撒密碼還在現代的ROT13系統中被應用。但是和所有的利用字母表進行替換的加密技術一樣,愷撒密碼非常容易被破解,而且在實際應用中也無法保證通信安全。例子愷撒密碼的替換方法是通過排列明文和密文字母表,密文字母表示通過將明文字母表向左或向右移動一個固定數目的位置。

2、kaiser加密演算法具體程序:

#include<stdio.h>
#include<conio.h>
charencrypt(charch,intn)/*加密函數,把字元向右循環移位n*/
{
while(ch>='A'&&ch<='Z')
{
return('A'+(ch-'A'+n)%26);
}
while(ch>='a'&&ch<='z')
{
return('a'+(ch-'a'+n)%26);
}
returnch;
}
voidmenu()/*菜單,1.加密,2.解密,3.暴力破解,密碼只能是數字*/
{
clrscr();
printf(" =========================================================");
printf(" 1.Encryptthefile");
printf(" 2.Decryptthefile");
printf(" 3.Forcedecryptfile");
printf(" 4.Quit ");
printf("========================================================= ");
printf("Pleaseselectaitem:");
return;
}
main()
{
inti,n;
charch0,ch1;
FILE*in,*out;
charinfile[20],outfile[20];
textbackground(BLACK);
textcolor(LIGHTGREEN);
clrscr();
sleep(3);/*等待3秒*/
menu();
ch0=getch();
while(ch0!='4')
{
if(ch0=='1')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要加密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入加密密碼*/
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入加密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))/*加密*/
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Encryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='2')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputthekey:");
scanf("%d",&n);/*輸入解密密碼(可以為加密時候的密碼)*/
n=26-n;
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
while(!feof(in))
{
fputc(encrypt(fgetc(in),n),out);
}
printf(" Decryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
if(ch0=='3')
{
clrscr();
printf(" Pleaseinputtheinfile:");
scanf("%s",infile);/*輸入需要解密的文件名*/
if((in=fopen(infile,"r"))==NULL)
{
printf("Cannotopentheinfile! ");
printf("Pressanykeytoexit! ");
getch();
exit(0);
}
printf("Pleaseinputtheoutfile:");
scanf("%s",outfile);/*輸入解密後文件的文件名*/
if((out=fopen(outfile,"w"))==NULL)
{
printf("Cannotopentheoutfile! ");
printf("Pressanykeytoexit! ");
fclose(in);
getch();
exit(0);
}
for(i=1;i<=25;i++)/*暴力破解過程,在察看信息正確後,可以按'Q'或者'q'退出*/
{
rewind(in);
rewind(out);
clrscr();
printf("========================================================== ");
printf("Theoutfileis: ");
printf("========================================================== ");
while(!feof(in))
{
ch1=encrypt(fgetc(in),26-i);
putch(ch1);
fputc(ch1,out);
}
printf(" ======================================================== ");
printf("Thecurrentkeyis:%d ",i);/*顯示當前破解所用密碼*/
printf("Press'Q'toquitandotherkeytocontinue...... ");
printf("========================================================== ");
ch1=getch();
if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'時退出*/
{
clrscr();
printf(" GoodBye! ");
fclose(in);
fclose(out);
sleep(3);
exit(0);
}
}
printf(" Forcedecryptisover! ");
fclose(in);
fclose(out);
sleep(1);
}
menu();
ch0=getch();
}
clrscr();
printf(" GoodBye! ");
sleep(3);
}

『叄』 C語言程序:數據的簡單加密和解密:對於重要的信息,為了在傳輸或存儲時避免泄露,可以在傳輸或存儲前進行加

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
int a,i;
char word[100];
printf("please input the array:");
gets(word);
printf("\nplease input the password:");
scanf("%d",&a);
for(i=0;i<strlen(word);i++)
word[i]+=(a+i);
printf("\n%s",word);
return 0;
}

閱讀全文

與加密和解密語言相關的資料

熱點內容
廣告app是哪裡來的 瀏覽:713
ice伺服器是什麼版本 瀏覽:410
單片機做小車需要哪些零件 瀏覽:377
怎麼節省手機電量安卓 瀏覽:2
iphoneqq郵件伺服器地址 瀏覽:106
新概念英語第二冊pdf 瀏覽:728
最受歡迎的源碼是什麼 瀏覽:475
linux給文件許可權命令 瀏覽:539
hashset介面源碼 瀏覽:879
python動力方程 瀏覽:659
一個編譯器需要多少行代碼 瀏覽:537
安卓7怎麼轉發視頻 瀏覽:245
c51編程教程 瀏覽:199
尼康的相機怎麼創建多個文件夾 瀏覽:465
壓縮餅干開袋即食嗎 瀏覽:158
騰訊雲伺服器創建要多久 瀏覽:831
存儲卡出現奇怪文件夾如何修復 瀏覽:236
恆智天成加密鎖怎麼裝驅動盤 瀏覽:569
醫統天下什麼app 瀏覽:415
程序員那麼可愛里的歌 瀏覽:918