Python3.7实现RC4加密解密(超详细)

作者: 大智大智 | 来源:发表于2019-04-23 11:11 被阅读14次
在密码学中,RC4(来自Rivest Cipher 4的缩写)是一种流加密算法,密钥长度可变。它加解密使用相同的密钥,因此也属于对称加密算法。RC4是有线等效加密(WEP)中采用的加密算法,也曾经是TLS可采用的算法之一。本文直接给出加密解密代码,看图,看注释!

运行结果:


捕获.PNG

加密源码:

import base64
def rc4_main(key = "init_key", message = "init_message"):
    # print("RC4加密主函数")
    s_box = rc4_init_sbox(key)
    crypt = str(rc4_excrypt(message, s_box))
    return  crypt
def rc4_init_sbox(key):
    s_box = list(range(256))  # 我这里没管秘钥小于256的情况,小于256不断重复填充即可
    # print("原来的 s 盒:%s" % s_box)
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    # print("混乱后的 s 盒:%s"% s_box)
    return s_box
def rc4_excrypt(plain, box):
    # print("调用加密程序成功。")
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    # print("res用于加密字符串,加密后是:%res" %res)
    cipher = "".join(res)
    # print("加密后的字符串是:%s" %cipher)
    # print("加密后的输出(经过编码):")
    # print(str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
    return (str(base64.b64encode(cipher.encode('utf-8')), 'utf-8'))
# rc4_main("123456sh","123456sh")

解密源码:

import base64
def rc4_main(key = "init_key", message = "init_message"):
    # print("RC4解密主函数调用成功")
    s_box = rc4_init_sbox(key)
    crypt = rc4_excrypt(message, s_box)
    return crypt
def rc4_init_sbox(key):
    s_box = list(range(256))  # 我这里没管秘钥小于256的情况,小于256不断重复填充即可
    # print("原来的 s 盒:%s" % s_box)
    j = 0
    for i in range(256):
        j = (j + s_box[i] + ord(key[i % len(key)])) % 256
        s_box[i], s_box[j] = s_box[j], s_box[i]
    # print("混乱后的 s 盒:%s"% s_box)
    return s_box
def rc4_excrypt(plain, box):
    # print("调用解密程序成功。")
    plain = base64.b64decode(plain.encode('utf-8'))
    plain = bytes.decode(plain)
    res = []
    i = j = 0
    for s in plain:
        i = (i + 1) % 256
        j = (j + box[i]) % 256
        box[i], box[j] = box[j], box[i]
        t = (box[i] + box[j]) % 256
        k = box[t]
        res.append(chr(ord(s) ^ k))
    # print("res用于解密字符串,解密后是:%res" %res)
    cipher = "".join(res)
    # print("解密后的字符串是:%s" %cipher)
    # print("解密后的输出(没经过任何编码):")
    return  cipher
# rc4_main("123456sh", "ABHCum92PMOXwqI=")

相关文章

  • Python3.7实现RC4加密解密(超详细)

    在密码学中,RC4(来自Rivest Cipher 4的缩写)是一种流加密算法,密钥长度可变。它加解密使用相同的密...

  • OPENSSL:RC4用法举例

    RC4是密钥流加密算法,明文长度和密文长度相同,加密过程和解密过程也相同。RC4加解密[https://www.j...

  • 加密解密

    加密解密 algorithm: 算法有很多,常用的里面,aes-256-cbc,rc4加密解密速度比较快key: ...

  • RC4加解密

    RC4算法rc4是流式加密算法,加密和解密都是按字节逐个处理。设明文是in、密文是out、密钥流是s,对于加密,o...

  • golang中crypto/rc4包

    rc4包实现了RC4加密算法,参见Bruce Schneier's Applied Cryptography。 t...

  • Rc4的加密和解密

    Rc4: 在密码学中,RC4(来自Rivest Cipher 4的缩写)是一种流加密算法,密钥长度可变。它加解密使...

  • ios 加密

    1、常见加密算法 : 对称加密:AES、DES、3DES、RC4 优点:加解密速度快 没有长度限制 缺点:密钥容易...

  • iOS rc4加密解密

    公司要求 rc4 加密解密,在网上自己找了好久,没找到一个能用的,只能自己写了一个,放出来供大家参考.好用求星ht...

  • Java 使用Cipher类实现加密

    ~~ 一、先看一个简单加密,解密实现 ~~1.1 加密 1.2 解密 1.3 代码解释上面简单实现了AES("AE...

  • Java 使用Cipher类实现加密

    ~~ 一、先看一个简单加密,解密实现 ~~1.1 加密 1.2 解密 1.3 代码解释上面简单实现了AES("AE...

网友评论

    本文标题:Python3.7实现RC4加密解密(超详细)

    本文链接:https://www.haomeiwen.com/subject/heomgqtx.html