【发布时间】:2021-01-14 16:08:52
【问题描述】:
我有一个问题需要帮助。 通常,在加密文件中,文件大小比未加密文件大。在这些时候熵会减少吗?我知道在 python 中熵的计算如下:
print('myfile.text'.format)
with open(r"C:\Users\Parisa\Desktop\myfile.txt", 'rb') as f:
byteArr = list(f.read())
fileSize = len(byteArr)
print
print('File size in bytes: {:,d}'.format(fileSize))
# calculate the frequency of each byte value in the file
print('Calculating Shannon entropy of file. Please wait...')
freqList = []
for b in range(256):
ctr = 0
for byte in byteArr:
if byte == b:
ctr += 1
freqList.append(float(ctr) / fileSize)
# Shannon entropy
ent = 0.0
for freq in freqList:
if freq > 0:
ent = ent + freq * math.log(freq, 2)
ent = -ent
print('Shannon entropy: {}'.format(ent))[![enter image description here][1]][1]
stack.imgur.com/jMhkc.png 这是加密后的
这是在加密之前
【问题讨论】: