Compare commits

...

10 Commits

Author SHA1 Message Date
c8aa211b6a Add poem 2023-10-02 18:37:15 +02:00
1da564e1ef Update project 2023-10-02 18:34:52 +02:00
8c8d17432f Add requirement 2023-09-28 22:00:07 +02:00
b115533bfa Add chart for IC 2023-09-28 21:50:14 +02:00
2e8bf0bd9e Upd requirements 2023-09-27 21:59:21 +02:00
50ff9dfb1c Update README and IC 2023-09-27 12:30:23 +02:00
34fda59784 Rename files 2022-07-09 16:09:22 +02:00
63d0dd32f1 update main file 2022-07-08 19:00:42 +02:00
f95807a90f Update scripts 2022-07-08 16:53:22 +02:00
61e8de0483 Add script ic 2022-07-06 21:52:44 +02:00
9 changed files with 339 additions and 194 deletions

5
.gitignore vendored Normal file

@ -0,0 +1,5 @@
__pycache__/**
**.txt
**.txt.**
!poem.txt.**
!poem.txt

@ -1,92 +1,17 @@
# Cryptanalysis
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
## Setup the environments
You may to create your virtualenv:
```
cd existing_repo
git remote add origin https://gitlab.com/gbucchino/cryptanalysis.git
git branch -M main
git push -uf origin main
$ virtualenv ~/venv/cryptanalysis
$ source ~/venv/cryptanalysis/bin/active
```
## Integrate with your tools
And you must install the packages for using the project:
- [ ] [Set up project integrations](https://gitlab.com/gbucchino/cryptanalysis/-/settings/integrations)
```
$ pip3 install -r requirements.txt
```
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
***
# Editing this README
When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
## Name
Choose a self-explaining name for your project.
## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
## Contributing
State if you are open to contributions and what your requirements are for accepting them.
For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
## License
For open source projects, say how it is licensed.
## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.

61
decryptCaesar.py Normal file

@ -0,0 +1,61 @@
# coding: utf-8
from argparse import ArgumentParser
import matplotlib.pyplot as plt
from functions import readFile
def checkArguments():
args = ArgumentParser(description='Cryptanalyst')
args.add_argument('-f', '--filename', help='Text to analyze')
return args.parse_args()
def decryptCaesarText(cipherFile):
"""
This function breach the caesar ciphertext with all key
"""
if cipherFile is None:
return
letterAToAscii = ord('A') # Get the ASCII code
letterZToAscii = ord('Z') # Get the ASCII code
letteraToAscii = ord('a') # Get the ASCII code
letterzToAscii = ord('z') # Get the ASCII code
for key in range (1, 27):
foo = []
for line in cipherFile:
newLine = str()
for letter in line:
letter = letter.upper()
letterAscii = ord(letter)
if letterAscii >= letterAToAscii and letterAscii <= letterZToAscii:
newLetter = getNewLetter(letterAscii, letterAToAscii, key)
elif letterAscii >= letteraToAscii and letterAscii <= letterzToAscii:
newLetter = getNewLetter(letteraToAscii, key)
else:
newLetter = letter
newLine = newLine + newLetter
foo.append(newLine)
print(f"key: {key}")
print(foo)
def getNewLetter(letterAscii, letterToAscii, key):
"""
This function return the new letter and we substract with the key.
The function return the new letter into a char
"""
letterAscii = letterAscii - letterToAscii
res = ((letterAscii - key) % 26) + letterToAscii
return chr(res)
if __name__ == "__main__":
args = checkArguments()
cipherFile = None
if args.filename:
cipherFile = readFile(args.filename)
decryptCaesarText(cipherFile)

25
functions.py Normal file

@ -0,0 +1,25 @@
# coding: utf-8
from re import sub
from unidecode import unidecode
def readFile(filename):
data = None
dataFile = str()
with open(filename, 'r') as f:
data = f.readlines()
for d in data:
u = unidecode(d.lower(), 'utf-8')
dataFile += sub(r'[^a-z]', '', u)
return dataFile
def countLetters(text):
# Get number of letters
totalLetters = 0
for letter in text:
totalLetters += 1
return totalLetters

53
ic.py Normal file

@ -0,0 +1,53 @@
# coding: utf-8
from argparse import ArgumentParser
from functions import readFile, countLetters
import matplotlib.pyplot as plt
def checkArguments():
args = ArgumentParser(description='Cryptanalyst')
args.add_argument('-f', '--filename', help='Text to analyze')
return args.parse_args()
def main():
args = checkArguments()
# Read file
dataFile = readFile(args.filename)
# Get number of letters
totalLetters = countLetters(dataFile)
counts = []
for i in range (0, 26):
counts.append(0)
# Count the number of letter in our text
letterToAscii = ord('a') # Get the ASCII code
for i in dataFile:
l = ord(i) - letterToAscii
if l >= 0 and l <= 26:
counts[l] += 1
# We calculate the sum of all character in the text
ic = 0
allIC = list()
for i in range(0, len(counts)):
sum = (counts[i] * (counts[i] - 1))
currentIC = sum / (totalLetters * (totalLetters - 1))
allIC.append(currentIC)
ic += currentIC
print(f"Index of coincidence: {ic}")
largeur = 0.5
y = list()
for alphabet in range(0, 26):
y.append(chr(ord('a') + alphabet))
plt.bar(y, allIC, largeur)
plt.show()
if __name__ == "__main__":
main()

109
main.py

@ -1,109 +0,0 @@
# coding: utf-8
from argparse import ArgumentParser
import matplotlib.pyplot as plt
from lorem import text as loremtext
def checkArguments():
args = ArgumentParser(description='Cryptanalyst')
args.add_argument('-f', '--filename', help='Text to analyze')
args.add_argument('-s', '--stats', help='Statistics', action='store_true')
args.add_argument('-d', '--decrypt', help='Decrypt Caesar text', action='store_true')
return args.parse_args()
def readCipherText(filename):
"""
This read the file passed in argument and return the data of it
"""
file = []
with open(filename, 'r') as f:
data = f.readlines()
for entry in data:
file.append(entry.strip())
return file
def stats(file=None):
"""
This function get a stats of all letter occurrence in a text
"""
if file is None:
text = loremtext()
else:
text = file
lettersOccurence = {}
letterUpperToAscii = ord('A') # Get the ASCII code
for i in range (0, 26):
lettersOccurence[chr(i + letterUpperToAscii)] = 0
for line in text:
for letter in line:
letter = letter.upper()
if letter in lettersOccurence:
lettersOccurence[letter] = lettersOccurence[letter] + 1
x = [chr(letter + 65) for letter in range(0, 26)]
y = [lettersOccurence[tmp] for tmp in lettersOccurence]
largeur = 0.5
plt.bar(x, y, largeur)
plt.show()
def decryptCaesarText(cipherFile):
"""
This function breach the caesar ciphertext with all key
"""
if cipherFile is None:
return
letterAToAscii = ord('A') # Get the ASCII code
letterZToAscii = ord('Z') # Get the ASCII code
letteraToAscii = ord('a') # Get the ASCII code
letterzToAscii = ord('z') # Get the ASCII code
for key in range (0, 26):
foo = []
for line in cipherFile:
newLine = str()
for letter in line:
letter = letter.upper()
letterAscii = ord(letter)
if letterAscii >= letterAToAscii and letterAscii <= letterZToAscii:
newLetter = getNewLetter(letterAscii, letterAToAscii, key)
elif letterAscii >= letteraToAscii and letterAscii <= letterzToAscii:
newLetter = getNewLetter(letteraToAscii, key)
else:
newLetter = letter
newLine = newLine + newLetter
foo.append(newLine)
print(f"key: {key}")
print(foo)
def getNewLetter(letterAscii, letterToAscii, key):
"""
This function return the new letter and we substract with the key.
The function return the new letter into a char
"""
letterAscii = letterAscii - letterToAscii
res = ((letterAscii - key) % 26) + letterToAscii
return chr(res)
def main():
args = checkArguments()
cipherFile = None
if args.filename:
cipherFile = readCipherText(args.filename)
if args.stats:
stats(cipherFile)
if args.decrypt:
decryptCaesarText(cipherFile)
if __name__ == "__main__":
main()

139
poem.txt.crypt Normal file

@ -0,0 +1,139 @@
Ô WEMRX TVêXVI ! KVERHI âQI ! SL ! NI XSQFI à KIRSYB !
NIYRI, MP EZEMX IRGSV HI PSRKW NSYVW TEVQM RSYW,
MP R'IR E TEW GSQTXé PI RSQFVI ;
MP éXEMX à GIX âKI Sù PI FSRLIYV JPIYVMX ;
MP E GSRWMHéVé PE GVSMB HI NéWYW-GLVMWX
XSYXI VECSRRERXI HERW P'SQFVI.
MP E HMX : — « G'IWX PI HMIY HI TVSKVèW IX H'EQSYV.
NéWYW, UYM ZSMX XSR JVSRX GVSMX ZSMV PI JVSRX HY NSYV.
GLVMWX WSYVMX à UYM PI VITSYWWI.
TYMWUY'MP IWX QSVX TSYV RSYW, NI ZIYB QSYVMV TSYV PYM ;
HERW WSR XSQFIEY, HSRX N'EM PE TMIVVI TSYV ETTYM,
MP Q'ETTIPPI H'YRI ZSMB HSYGI.
« WE HSGXVMRI IWX PI GMIP IRXV'SYZIVX ; TEV PE QEMR,
GSQQI YR TèVI P'IRJERX, MP XMIRX PI KIRVI LYQEMR ;
TEV PYM RSYW ZMZSRW IX RSYW WSQQIW ;
EY GLIZIX HIW KIôPMIVW HSVQERX HERW PIYVW QEMWSRW,
MP HéVSFI PIW GPIJW HI XSYXIW PIW TVMWSRW
IX QIX IR PMFIVXé PIW LSQQIW.
« SV MP IWX, PSMR HI RSYW, YRI EYXVI LYQERMXé
UYM RI PI GSRREîX TSMRX, IX HERW P'MRMUYMXé
VEQTI IRGLEîRéI, IX WSYJJVI IX XSQFI ;
MPW JSRX TSYV XVSYZIV HMIY HI XéRéFVIYB IJJSVXW ;
MPW W'EKMXIRX IR ZEMR ; MPW WSRX GSQQI HIW QSVXW
UYM XâXIRX PI QYV HI PIYV XSQFI.
« WERW PSM, WERW FYX, WERW KYMHI, MPW IVVIRX MGM-FEW.
MPW WSRX QéGLERXW, éXERX MKRSVERXW ; MPW R'SRX TEW
PIYV TEVX HI PE KVERHI GSRUYêXI.
N'MVEM. TSYV PIW WEYZIV NI UYMXXI PI WEMRX PMIY.
Ô QIW JVèVIW, NI ZMIRW ZSYW ETTSVXIV QSR HMIY,
NI ZMIRW ZSYW ETTSVXIV QE XêXI ! » —
TVêXVI, MP W'IWX WSYZIRY, GEPQI IR RSW NSYVW XVSYFPéW,
HI PE TEVSPI HMXI EYB ETôXVIW : — EPPID,
FVEZID PIW FûGLIVW IX PIW GPEMIW ! —
IX HI P'EHMIY HY GLVMWX EY WYTVêQI QSQIRX :
— Ô ZMZERX, EMQID-ZSYW ! EMQID. IR ZSYW EMQERX,
JVèVIW, ZSYW JIVQIVID QIW TPEMIW. —
MP W'IWX HMX UY'MP IWX FSR H'éGPEMVIV HERW PIYV RYMX
GIW TIYTPIW éKEVéW PSMR HY TVSKVèW UYM PYMX,
HSRX P'âQI IWX GSYZIVXI HI ZSMPIW ;
TYMW MP W'IR IWX EPPé, HERW PIW ZIRXW, HERW PIW JPSXW,
ZIVW PIW RSMVW GLIZEPIXW IX PIW WERKPERXW FMPPSXW,
PIW CIYB JMBéW WYV PIW éXSMPIW.
MM.
GIYB ZIVW UYM GIX ETôXVI EPPEMX, P'SRX éKSVKé.
MMM.
SL ! XERHMW UYI Pà-FEW, LéPEW ! GLID GIW FEVFEVIW,
W'éXEPI P'éGLEJEYH HI XIW QIQFVIW GLEVKé,
UYI PI FSYVVIEY, VERKIERX WIW KPEMZIW IX WIW FEVVIW,
JVSXXI EY KMFIX WSR SRKPI Sù XSR WERK W'IWX JMKé ;
GMIP ! XERHMW UYI PIW GLMIRW HERW GI WERK ZMIRRIRX FSMVI,
IX UYI PE QSYGLI LSVVMFPI, IWWEMQ EY ZSP NSCIYB,
GSQQI HERW YRI VYGLI IRXVI IR XE FSYGLI RSMVI
IX FSYVHSRRI EY WSPIMP HERW PIW XVSYW HI XIW CIYB ;
XERHMW UY'éGLIZIPéI, IX WERW ZSMB, WERW TEYTMèVIW,
XE XêXI FPêQI IWX Pà WYV YR MRJâQI TMIY,
PMZVéI EYB ZMPW EJJVSRXW, QIYVXVMI à GSYTW HI TMIVVIW,
MGM, HIVVMèVI XSM, QEVXCV, SR ZIRH XSR HMIY !
GI HMIY UYM R'IWX UY'à XSM, QEVXCV, SR XI PI ZSPI !
SR PI PMZVI à QERHVMR, GI HMIY TSYV UYM XY QIYVW !
HIW LSQQIW, GSQQI XSM VIZêXYW HI P'éXSPI,
TSYV êXVI GEVHMREYB, TSYV êXVI WéREXIYVW,
HIW TVêXVIW, TSYV EZSMV HIW TEPEMW, HIW GEVVSWWIW,
IX HIW NEVHMRW P'éXé VMERX WSYW PI GMIP FPIY,
TSYV EVKIRXIV PIYV QMXVI IX TSYV HSVIV PIYVW GVSWWIW,
TSYV FSMVI HI FSR ZMR, EWWMW TVèW H'YR FSR JIY,
EY JSVFER HSRX PE QEMR HERW PI QIYVXVI IWX XVIQTéI,
EY PEVVSR GLEVKé H'SV UYM TECI IX UYM WSYVMX,
KVERH HMIY ! VIXSYVRI-XSM ZIVW RSYW, XêXI GSYTéI !
MPW ZIRHIRX NéWYW-GLVMWX ! MPW ZIRHIRX NéWYW-GLVMWX !
MPW PMZVIRX EY FERHMX, TSYV UYIPUYIW WEGW WSVHMHIW,
P'éZERKMPI, PE PSM, P'EYXIP éTSYZERXé,
IX PE NYWXMGI EYB CIYB WéZèVIW IX GERHMHIW,
IX P'éXSMPI HY GSIYV LYQEMR, PE ZéVMXé !
PIW FSRW NIXéW, ZMZERXW, EY FEKRI, SY QSVXW, EYB JPIYZIW,
P'LSQQI NYWXI TVSWGVMX TEV GEVXSYGLI WCPPE,
P'MRRSGIRX éKSVKé, PI HIYMP WEGVé HIW ZIYZIW,
PIW TPIYVW HI P'SVTLIPMR, MPW ZIRHIRX XSYX GIPE !
XSYX ! PE JSM, PI WIVQIRX UYI HMIY XMIRX WSYW WE KEVHI,
PI WEMRX XIQTPI Sù, QSYVERX, XY HMW :MRXVSïFS,
MPW PMZVIRX XSYX ! TYHIYV, ZIVXY ! — QEVXCV, VIKEVHI,
VSYZVI XIW CIYB UY'IQTPMX PE PYIYV HY XSQFIEY ; —
MPW ZIRHIRX P'EVGLI EYKYWXI Sù P'LSWXMI éXMRGIPPI !
MPW ZIRHIRX GLVMWX, XI HMW-NI ! IX WIW QIQFVIW PMéW !
MPW ZIRHIRX PE WYIYV UYM WYV WSR JVSRX VYMWWIPPI,
IX PIW GPSYW HI WIW QEMRW, IX PIW GPSYW HI WIW TMIHW !
MPW ZIRHIRX EY FVMKERH UYM GLID PYM PIW EXXMVI
PI KVERH GVYGMJMé WYV PIW LSQQIW TIRGLé ;
MPW ZIRHIRX WE TEVSPI, MPW ZIRHIRX WSR QEVXCVI,
IX XSR QEVXCVI à XSM TEV-HIWWYW PI QEVGLé !
XERX TSYV PIW GSYTW HI JSYIX UY'MP VIçYX à PE TSVXI !
GéWEV ! XERX TSYV P'EQIR, XERX TSYV P'EPPéPYME !
XERX TSYV PE TMIVVI Sù ZMRX LIYVXIV WE XêXI QSVXI !
XERX TSYV PI HVET VSYKM UYI WE FEVFI IWWYCE !
MPW ZIRHIRX WIW KIRSYB QIYVXVMW, WE TEPQI ZIVXI,
WE TPEMI EY JPERG, WSR SIMP XSYX FEMKRé H'MRJMRM,
WIW TPIYVW, WSR EKSRMI, IX WE FSYGLI IRXVSYZIVXI,
IX PI GVM UY'MP TSYWWE : PEQQE WEFEGXLERM !
MPW ZIRHIRX PI WéTYPGVI ! MPW ZIRHIRX PIW XéRèFVIW !
PIW WéVETLMRW GLERXERX EY WIYMP TVSJSRH HIW GMIYB,
IX PE QèVI HIFSYX WSYW P'EVFVI EYB FVEW JYRèFVIW,
UYM, WIRXERX Pà WSR JMPW, RI PIZEMX TEW PIW CIYB !
SYM, GIW éZêUYIW, SYM, GIW QEVGLERHW, SYM, GIW TVêXVIW
E P'LMWXVMSR HY GVMQI, EWWSYZM, GSYVSRRé,
E GI RéVSR VITY UYM VMX TEVQM PIW XVEîXVIW,
YR TMIH WYV XLVEWéEW, YR GSYHI WYV TLVCRé,
EY ZSPIYV UYM XYE PIW PSMW à GSYTW HI GVSWWI,
EY TMVEXI IQTIVIYV RETSPéSR HIVRMIV,
MZVI HIYB JSMW, MQQSRHI IRGSV TPYW UYI JéVSGI,
TSYVGIEY HERW PI GPSEUYI IX PSYT HERW PI GLEVRMIV,
MPW ZIRHIRX, ô QEVXCV, PI HMIY TIRWMJ IX TâPI
UYM, HIFSYX WYV PE XIVVI IX WSYW PI JMVQEQIRX,
XVMWXI IX RSYW WSYVMERX HERW RSXVI RYMX JEXEPI,
WYV PI RSMV KSPKSXLE WEMKRI éXIVRIPPIQIRX !

48
stats.py Normal file

@ -0,0 +1,48 @@
# coding: utf-8
from argparse import ArgumentParser
import matplotlib.pyplot as plt
from lorem import text as loremtext
from functions import readFile
def checkArguments():
args = ArgumentParser(description='Cryptanalyst')
args.add_argument('-f', '--filename', help='Text to analyze')
return args.parse_args()
def stats(file=None):
"""
This function get a stats of all letter occurrence in a text
"""
if file is None:
dataFile = loremtext()
else:
dataFile = file
lettersOccurence = []
# Init our tab
for i in range (0, 26):
lettersOccurence.append(0)
letterToAscii = ord('a') # Get the ASCII code
for letter in dataFile:
l = ord(letter) - letterToAscii
if l >= 0 and l <= 26:
lettersOccurence[l] += 1
x = [chr(letter + 65) for letter in range(0, 26)]
y = [tmp for tmp in lettersOccurence]
largeur = 0.5
plt.bar(x, y, largeur)
plt.show()
if __name__ == "__main__":
args = checkArguments()
if args.filename:
dataFile = readFile(args.filename)
stats(dataFile)

@ -1,2 +0,0 @@
JGNNQ YQTNF!
LG UWKU WP OGUUCIG GP ENCKT !!