培根密码是一种非常好的密码,它以5位表示形式将字母A-Z映射到0-25中的数字(例如A = 00000Z=11001等)。 here是一个很好的描述。

作为其中的一部分,我想解密以下用培根密码加密的text。基本上,如果它是一个大写字母,那么我会考虑使用1,如果它是一个较低的字母,请考虑0。然后通过分成5位组,我们得到密文的解密。例如,前5个字母CrYPt转换为10110,后者转换为W

我在file.txt中插入了文本:

CrYPtogRapHY iS a ScIEnce Of "seCrET wriTinG". FOr aT Least Two
THoUsAnd yeaRS ThErE haVE bEeN peOPlE WHo WAnTeD to
SEnd MESsaGes WHiCh coUlD oNLY bEeN reAd bY tHe pEOplE FOR
WhOm thEy were INtEndeD. a lOT oF different MEtHODs For
CONceaLiNG MESSAgeS weRE InvENted startINg WiTh ancient
cIPhERS lIke "skYtAle" AnD "AtbasH" AND EndINg WITh mOdERn
SYMmetRIc And puBLiC key eNCryptiON algOriThMS SuCH AS aES
AnD rSa. THe deVelOPMeNt oF CRyPTOgrAPhY coNtiNUeS aND
neveR SToPs! DecRYpT the MessAGe ThAt iS hIDDeN iN tHE TeXt of
This Task! thE alPhaBeT For thE mESsagE cOnsIsTS of alL TWENty
SIx ENGliSh letTErs fROM "a" tO "Z" and sIx puNCtUAtiON MARkS "
", ".", ",", "!", "?", "'"


这是我的python代码:

import re

tr = {26: ' ', 27: '.', 28: ',', 29: '!', 30: '?', 31: '\''}


def transform(binary_ch):
    v = int(binary_ch, 2)
    if v < 26:
        return chr(65 + v)
    return tr[v]


# read the ciphertext from file
crypto_text = open("file.txt").read()

# remove the unnecessary characters (e.g. punctuation marks)
crypto_text = ''.join(ch for ch in crypto_text if ch.isalpha())

# convert to 1 the upper letter and to 0 the lower letter
crypto_text = ''.join('1' if ch.isupper() else '0' for ch in crypto_text)

# split in blocks of 5 characters
crypto_text = re.findall('.'*5, crypto_text)

plain_text = ''.join(transform(ch) for ch in crypto_text)

print crypto_text
print plain_text


但是平原并不是人们所期望的。我得到这个:

WE[WELCOME[QOU[TO[THE[FOUSTH[EDITIAN[OFSOMANIAN[CSYPTOLOGQ[DAYS^^[WE^HONE[YOU[ENJOV[EIESYLCSYPTOIMOMENT^

我认为应该是这样的:

We welcome you to the fourth edition of romanian cryptology days etc..

另外,我不确定它是否打算比此更具可读性,但是我还考虑到我在代码中的某个地方犯了一个错误。也可以认为它是捕获标志的东西。

我在代码中犯了任何错误吗?

编辑:

如果密文错误,则正确的版本是

CrYPtogRapHY iS a ScIEnce Of "seCrET wriTinG". FOr aT Least Two
THoUsANd yeaRS ThErE haVE bEeN peOPlE WHo WAnTeD to
SEnd MESsaGes WHiCh coUlD oNLY bEeN reAd by THe pEOplE FOR
WhOm thEy were INtEndeD. a lOT oF diffEREnt MEtHODs For
CONceaLiNG MeSsAges WeRE InvENted startINg WiTh ancient
cIPhERS lIke "skYtAle" aND "AtbasH" AND EndINg WITh mOdERn
SYMmetRIc ANd puBLiC key eNCryptiON algOriThMS SuCH AS aES
AnD rSa. THe deVelOPmEnt oF CRyPTOgrAPHY coNtiNUeS aND
neveR SToPs! DecRYpT the MessAGe ThAt iS hIDDeN In thE TeXt of
ThiS tAsK! thE alPhabET For tHE mEssagE cOnsiSTS of alL TWENty
SIx ENGlISh LetTErs fROM "a" tO "Z" and sIx puNCtUAtiON MARkS "
", ".", ",", "!", "?", "'"


这给了我们简单的价值:

WE WELCOME YOU TO THE FOURTH EDITION OF ROMANIAN CRYPTOLOGY DAYS!! WE HOPE YOU ENJOY EVERY CRYPTO MOMENT!

最佳答案

我写这些代码时没有看你的代码,我得到的结果基本相同。因此您的代码还可以,密文本身必须包含错误……或者其中嵌入了另一层消息。 ;)

我已经稍微重新排列了解码字母,以适应某些错误。

from string import ascii_letters

src = '''\
CrYPtogRapHY iS a ScIEnce Of "seCrET wriTinG". FOr aT Least Two
THoUsAnd yeaRS ThErE haVE bEeN peOPlE WHo WAnTeD to
SEnd MESsaGes WHiCh coUlD oNLY bEeN reAd bY tHe pEOplE FOR
WhOm thEy were INtEndeD. a lOT oF different MEtHODs For
CONceaLiNG MESSAgeS weRE InvENted startINg WiTh ancient
cIPhERS lIke "skYtAle" AnD "AtbasH" AND EndINg WITh mOdERn
SYMmetRIc And puBLiC key eNCryptiON algOriThMS SuCH AS aES
AnD rSa. THe deVelOPMeNt oF CRyPTOgrAPhY coNtiNUeS aND
neveR SToPs! DecRYpT the MessAGe ThAt iS hIDDeN iN tHE TeXt of
This Task! thE alPhaBeT For thE mESsagE cOnsIsTS of alL TWENty
SIx ENGliSh letTErs fROM "a" tO "Z" and sIx puNCtUAtiON MARkS "
", ".", ",", "!", "?", "'"'''

letterset = set(ascii_letters)
#alpha = 'abcdefghijklmnopqrstuvwxyz' + " .,!?'"
alpha = 'abcdefghijklmnopysrtuvwxyz' + " .,!?'"

bits = ('01'[c.isupper()] for c in src if c in letterset)
nums = [int(''.join(u), 2) for u in zip(*[iter(bits)]*5)]

plain = ''.join([alpha[u] for u in nums])
print(plain)


输出

we welcome you to the fourth editian of'romanian cryptology dayr!! we!hone you enjov eierylcryptoimoment!


也许这里有一个解释:

4th Edition of Romanian Cryptology Days Conference
RCD-2017
2017年9月18-20日

08-26 19:25
查看更多