培根密码解密

时间:2017-10-21 14:43:31

标签: python encryption

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

作为其中的一部分,我想解密使用Bacon Cipher加密的以下text。基本上,如果它是一个大写字母,我会考虑它10,如果它是较低的一个。然后通过拆分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!

1 个答案:

答案 0 :(得分:2)

我在不看你的情况下编写了这段代码,并得到了大致相同的结果。所以你的代码没问题,密文本身必须包含错误......或者其中嵌入了另一层消息。 ;)

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

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日