ROT系列密码加解密实现(python)

ROT家族

ROT5加解密算法

加密方法:

  1. 将数字字符转换为整数
  2. 将整数加5,对10取模,得到加密后的整数
  3. 将加密后的整数转换为数字字符

解密方法:

  1. 将数字字符转换为整数
  2. 将整数减去5,若结果小于0,则加上10
  3. 将解密后的整数转换为数字字符

加解密脚本

def rot5_encrypt(plaintext):
    ciphertext = ""
    for c in plaintext:
        if c.isdigit():
            new_digit = (int(c) + 5) % 10
            ciphertext += str(new_digit)
        else:
            ciphertext += c
    return ciphertext

def rot5_decrypt(ciphertext):
    plaintext = ""
    for c in ciphertext:
        if c.isdigit():
            new_digit = (int(c) - 5) % 10
            plaintext += str(new_digit)
        else:
            plaintext += c
    return plaintext

ROT13加解密算法

加密过程:

  1. 首先,将明文中的每个字母替换成它在字母表中顺序排列的第13个字母。例如,明文中的字母A将替换为N,字母B将替换为O,以此类推。
  2. 如果字母表中的字母已经到了Z,则继续从字母表的开头(即字母A)开始计数。
  3. 对于非字母字符(例如数字、标点符号和空格),不进行加密,直接保留原样。
  4. 最终输出加密后的密文。

解密过程:

  1. 将密文中的每个字母替换为它在字母表中顺序排列的前13个字母,即将字母N替换为A,字母O替换为B,以此类推。
  2. 如果字母表中的字母已经到了A,则继续从字母表的末尾(即字母Z)开始计数。
  3. 对于非字母字符,不进行解密,直接保留原样。
  4. 最终输出解密后的明文。

加解密脚本

def rot13_encrypt(plaintext: str) -> str:
    ciphertext = ""
    for c in plaintext:
        if c.isalpha():
            if c.isupper():
                new_ascii = (ord(c) - 65 + 13) % 26 + 65
            else:
                new_ascii = (ord(c) - 97 + 13) % 26 + 97
            ciphertext += chr(new_ascii)
        else:
            ciphertext += c
    return ciphertext


def rot13_decrypt(ciphertext: str) -> str:
    plaintext = ""
    for c in ciphertext:
        if c.isalpha():
            if c.isupper():
                new_ascii = (ord(c) - 65 - 13) % 26 + 65
            else:
                new_ascii = (ord(c) - 97 - 13) % 26 + 97
            plaintext += chr(new_ascii)
        else:
            plaintext += c
    return plaintext

ROT18加解密算法

加密过程:

  1. 首先,将明文中的每个字母替换成它在字母表中顺序排列的第18个字母。例如,明文中的字母A将替换为S,字母B将替换为T,以此类推。
  2. 如果字母表中的字母已经到了Z,则继续从字母表的开头(即字母A)开始计数。
  3. 对于非字母字符(例如数字、标点符号和空格),不进行加密,直接保留原样。
  4. 最终输出加密后的密文。

解密过程:

  1. 将密文中的每个字母替换为它在字母表中顺序排列的前18个字母,即将字母S替换为A,字母T替换为B,以此类推。
  2. 如果字母表中的字母已经到了A,则继续从字母表的末尾(即字母Z)开始计数。
  3. 对于非字母字符,不进行解密,直接保留原样。
  4. 最终输出解密后的明文。

加解密脚本

def rot18_encrypt(plaintext: str) -> str:
    ciphertext = ""
    for c in plaintext:
        if c.isalpha():
            if c.isupper():
                new_ascii = (ord(c) - 65 + 18) % 26 + 65
            else:
                new_ascii = (ord(c) - 97 + 18) % 26 + 97
            ciphertext += chr(new_ascii)
        else:
            ciphertext += c
    return rot5_encrypt(rot13_encrypt(plaintext))


def rot18_decrypt(ciphertext: str) -> str:
    plaintext = ""
    for c in ciphertext:
        if c.isalpha():
            if c.isupper():
                new_ascii = (ord(c) - 65 - 18) % 26 + 65
            else:
                new_ascii = (ord(c) - 97 - 18) % 26 + 97
            plaintext += chr(new_ascii)
        else:
            plaintext += c
    return rot5_decrypt(rot13_decrypt(ciphertext))

ROT47加解密算法

加密过程:

  1. 首先,将明文中的每个字符转换为其对应的ASCII码值。
  2. 对于每个可打印字符,将其ASCII码值加上47。
  3. 如果加上47后的ASCII码值超过了126,则从33开始重新计数。
  4. 将加密后的ASCII码值转换回字符形式。
  5. 最终输出加密后的密文。

解密过程:

  1. 首先,将密文中的每个字符转换为其对应的ASCII码值。
  2. 对于每个可打印字符,将其ASCII码值减去47。
  3. 如果减去47后的ASCII码值小于33,则从126开始重新计数。
  4. 将解密后的ASCII码值转换回字符形式。
  5. 最终输出解密后的明文。

加解密脚本

def rot47_encrypt(plaintext: str) -> str:
    ciphertext = ""
    for c in plaintext:
        if 33 <= ord(c) <= 126:
            ciphertext += chr((ord(c) - 33 + 47) % 94 + 33)
        else:
            ciphertext += c
    return ciphertext


def rot47_decrypt(ciphertext: str) -> str:
    plaintext = ""
    for c in ciphertext:
        if 33 <= ord(c) <= 126:
            plaintext += chr((ord(c) - 33 - 47) % 94 + 33)
        else:
            plaintext += c
    return plaintext
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bestkasscn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值