简单的密码技术——凯撒密码(Caesar cipher)

简单的密码技术——凯撒密码(Caesar cipher)


介绍

对于加密技术最早的就是凯撒加密,凯撒密码涉及替换情报中的每个字母,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期恺撒的名字命名的,当年恺撒曾用此方法与其将军们进行联系。

相应代码

主代码
python语言实现

class CaesarCipher:
    '''Class for doing encryption(加密) and decryption(解密) using a Caesar cipher'''
    def __init__(self,shift):
        '''Construcr Caesar cipher given integar shift for ratation'''
        encoder = [None] * 26
        decoder = [None] * 26
        for k in range(26):
            encoder[k] = chr((k+shift)%26 + ord('A'))
            decoder[k] = chr((k-shift)%26 + ord('A'))
        self._forward = ''.join(encoder)
        self._backwad = ''.join(decoder)
    def enctypt(self,message):
        '''Return string representing encrypted message'''
        return self._transform(message,self._forward)
    def decrypt(self,secret):
        '''return decrypted message given encrypted secret'''
        return self._transform(secret,self._backwad)
    def _transform(self,original,code):
        '''Utility to perform transformation based on given code string'''
        msg = list(original.upper())
        for k in range(len(msg)):
            if msg[k].isupper():
                j = ord(msg[k])-ord('A')
                msg[k] = code[j]
        return ''.join(msg)

这里创建了一个类,其中enctypt方法就是加密过程,decrypt方法就是解密过程

测试代码

def main():
    cipher = CaesarCipher(3)
    message = 'i love you'
    code = cipher.enctypt(message)
    print('the secret message is: ',code)
    answer = cipher.decrypt(code)
    print('the real meassage is: ',answer)

if __name__ == '__main__':
    main()

显示结果:
在这里插入图片描述

解释:CaesarCipher类初始化生成self._forwardself._backward两个字符串,分别是

DEFGHIJKLMNOPQRSTUVWXYZABC
XYZABCDEFGHIJKLMNOPQRSTUVW

这里主要通过 _transform(self,original,code)方法这个方法是私有的,通过索引值,在code字符串里面取值,并返回。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c语言编写,欢迎扔板砖 //移位算法 #include #include #define SIZE 50 int main() { //i 用于计数输入个数,j 为临时变量, plain 存放明文, cipher 存放密文,decryption存放解密后文本,fpp 为明文文件指针,fpc 为密文文件指针 int i,j; char plain[SIZE],cipher[SIZE],decryption[SIZE],ciphertext[SIZE]; FILE * fpp,* fpc,* fpd; //加密 //建立新的明文TXT文件 printf("Caesar algorithm\n"); if((fpp=fopen("plain.txt","w+"))==NULL) { printf("creat new plain file error!\n"); exit(0); } //输入明文 printf("input plain alphabet:\n"); i=0; scanf("%c",&plain[i]); while(plain[i]!='\n'&&i<SIZE) { i++; scanf("%c",&plain[i]); } printf("success input %d characters\n",i); //将明文转存到文件中 for(j=0;j<i;j++) { if(fwrite(&plain[j],sizeof(char),1,fpp)!=1) { printf("saving plain file error!\n"); exit(0); } } printf("success saving plain text!\n"); //加密 for(j=0;j<i;j++) { cipher[j]=plain[j]+3; if(cipher[j]99) { printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>122) { cipher[j]=cipher[j]%122+96; printf("cipher %d = %c\n",j,cipher[j]); } else if(cipher[j]>90) { cipher[j]=cipher[j]%90+64; printf("cipher %d = %c\n",j,cipher[j]); } else { printf("cipher %d = %c\n",j,cipher[j]); } } //建立密文文件 if((fpc=fopen("cipher.txt","w+"))==NULL) { printf("create new cipher file error!"); exit(0); } for(j=0;j<i;j++) { if(fwrite(&cipher[j],sizeof(char),1,fpc)!=1) { printf("saving cipher file error!"); exit(0); } } printf("success saving cipher file!"); printf("\n"); //解密 printf("input ciphertext alphabet:\n"); i=0; scanf("%c",&ciphertext[i]); while(ciphertext[i]!='\n'&&i<SIZE) { i++; scanf("%c",&ciphertext[i]); } for(j=0;j<i;j++) { decryption[j]=ciphertext[j]-3; if(decryption[j]90&&decryption[j]<97) { decryption[j]=123-(97-decryption[j]); printf("character %d = %c\n",j,decryption[j]); } else {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值