Skip to main content

Quick and robust Python functions to do cryptanalysis using language statistics data for 15 languages

Project description

LanguageStatisticsLibPy

LanguageStatisticsLibPy is a Python library designed to facilitate the fast analysis and manipulation of language statistics data. It originates from a C# library that was first used in the widespread cryptography e-learning software CrypTool 2. From now on "CrypTool 2" is abbreviated "CT2". CT2 is an open-source e-learning program for Windows to do cryptography and cryptanalysis (https://www.cryptool.org/en/ct2/).

This Python library supports 15 different languages and offers functionality for generating and handling n-gram data, specifically for calculating n-gram frequencies using the language statistics files from CT2. Additionally, it facilitates the use of CT2's dictionaries through a "Word Tree", an efficient data structure for rapid word searches within a language.

The language statistics files (for example, en-5gram-nocs.gz indicates an English 5-gram file that is not case-sensitive and excludes spaces) can be found in the "LanguageStatistics" subdirectory of CT2, if you have installed CT2 on Windows. If you don't have a Windows machine or you don't want to install CT2, you may download the language statistics files and dictionaries from the CT2 Github repo: Language Statistics.

Remark: This package contains the implemented algorithms without the language statistics files. These files have to be downloaded separately as they occupy many megabytes.

Features

  • Support for multiple languages: The library includes predefined support for 15 languages, including English, German, Spanish, French -- each with its own set of unigram frequencies and alphabets.

  • N-gram loading: Users can load unigrams, bigrams, trigrams, tetragrams, pentagrams, and hexagrams as n-gram objects in supported languages, with the option to include or exclude spaces. The n-grams delivered in the language statistics files range from 1 to 5 (we don't deliver 6-grams within CT2, since these files are too big). All language statistics files delivered are case-insensitive, denoted as "nocs" in the filename. Each language statistics is available in two forms: with space/blank ("sp" in the filename) and without space/blank (indicated by the absence of "sp" in the filename) within the alphabet.

  • Index of Coincidence calculation: It offers a method to calculate the index of coincidence (IoC) for a given plaintext, which is useful for cryptanalysis and language pattern recognition.

  • Alphabet and number mapping: The library provides functionality to map characters to their respective positions in a language's alphabet and vice versa, supporting operations on encoded messages or language data.

  • Dynamic n-gram support: Depending on the available data, the library dynamically supports various n-gram types.

  • Word tree data structure: It supports a word tree data structure for fast word lookups (true = part of language, false = not part of language) of a specific language.

Usage

Prerequisites: LanguageStatisticsLibPy is installed on your computer via $ pip3 install LanguageStatisticsLibPy

  1. Initialization: Start by importing the LanguageStatistics class and specify the language code for your analysis.
  2. Loading n-grams: To load n-grams of your chosen type (e.g., unigrams, bigrams) for a specific language, use the create_grams method with the appropriate .gz file from the LanguageStatistics directory in CT2. For instance, to load English 4-grams that are case-insensitive and include the space/blank symbol, use the file named en-4gram-nocs-sp.gz.
  3. Calculating IoC: Calculate the index of coincidence for a given plaintext using the calculate_ioc method.
  4. Word tree loading: For advanced language analysis, load a pre-built word tree for a specific language using the load_word_tree method.

Sample usage (from file test1.py):

from languagestatisticslibpy.LanguageStatistics import LanguageStatistics as LS

plaintext = LS.map_text_into_number_space("HELLOWORD", LS.alphabets['en'])
ioc = LS.calculate_ioc(plaintext)

print(ioc)

You can find further example usages in the file test2.py within the package.

Supported languages

The library includes predefined configurations for the following languages:

  • English (en)
  • German (de)
  • Spanish (es)
  • French (fr)
  • Italian (it)
  • Hungarian (hu)
  • Russian (ru)
  • Czech (cs)
  • Greek (el)
  • Latin (la)
  • Dutch (nl)
  • Swedish (sv)
  • Portuguese (pt)
  • Polish (pl)
  • Turkish (tr)

Some more technical details

Where are the package files stored after installing the package and how to find this out

% pip3 list | grep  LanguageStatisticsLibPy
% pip3 show  LanguageStatisticsLibPy

# show package content (including the test files) for example on Mac
% tree /Users/be/Library/Python/3.13/lib/python/site-packages/LanguageStatisticsLibPy
# on Linux this could be in:
# /home/user/.local/lib/python3.10/site-packages/LanguageStatisticsLibPy/
...
# show content of a directory where the statistics files had been copied to
tree /Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/LSLP
...

How to call the test files

% pwd
/Users/be/Documents/Python/LanguageStatisticsLibPy_PIP-Test/testen2

% ls -l
-rwx------  1 be  staff   956 27 Dez 09:44 test1.py
-rwx------  1 be  staff  2944 27 Dez 09:42 test2.py

% python3 test1.py                                                     
0.08333333333333333

% python3 test2.py
Grams size: 1
	Grams loaded in 0:00:00.000097
	Grams normalized in 0:00:00.000007
	Text: HELLOWORLDTHISISATEST
	Cost value: 771793.56
...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

languagestatisticslibpy-1.0.4.tar.gz (21.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

languagestatisticslibpy-1.0.4-py3-none-any.whl (33.8 kB view details)

Uploaded Python 3

File details

Details for the file languagestatisticslibpy-1.0.4.tar.gz.

File metadata

  • Download URL: languagestatisticslibpy-1.0.4.tar.gz
  • Upload date:
  • Size: 21.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for languagestatisticslibpy-1.0.4.tar.gz
Algorithm Hash digest
SHA256 0d531d7ea160d79b07dd186b3316b692e68cca075c1791a4138cb66c537cb989
MD5 0d206680841aee97d5491cebb151f85e
BLAKE2b-256 59c26c27b68c0765bd10216125ab510d2ed8a413aa476bf234e5d30831251d1a

See more details on using hashes here.

File details

Details for the file languagestatisticslibpy-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for languagestatisticslibpy-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 c9e98845ead945137b43793e0d94e3778d686f5e4676da63f85b97e724c1b343
MD5 12065070afc2b198fcd3d4f72d27d6f1
BLAKE2b-256 92d054a3654f53ab163428aed7186756cde3b0388f824ea2c6c8d594bcac635e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page