熵和加密文件

问题描述

我有一个问题需要帮助。 通常,在加密文件中,文件大小大于未加密文件在这些时候熵会减少吗?我知道在 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]

enter image description here

stack.imgur.com/jMhkc.png 这是加密后

enter image description here

这是在加密之前

解决方法

加密文件的熵最大化。熵是文件“随机性”的度量。

这是文本文件通常的外观,根据文件内容绘制熵:

enter image description here

这是加密文件的外观(忽略低熵的微小尖峰,它是由某些标题信息或等效信息引起的):

enter image description here

加密文件的熵接近 1。如果没有,它们就不会被很好地加密。模式 == 低熵,低熵 == 糟糕的加密。

,

你确实触及了一些困难的事情。让我引用过去的一些家伙:-

“我最关心的是如何称呼它。我想把它叫做“信息”,但这个词用得太多了,所以我决定把它叫做“不确定性”。当我与约翰·冯·诺依曼 (John von Neumann) 讨论时,他有了更好的主意。冯诺依曼告诉我:“你应该称之为熵,有两个原因。首先,您的不确定性函数已以该名称用于统计力学,因此它已经有了一个名称。其次,更重要的是,没有人知道熵到底是什么,所以在辩论中你总是占优势。”

-克劳德·香农。

正如所讨论的,elsewhere“熵”通常会在您加密源/纯文本时增加。我可能没有深入研究细节,但根据上面的引用,熵有不同的定义。尤其是在密码学方面。密文看起来会更复杂。


使用 here 中的 ent