凱撒加密(Julius Caesar)該方法把一條消息中的每個字母用字母表中固定距離之后的那個字母代替。(如果超越了字母Z,會繞道字母表的起始位置。例如,如果每個字母都用字母表中兩個位置之后的字母代替,那么Y就會被替換為A,Z就會被替換為B。)
然后編寫程序…………
用戶輸入待加密的消息和移位數:
不是字母的不要改動…………
#include <stdio.h> #include <string.h> int main() { char passwd[100],encrypted[100]; int i,j,k,t,move; while(1) { printf("Enter message to be encrypted:"); gets(passwd); printf("Enter shift amount(1-25):"); scanf("%d%*c",&move); for(i=0; i<strlen(passwd); i++) { if(passwd[i] >= 'A' && passwd[i] <= 'Z') { passwd[i] = ((passwd[i]-'A')+move)%26+'A'; } else if(passwd[i] >= 'a' && passwd[i] <= 'z') { passwd[i] = ((passwd[i]-'a')+move)%26+'a'; } } printf("%s",passwd); printf("\n"); } return 0; }
然后就是這樣子
如輸入
Go head, make my day.
3
輸出:Jr dkhdg, pdnh pb gdb.
………………………………………………
如果,輸入這樣就會解密:
Jr dkhdg, pdnh pb gdb.
23
輸出:Go head, make my day.