听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • 《Go语言实战》

  • 《Go Web编程》

  • 《算法精粹 经典计算机科学问题的Python实现》

    • 几个小问题

      • 斐波那契序列
      • 简单的压缩算法
      • 牢不可破的加密方法
        • 练习
      • 计算π
      • 汉诺塔
    • 搜索问题

    • 约束满足问题

    • 图问题

    • 遗传算法

    • k均值聚类

    • 简单的神经网络

    • 对抗搜索

    • 其他问题

  • 读书笔记
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 几个小问题
zfprotectors
2022-05-17
目录

牢不可破的加密方法

一次性密码本 是一种加密数据的方法,它将无意义的随机的假数据混入数据中,从而达到加密的作用。

一次性密码本的加密操作用到的假数据必须符合3个标准:

  • 假数据与原始数据长度相同,
  • 假数据真正随机
  • 假数据完全保密

具体的python实现如下:

from secrets import token_bytes
from typing import Tuple


def random_key(length: int) -> int:
    tb: bytes = token_bytes(length)
    return int.from_bytes(tb, "big")


def encrypt(original: str) -> Tuple[int, int]:
    original_bytes: bytes = original.encode()
    dummy: int = random_key(len(original_bytes))
    original_key: int = int.from_bytes(original_bytes, "big")
    encrypted: int = original_key ^ dummy
    return dummy, encrypted


def decrypt(key1: int, key2: int) -> str:
    decrypted: int = key1 ^ key2
    temp: bytes = decrypted.to_bytes((decrypted.bit_length() + 7) //8, "big")
    return temp.decode()


if __name__ == "__main__":
    key1, key2 = encrypt("One Time Pad!")
    result: str = decrypt(key1, key2)
    print(result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 练习

用一次性密码本方案加密并解密图像

首先,理解一下思路,就是需要将图像先转换成base64(bytes)的形式进行存储,这样方便进行转换。 之后就可以按照一次性密码本的实现。

import base64

from secrets import token_bytes
from typing import Tuple


def random_key(length: int) -> int:
    tb: bytes = token_bytes(length)
    return int.from_bytes(tb, "big")


def encrypt(original_bytes: bytes) -> Tuple[int, int]:
    dummy: int = random_key(len(original_bytes))
    original_key: int = int.from_bytes(original_bytes, "big")
    encrypted: int = original_key ^ dummy
    return dummy, encrypted


def decrypt(key1: int, key2: int) -> bytes:
    decrypted: int = key1 ^ key2
    temp: bytes = decrypted.to_bytes((decrypted.bit_length() + 7) //8, "big")
    return temp


if __name__ == "__main__":
    img = r'feng.png'

    # 图片转base64
    with open(img, 'rb') as f:
        image_base64 = base64.b64encode(f.read())
    print(image_base64)
    key1, key2 = encrypt(image_base64)

    print('key1',key1)
    print('key2',key2)
    result: bytes = decrypt(key1, key2)
    print(result)

    # 解码图片
    imgdata = base64.b64decode(result)
    # # 将图片保存为文件
    with open("temp.jpg", 'wb') as f:
        f.write(imgdata)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
编辑 (opens new window)
#Python#算法
上次更新: 2022/05/25, 16:53:28
简单的压缩算法
计算π

← 简单的压缩算法 计算π→

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式