凯撒密码就是简单的加上一个数,'a'+3='d';'z'+3='c' 假设原文全是小写字母,那么 char plain[N]={...}; //明文 char cipher[N]={};//密文 int key=3; int i=0,temp; for(i=0;iN;i++) {if(plain[i]!=' ') {temp=plain[i]+key-'a'; temp=temp%26; cipher[i]=temp+'a'; } else cipher[i]=plain[i]; } 这样就完成了加密,密文数组里面就是对原文加密后的密文,key是密钥。
def encryption():
str_raw = input("请输入明文:")
k = int(input("请输入位移值:"))
str_change = str_raw.lower()
str_list = list(str_change)
str_list_encry = str_list
i = 0
while i len(str_list):
if ord(str_list[i]) 123-k:
str_list_encry[i] = chr(ord(str_list[i]) + k)
else:
print ("解密结果为:"+"".join(str_list_decry))
while True:
print (u"1. 加密")
print(u"2. 解密")
choice = input("请选择:")
if choice == "1": encryption()
elif choice == "2": decryption()
else: print (u"您的输入有误!")
//1. Math.ceil()用作向上取整。
//2. Math.floor()用作向下取整。
alert(Math.ceil(10/3));//4
alert(Math.floor(10/3));//3
alert(Math.round(10/3));//3
void decrypt(char*pin, char*pout,int k)
{
for(int i=0;pin[i];i++)
{
if(pin[i]='a'pin[i]='z')
{
pout[i] = pin[i]+k;
if(pout[i]'z') pout[i] -= 26;
}
else if(pin[i]='A'pin[i]='Z')
{
pout[i] = pin[i]+k;
if(pout[i]'Z') pout[i] -= 26;
}
else pout[i] = pin[i];
}
pout[i] = '\0';
}