Skip to content

Commit

Permalink
Merge pull request jtemporal#27 from jcemelanda/VigenereImplementation
Browse files Browse the repository at this point in the history
Cifra Vigenère
  • Loading branch information
jtemporal authored Sep 25, 2019
2 parents 2995850 + e6afbc9 commit f8f22e8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 91 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ pip install caipyra
## Usage

```
>>> from caipyra import this
>>> from caipyra import this # Abre o texto paginado
Caipyra por Humberto Diógenes
Continue (Y/n)? y
Na minha terra é matuto
Que é quem veio do mato
Ou beradêro, do rio
mermo na beira
Na do Andrews é capiau
Mas aqui é caipira
Continue (Y/n)? n
Embarquei nessa viagem
A convite do "seocam"
O Serginho da Cameron
:
>>> this.link() # abre a apresentação do Import Caipyra no youtube
Expand Down
158 changes: 71 additions & 87 deletions caipyra/this.py
Original file line number Diff line number Diff line change
@@ -1,92 +1,76 @@
# coding: utf-8
import sys
import webbrowser

s = u"""Pnvclen cbe Uhzoregb Qvótrarf
An zvaun green é znghgb
Dhr é dhrz irvb qb zngb
Bh orenqêeb, qb evb
zrezb an orven
An qb Naqerjf é pncvnh
Znf ndhv é pnvcven
Rzonedhrv arffn ivntrz
N pbaivgr qb "frbpnz"
B Fretvaub qn Pnzreba
Oben crtne rffr obaqr?
Sreanaqb qvffr dhr
cbe ryr gá Znffn!
Znepb, "Pnqê Ebtê?"
Svpbh an tenawn pbz Cnhyn
R Wrffvpn aãb cbqr ive
Svpbh cerfn ab Grzcbeny
Niryvab sbv ceb Fnzon
Pbz Yrgípvn qn Cbegryyn
Cnffnenz ab One Frzragr
Arz pbaivqnenz n tragr
Qrfpbafvqrençãb gbgny
N Znev ynetbh Frbpnz
R sbv rzoben pbz b Sbsãb
Frz fnore fr vn qne pregb
Naqerjf svpbh Zrqvab
R npnobh sbv qrfvfgvab
Uhzoregb, ibpé é qr baqr?
Rh fbh Qvótrarf
Znf zvaun pvqnqr é Angny
B Encun qb bhgeb Evb
Dhnfr ongrh zvaun pnegrven
Znf rh aãb znedhrv oborven
R nvaqn npurv b yrx yrtny
B riragb Fra Enznyub
Aãb é n zrfzn pbvfn
R rh gnzoéz fragv snygn
Qndhryr pbzchgnqbe
B zryube dhr wá iv
Npub dhr ren hz Qéb
Fó aãb gvaun n shaçãb fyrrc
Qr abvgr ivenin grgéh
Zrh nzvtb, ahapn iv
Hzn cerfrcnqn gãb tenaqr
Crafr ahzn neehznçãb
Gvaun Bcra One qr cnçbpn
Pbz qvervgb n dhragãb
R dhnaqb ivrerz gr snyne
Qr dhnydhre grpabybtvn
Bh yvathntrz qr cebtenznçãb
Crethagr nb pnoen, an yngn:
– Arffn ghn pbasreêapvn
Ibpêf iãb ceb one
Rapure n pnen
An Pneergn Shenpãb?"""

d = {}
for c in (65, 97):
for i in range(26):
d[chr(i + c)] = chr((i + 13) % 26 + c)

s_list = s.split('\n\n')

for index, verse in enumerate(s_list):
print("\n" + "".join([d.get(c, c) for c in verse]))
if index < len(s_list) - 1:

# Checks Python major version to accepting data from user
if sys.version_info.major == 2:
next = raw_input('\nContinue (Y/n)? ')
else:
next = input('\nContinue (Y/n)? ')

if next.lower() == 'n':
break
import pydoc
from vigenere import decrypt

s = u"""Rybwmep ivf Wsfiseim Kwóvcglg
Lt avcft hrgpt é bymbhb
Onl é onla kcbv qd fhhb
Mn prgywêfb, wv exm
tsebm uo qcbyo
Cy kc Plwysjh é qnegtb
Zpq hehx é qnxnbyo
Cfioefsxp atqlh ixyzla
P vvbixrx rb "llcppk"
C Hcknwawm ko Ryflfbc
Icep ilung xzgr zhurr?
Ylfaplwv qxqll djc
wce cel gá Fhgfp!
Toerm, "Ppbê Fbvê?"
Sxahb ap zyoayy jcz Ntbzn
C Qsfhgvh aãm wcqt opf
Ugvvi epxzo cm Aszemkhz
Yolzvcm mcv nkv Fpkuh
Pdk Ssgíabh qp Ivfgtjeh
Cpqlhfnb gv Opp Zsztlml
Atk jcakgwhfnb t urcrx
Rrhahugvsckhçãd mvhna
H Zppb zngehb Ftmvha
T yvw tkuvfn aht b Dhmãb
Qxt fpzxy ft bh qpp jseim
Hbqgcpz sxahb Ztbbuc
T tjoods mcv bxzwfiggv
Wsfiseim, jbré é qt hurr?
Xb fds Kwóvcglg
Byl avcft qvsywl é Ltaoy
M Yocwy kc dsmyc Ggh
Ehpqx pnicn avcft qngrxpfn
Ktz rj gãc bykxirx uvprxpt
S pggko paalw d ely aczhz
M ljrcrh Grc Khanafh
Bãd é o bclto rmbzo
T xb gpkuéa hcgaw uyeao
Syjbsyt vvacjrtkce
M tsywmk eht cá ix
Tjvb onl rgy ba Séh
Gó lãv gxlah n dnuçãd lssre
Wl adgml ixptco icméi
Kxb nbgzv, csgjo kg
Ban nklgreywh gãm nfncbx
Drcqx bhby hfejktçãb
Rbuvn Milb Qyk rr ntçcpp
Vva sgklwgd t ehtlmãc
C xincbh jvtpxt gt yhzng
Wl djyexirg mlqadjhnwn
Mn zvcenhurb wl cgmzyozpçãv
Ctpzbbgt tv ppzkh, cy sogp:
– Cclzo ist qbcdxyêargt
Jbrêl jãd iyc qyk
Sarfxy n atyo
Cy Joegcmh Sjptjãb?"""


pydoc.pager(decrypt('python', s))


def link():
Expand Down
35 changes: 35 additions & 0 deletions caipyra/vigenere.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Code inspired by
# http://programeveryday.com/post/implementing-a-basic-vigenere-cipher-in-python/

# version with encrypter
# https://gist.github.com/jcemelanda/cba27615f50b2c80cbcd5fc1188d4f43

from itertools import cycle
from functools import reduce

ALPHA_LOW = 'abcdefghijklmnopqrstuvwxyz'
ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


def decrypt(key, ciphertext):
"""Decrypt the string and return the plaintext"""
pairs = zip(ciphertext, cycle(key))
result = ''

for pair in pairs:
if pair[0] in ALPHA_LOW:
total = reduce(
lambda x, y: ALPHA_LOW.index(x) - ALPHA_LOW.index(y),
pair
)
result += ALPHA_LOW[total % 26]
elif pair[0] in ALPHA_UPPER:
total = reduce(
lambda x, y: ALPHA_UPPER.index(x) - ALPHA_LOW.index(y),
pair
)
result += ALPHA_UPPER[total % 26]
else:
result += pair[0]

return result

0 comments on commit f8f22e8

Please sign in to comment.