Python中String, Bytes, Hex, Base64之间的关系与转换方法详解

⭐本专栏主要用python实现密码学中的常用经典算法,例如Vigenere、3DES、RSA、ElGamal、Diffie-Hellman、RSA签名、ElGamal签名、HMAC、哈希算法、列移位、AES等等。
🔥文章和代码已归档至【Github仓库:cryptography-codebase】,需要的朋友们自取。或者公众号【AIShareLab】回复 密码学 也可获取。

Program : Type Hint, String, Bytes, Hex, Base64

In this program, you are required to learn basic concepts of Python 3.

Type hints is a feature to specify the type of a variable, which is useful for write correct codes. In all lab assignments, you are required to write Python 3 code with type hints feature. Recall that you are required to use at least Python 3.10, otherwise you might suffer from issues brings by type hints as PEP 563 has not become the default option until Python 3.10.

Your programs does the following:

  • Read a byte array from the console input, where the byte array is expressed as a hex string (str). The console input is:

    deadbeef
    
  • Decode the hex string as the byte array (bytes)

  • Print each byte in the byte array as a decimal integer, with a space as the separator, i.e.:

    def output_bytes(in_bytes:bytes):
        for ch in in_bytes:
            print(ch, end=' ')
        print()
    
  • Print each byte in the byte array as a hexadecimal integer, with a space as the separator

  • Encode the byte array as a Base64 string(str), and output the string

  • Read another byte array from the console input, where the byte array is expressed as a hex string (str). The console input is:

    6465616462656566
    
  • Decode the hex string as the byte array (bytes)

  • As the decoded byte array happens to be a UTF-8 (or, ASCII) encoded bytes, decode the byte array to the text string(str):

    def decode_utf8(in_bytes:bytes)->str:
        return in_bytes.decode('utf-8')
    
  • Print the decoded text string

In your readme.pdf file, apart from the general information, it should include:

  • A figure representing the relationship between all the variables in your program with type bytes and str. Example here:

Python中String, Bytes, Hex, Base64之间的关系与转换方法详解

The figure above is corresponding to the following code.

first_hex:str = input()
first_bytes:bytes = bytes.fromhex(first_hex)

solution code

import base64


def output_bytes(in_bytes: bytes):
    for ch in in_bytes:
        print(ch, end=' ')
    print()


def output_hex(in_bytes: bytes):
    for ch in in_bytes:
        print(hex(ch), end=' ')
    print()


def decode_utf8(in_bytes: bytes) -> str:
    return in_bytes.decode('utf-8')


print("Enter a string str1:")
str1: str = input()
byte_array: bytes = bytearray.fromhex(str1)
output_bytes(byte_array)
output_hex(byte_array)
encoded: bytes = base64.b64encode(byte_array)
print(encoded)
print("Enter a string str2:")
str2: str = input()
byte_array2: bytes = bytearray.fromhex(str2)
str3: str = decode_utf8(byte_array2)
print(str3)
  • output
Enter a string str1:
deadbeef
222 173 190 239 
0xde 0xad 0xbe 0xef 
b'3q2+7w=='
Enter a string str2:
4445414442454546
DEADBEEF

进程已结束,退出代码为 0
  • operating system version:WIN10
  • CPU instruction set:x64
  • Python interpreter version:Python3.9
    A screenshot of the console output of the program

在这里插入图片描述

A figure representing the relationship between all the variables in your program
with type bytes and str:
在这里插入图片描述

初学密码学,如果发现错误,还请各位指正。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年3月5日 下午4:18
下一篇 2023年3月5日 下午4:19

相关推荐