Git Product home page Git Product logo

pygost's Introduction

pyGOST

This Python program provides an implementation of the soviet GOST cipher. The GOST cipher is a symmetric block cipher that works on 64 bit blocks using 256 bit keys. The S-Boxes used in this implementation are conformed to the standard GOST R 34.12-2015.

To use the cipher, you need to import the library GOST and instantiate a GOST object. Use the setter methods to set the plaintext (or ciphertext), the key and, in case you want to use the cipher in CBC mode, you have to possibility to set the IV. The parameters required are always in binary form, so strings containing only 0s and 1s. Beware that these parameters are strings, not binary numbers!! Using the encrypt and the decrypt methods (specifying, optionally, ECB or CBC mode) you'll be able to encrypt and decrypt the message you set or the ciphertext. If you encrypt a message, the result will also be stored in a GOST attribute, namely self.encrypted; if you then use the decrypt method, you won't need to set the ciphertext because it's already there.

As of now, the library supports ECB, CBC, OFB, CFB and CTR modes.

Here is an example of how the program should be used for both encryption and decryption.

Encryption

from GOST import GOST

import my_utils

gost = GOST()

key, salt = my_utils.pbkdf2('Password', 'Salt')

gost.set_message(my_utils.string_to_bytes('Hello, world!'))

gost.set_key(key)

gost.set_operation_mode(gost.CBC)

gost.encrypt()

// you can get the resulting ciphertext with gost.get_encrypted_msg() and the IV with gost.get_iv()

// the IV is randomly generated if not provided in the setup phase

Decryption

from GOST import GOST

import my_utils

gost = GOST()

key, salt = my_utils.pbkdf2('Password', 'Salt')

ciphertext = // a k*64 character string, where k = 0, 1, ...

iv = // a 256 character string containing only 0s and 1s

gost.set_iv(iv)

gost.set_key(key)

gost.set_operation_mode(gost.CBC)

gost.set_encrypted_msg(ciphertext)

gost.decrypt()

NOTES

Beware that when the programs produces outputs, there could be some mismatches in the length of the strings. Suppose that the ciphertext is 63 zeros and a single 1. Then the result would be 1 because the 0s are trimmed out. If you use the functions in my_utils (namely, hex_to_bin_mult_64 and leading_zeros_hex) you'll be able to overcome this problem.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.