VC移位算法实现数据的加密解密
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
void Shift()
{
char c[100];
int length, i=0, key=0;
printf("/n********Shift Cipher********/nPlease input primal sentence: ");
gets(c);
length = strlen(c);
printf("Input the key(0~26): /n ");
scanf("%d", &key);
getchar();
if(key<0)
{
printf("The value of key is error!/nPress any key to return...");
getch();
return;
}
for(i=0; i<length; i++)
{
if(c[i]>96&&c[i]<123)
c[i] = (c[i]+key-97)%26+97;
else if(c[i]>64&&c[i]<91)
c[i] = (c[i]+key-65)%26+65;
}
printf("Result is: %s/n", c);
for(i=0; i<length; i++)
{
if(c[i]>64&&c[i]<91)
c[i] = (c[i]-key-65+26)%26+97;
}
printf("/nAfter translated the sentence,we can see the primal sentence as follow:/n%s/n", c);
getch();
}
void jiemi()
{
char d[100];
int extent,j=0,password=0;
printf("/n*******shift ciphertext***********/nplease input your ciphertext: ");
gets(d);
extent = strlen(d);
printf("Input the password(0~26): /n ");
scanf("%d", &password);
getchar();
for(j=0; j<extent; j++)
{
if(d[j]>96&&d[j]<123)
d[j] = (d[j]-password-71)%26+97;
else if(d[j]>64&&d[j]<91)
d[j] = (d[j]-password-65+26)%26+65;
}
printf("Result is: %s/n", d);
for(j=0; j<extent; j++)
{
if(d[j]>64&&d[j]<91)
d[j] = (
d[j]-password-65+26)%26+97;
}
printf("/nAfter translated the sentence,we can see the primal sentence as follow:/n%s/n", d);
printf("Press any key to return...");
getch();
}
void menu()
{
system("cls");
printf("/n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
printf("/n 您好:以下是替换加密算法~");
printf("/n pleas select a item:");
printf("/n no.1:Encrypt the file");
printf("/n no.2:Decrypt the file");
printf("/n no.3:Exit");
printf("/n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
void main()
{
int k=0;
menu();
k=getch();
while(k!=3)
{
if(k=='1')
{
Shift();
break;
}
else if(k=='2')
{
jiemi();
break;
}
else if(k=='3')
break;
}
}