仿射密码加解密(python实现)

1. 简介

仿射密码是一种替换密码。它是利用加密函数一个字母对一个字母的加密。

加密函数是E(x)= (ax + b) (mod m),其中,a和m互质,m是字符集的大小。
(例如,26即是以26个字母作为编码,当m是26时,a必须是1,3,5,7,9,11,15,17,19,21,23,25其中之一)

解密函数为D(x) = a-1(x - b) (mod m),其中a-1是a在Zm群的乘法逆元。

2. 乘法逆元

所谓乘法逆元,定义如下:

群G中任意一个元素a,都在G中有唯一的逆元a`,具有性质aa` = a`a = e,其中e为群的单位元。

例如:4 * 2 ≡ 1 (mod 7)中,2即是4的乘法逆元。

要求乘法逆元,在字符集大小不大的情况下,完全可以通过遍历实现。

另外也可以通过gmpy2库中的invert函数实现。

在密码学中,乘法逆元一般是通过拓展欧几里得算法求得的。

3. 代码

m = 26,字符集为小写字母时

'''
仿射密码
(a,b)
m = 26,字符集为小写字母
加密函数是E(x)= (ax + b) (mod m)
解密函数为D(x) = (a^-1)(x - b) (mod m),其中a^-1是a的乘法逆元
'''

#通过一个简单的遍历得到a的乘法逆元,也可以通过gmpy2库中的invert函数实现
def get_inverse(a):
    for i in range(1,27):
        if a*i%26==1:
            return i

#加密
def encipher(a, b, p):
    c=[]
    for i in p:
        temp=((ord(i)-97)*a+b)%26+97
        c.append(chr(temp))
    print(''.join(c))

#解密
def decipher(a, b, c):
    a_inv = get_inverse(a)
    p=[]
    for i in c:
        temp=(((ord(i)-97)-b)*a_inv)%26+97
        p.append(chr(temp))
    print(''.join(p))

if __name__ == "__main__":
    a = 11
    b = 6
    message = 'sorcery'
    encipher(a,b,message)
    #decipher(a,b,message)

m = 52,字符集为小写和大写字母时

'''
仿射密码
m = 52
字符集为小写和大写字母
'''
import string
def encrypt(k1,k2,message):
    dic = string.ascii_letters
    c = []
    for i in message:
        if i.islower():
            num = ord(i)-ord('a')
            c.append(dic[(num*k1+k2)%52])
        elif i.isupper():
            num = ord(i)-ord('A')+26
            c.append(dic[(num*k1+k2)%52])
        else:
            c.append(i)
    print(''.join(c))

def decrypt(k1,k2,message):
    for i in range(52):
        if k1*i%52==1:
            inv = i
            break
    dic = string.ascii_letters
    m = []
    for i in message:
        if i.islower():
            num = ord(i)-ord('a')
            m.append(dic[inv*(num-k2)%52])
        elif i.isupper():
            num = ord(i)-ord('A')+26
            m.append(dic[inv*(num-k2)%52])
        else:
            m.append(i)
    print(''.join(m))

message = 'gVEXGT iDIT' #待加密或解密的消息
a = 5 # key的范围0~51之间
b = 29 # key的范围0~51之间
# encrypt(a,b,message)
decrypt(a,b,message)
  • 2
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 程序,用于实现仿密码密和解密: ```python # 定义密函数 def encrypt(plaintext, a, b): ciphertext = "" for c in plaintext: if c.isalpha(): # 将字符转换为 ASCII 码 ascii_code = ord(c) # 判断字符是否为大写字母 if c.isupper(): # 密公式:ciphertext = (a * ascii_code + b) % 26 ascii_code = ((a * (ascii_code - 65)) + b) % 26 + 65 # 判断字符是否为小写字母 elif c.islower(): # 密公式:ciphertext = (a * ascii_code + b) % 26 ascii_code = ((a * (ascii_code - 97)) + b) % 26 + 97 # 将 ASCII 码转换为字符 ciphertext += chr(ascii_code) else: ciphertext += c return ciphertext # 定义解密函数 def decrypt(ciphertext, a, b): plaintext = "" # 计算 a 的逆元 a_inverse = 0 for i in range(26): if (a * i) % 26 == 1: a_inverse = i break for c in ciphertext: if c.isalpha(): # 将字符转换为 ASCII 码 ascii_code = ord(c) # 判断字符是否为大写字母 if c.isupper(): # 解密公式:plaintext = (a_inverse * (ascii_code - b - 65)) % 26 + 65 ascii_code = (a_inverse * (ascii_code - b - 65)) % 26 + 65 # 判断字符是否为小写字母 elif c.islower(): # 解密公式:plaintext = (a_inverse * (ascii_code - b - 97)) % 26 + 97 ascii_code = (a_inverse * (ascii_code - b - 97)) % 26 + 97 # 将 ASCII 码转换为字符 plaintext += chr(ascii_code) else: plaintext += c return plaintext # 测试密和解密函数 plaintext = "Hello, world!" a = 5 b = 8 ciphertext = encrypt(plaintext, a, b) print(ciphertext) plaintext = decrypt(ciphertext, a, b) print(plaintext) ``` 输出结果为: ``` Czggj, htycr! Hello, world! ``` 其中,`a` 和 `b` 分别为仿密码的两个参数,`encrypt()` 函数用于密,`decrypt()` 函数用于解密。需要注意的是,仿密码密和解密涉及到取模运算和求逆元,所以需要对输入参数进行判断和计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值