Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Crypto Lab 1 

Task 0: Install python (v3), python IDE, python cryptography

I have downloaded anaconda and use jupyter to run python file.

 

Install the python cryptography module.

 

Task 1: Read/Write Files in Python For this task you are going to get familiar with the File I/O in python.

Task 1.1: Using buffered Byte Streams

The following code shows reading data from one file and writing data to another file. Familiarize yourself with the code (lookup the functions in the python documentation to understand what they do).

# “import math” call function

import os

 import io    

# fname: source file name  fname2: destination file

 fname = 'infile.txt'

 fname2 = 'outfile.txt'

# os.path.abspath() is the Python function for getting an absolute path. It takes one argument, the relative path to be converted to an absolute path, and returns the absolute path of that relative path.

path = os.path.abspath(fname)

path2 = os.path.abspath(fname2)       

# print message in terminal to user

 print('copying ', path, 'to ', path2)

# set the blocksize  blocksize: size of blocks in bytes

 blocksize = 16

# set the totalsize counter

totalsize = 0

# create a mutable array to hold the bytes

data = bytearray(blocksize)

# open the files, in buffered binary mode

file = open(fname, 'rb')

file2 = open(fname2, 'wb')

# loop while function until satisfy the condition in if, the loop will done

while True:

# read block from source file

num = file.readinto(data)

 # adjust totalsize

totalsize += num

 # print data, assuming text data

print(num,data)

 # use following if raw binary data

# print(num,data.hex())

# check if full block read

if num == blocksize:

# write full block to destination

file2.write(data)

else:

# extract subarray

data2 = data[0:num]

# write subarray to destination and break loop

file2.write(data2) break

# close files (note will also flush destination file

file.close() file2.close()

# print totalsize

print('read ',totalsize,' bytes')

Now:

1. Create a python file for this code in your IDE

2. Copy the code to your file

3. Create a file called infile.txt and enter some text into it.

4. Run the program and ensure that the data is copied to the file outfile.txt

I have checked the path and name of the file with no problem and have tried the specified path method. I still get an error that the file cannot be found.

Task 1.2: Using Character Streams

 If the underlying data is character based, then you can connect the stream to a character based stream and read and write as strings instead. Create a new version of the program that reads text data and copies it to another file.

with open(path, 'rb') as f:

    datas = f.read()

    start_char = datas.find(b'Start')

    # start_char2 = datas.find(b'Start', start_char)

    end_char = datas.find(b'End', start_char)

    # end_char2 = datas.find(b'End', start_char2)

    data = datas[start_char:end_char]

    print(data)