Ⅰ c語言設計一個簡單的加密解密程序
C語言設計一個簡單的加密解密程序如下:
加密程序代碼:
#include<stdio.h>
main()
{
char c,filename[20];
FILE *fp1,*fp2;
printf("請輸入待加密的文件名:\n");
scanf("%s",filename);
fp1=fopen(filename,"r");
fp2=fopen("miwen.txt","w");
do
{
c=fgetc(fp1);
if(c>=32&&c<=126)
{
c=c-32;
c=126-c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
解密程序代碼:
#include<stdio.h>
#include<string.h>
main()
{
char c,filename[20];
char yanzhengma[20];
FILE *fp1,*fp2;
printf("請輸入待解密文件名:\n");
scanf("%s",filename);
printf("請輸入驗證碼:\n");
scanf("%s",yanzhengma);
if(strcmp(yanzhengma,"shan")==0)
{
fp1=fopen(filename,"r");
fp2=fopen("yuanwen.txt","w");
do
{
c=fgetc(fp1);
if(c>=32&&c<=126)
{
c=126-c;
c=32+c;
}
if(c!=-1)
fprintf(fp2,"%c",c);
}
while(c!=-1);
}
else
{
printf("驗證碼錯誤!請重新輸入:\n");
scanf("%s",filename);
}
}
Ⅱ C語言(文件的移位與加密解密)
這道題,並不難,只是樓主,沒有說清,是就字母移位嗎?
但是看你的例子,有不全是。
程序如下:
#include <stdio.h>
#include <stdlib.h>
FILE *source;//源文件
FILE *destination;//目標文件
int key;//密鑰
char file[100];//文件名
void encryption()//加密
{
char ch;
printf("請輸入要加密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("無法打開文件!\n");
exit(0);
}
printf("請輸入加密後的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("無法創建文件!\n");
exit(0);
}
printf("請輸入密鑰\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch+=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
void decrypt()//解密
{
char ch;
printf("請輸入要解密的文件名\n");
scanf("%s",file);
if((source=fopen(file,"r"))==NULL)
{
printf("無法打開文件!\n");
exit(0);
}
printf("請輸入加密後的文件名\n");
scanf("%s",file);
if((destination=fopen(file,"w"))==NULL)
{
printf("無法創建文件!\n");
exit(0);
}
printf("請輸入密鑰\n");
scanf("%d",&key);
ch=fgetc(source);
while(ch!=EOF)
{
if(ch=='\n')
{
fputc(ch,destination);
ch=fgetc(source);
continue;
}
ch-=key;
fputc(ch,destination);
ch=fgetc(source);
}
fclose(source);
fclose(destination);
}
int main()//主函數提供菜單
{
int choice=0;
printf("******************\n");
printf("1 文件加密\n");
printf("2 文件解密\n");
printf("3 退出\n");
printf("******************\n");
printf("請輸入1 2 3選擇操作\n");
scanf("%d",&choice);
switch(choice)
{
case 1:encryption();break;
case 2:decrypt();break;
case 3:break;
}
return 0;
}
Ⅲ C語言編程: 文件移位加密與解密。
直接對字元串按字元加減密鑰的位數就可以了。
#include
<iostream.h>
#define
MAX
1000
//加密
char
*
Encryption(char
*E,int
Key)
{
for(int
i=0;*(E+i);i++)
{
*(E+i)
+=
Key;
if(*(E+i)>'z')
*(E+i)
-=
('z'-'a')+1;
}
return
E;
}
//解密
char
*
Decryption(char
*E,int
Key)
{
for(int
i=0;*(E+i);i++)
{
*(E+i)
-=
Key;
if(*(E+i)<'a')
*(E+i)
+=
('z'-'a')+1;
}
return
E;
}
void
main()
{
char
a[MAX];
int
key;
cout<<"輸入字元串:"<<endl;
cin
>>
a;
cout<<"輸入密鑰:"<<endl;
cin
>>key;
cout<<"加密輸出:"<<Encryption(a,key)<<endl;
cout<<"解密輸出:"<<Decryption(a,key)<<endl;
}
Ⅳ 跪求c語言編程問題 文件移位加密與解密 急!
我自己用C#做了一下這功能,注釋都寫上了,希望能幫上你忙 ~ ~using System;
using System.Collections.Generic;
using System.Text;namespace myHomeworkTest30
{
class Program
{
static void Main(string[] args)
{
Emp emp = new Emp();//定義一個對象.自動調用其構造方法,接收字元串並轉化為字元數組
int len=emp.show();//調用實現加密字元串的方法,並返回字元數組的長度
for (int i = 0; i < len-1; i++)
{
Console.Write(emp[i]);//通過索引器進行輸出
}
Console.ReadLine();
}
}
public class Emp
{
public string str = "";//接收字元串
public char[] arr;//字元數組來用存放把字元串轉後成字元
public char first;//加密後的字元串中的第一個字元
public Emp()
{
Console.WriteLine("請輸入一個要加密的字元串:");
str = Console.ReadLine();//接收
arr=str.ToCharArray();//字元串.ToCharArray()把字元串轉化為字元數組
}
public int show()//實現加密的方法
{
first=arr[arr.Length - 1];//把原字元數組中的最後一個字元拿出來作為加密後的第一個字元
Console.WriteLine("加密後的字元串為:");
Console.Write(first);//輸出加密後的第一個字元
for (int i = 0; i < arr.Length; i++)
{
arr[i] = (char)(arr[i] + 3);//對於其它的字元進行加密,+3
}
return arr.Length;//返回字元數組的長度
}
//定義一個索引器
public char this[int index]
{
get
{
return arr[index];
}
set
{
arr[index] = value;
}
}
}
}
Ⅳ 利用C語言實現移位加密和解密演算法
呵呵
這兩天剛編了一個
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char a[500];
gets(a);
int n,i;
n = strlen(a);
for(i=0;i<n;i++)
if(a[i]=='y')
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.
解密試試吧~~
加密的話
改動三個地方就行 自己試試吧 哈哈
Ⅵ c語言 文件移位加密與解密
您說的這個方法,我沒有操作過。 給文件加密,我使用的是超級加密3000. 超級加密3000採用國際上成熟的加密演算法和安全快速的加密方法,可以有效保障數據安全! 具體操作方法: 1 下載安裝超級加密3000。
Ⅶ c語言問題 密碼破解功能,對輸入的字元進行移位操作,輸入字元必須為2
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
char c;
char f[]="t.txt";
FILE *in;
do
{
printf("請輸入一個要加密的英文字母:");
scanf("%c",&c);
if(c>='a' && c<='z')
{
c+=2;
c='a'+(c-'a')%26;
break;
}
else if(c>='A' && c<='Z')
{
c+=36;
c='a'+(c-'a')%26;
break;
}
else
{
printf("你輸入的不是英文字母,請重新輸入。\n");
getchar();
}
}while(1);
in=fopen(f,"w");
fprintf(in,"%c",c);
fflush(in);
fclose(in);
printf("已保存。\n");
system("PAUSE");
return 0;
}
Ⅷ 用C語言編寫文件移位加密與解密程序,如加密:設原文為abcdef,密鑰為5,則有abcdef每個字母按字母表向後
您說的這個方法,我沒有操作過。
給文件加密,我使用的是超級加密3000.
超級加密3000採用國際上成熟的加密演算法和安全快速的加密方法,可以有效保障數據安全!
具體操作方法:
1 下載安裝超級加密3000。
2 然後在需要加密的文件上單擊滑鼠右鍵選擇加密。
3 在彈出的文件加密窗口中設置文件加密密碼就OK了。
Ⅸ 求移位密碼(凱撒密碼)在c#上實現的詳細代碼
忘了說解密了,解密過程完全可以使用上面的代碼,只要把key的值改成26-key就行了。比如,加密的密鑰是3,那麼解密密鑰就是23,這樣就可以使用同一段代碼。至於為什麼+23和-3的效果一樣,還請翻閱數論有關剩餘類環的知識。
Ⅹ C語言密碼移位問題
明文移動k位之後,如果超出了字母z,如:z字母向右移動16位,已經超出了范圍,就需要經過處理
chare(charm,intk)
{
m=m+k;
if(m>90)m=k-90+65;//這里涉及到按鍵碼問題,每個鍵盤都有一個asc2碼,Z=90,A=65
returnm;
}