Git initial commit

This commit is contained in:
Lars Hahn 2023-08-19 23:37:12 +02:00
commit 534d88113d
13 changed files with 673 additions and 0 deletions

9
LICENSE Executable file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

14
README.md Executable file
View File

@ -0,0 +1,14 @@
# crypto
Implementation of different crypto systems; bases on Introduction to Cryptography with Open-Source Software by Alasdair McAndrew.
Visited successfully the cryptography lecture and now experimenting a bit with AES in C++ and python.
This short script/library is an approach to understand and implement the AES-128 en-/decryption not only to bit-lists, but also to typical files on your system.
At the moment, files can be en-/decrypted using only the 'electronic-code-book' mode (ECB), with a low security level in python.
Files of each type can be read, since files are opened binary.
Future aspects/goals:
-more modes, like CBC etc.
-additional to files, take whole folders/paths for en-/decryption
-speed up the python script (xx-08-2017: ~10KB/

101
lib-cp/aes.cpp Executable file
View File

@ -0,0 +1,101 @@
#include "aes.hpp"
#include <iostream>
aes::aes(std::string && KeyWord){
set_key(std::vector<unsigned char>(KeyWord.begin(),KeyWord.end()));
}
aes::aes(std::string & KeyWord){
set_key(std::vector<unsigned char>(KeyWord.begin(),KeyWord.end()));
}
aes::aes(std::vector<unsigned> && KeyWord){
set_key(std::vector<unsigned char>(KeyWord.begin(),KeyWord.end()));
}
aes::aes(std::vector<unsigned> & KeyWord){
set_key(std::vector<unsigned char>(KeyWord.begin(),KeyWord.end()));
}
aes::aes(std::vector<unsigned char> && KeyWord){
set_key(KeyWord);
}
aes::aes(std::vector<unsigned char> & KeyWord){
set_key(KeyWord);
}
void aes::set_key(std::vector<unsigned char> && KeyWord){
set_key(KeyWord);
}
void aes::set_key(std::vector<unsigned char> & KeyWord){
_adjust_key_length(KeyWord);
std::vector< std::vector<unsigned char> > KeyMatrix;
for(unsigned i = 0; i < MatDim; i++){
std::vector<unsigned char> NewCol;
for(unsigned j = 0; j < MatDim; j++){
NewCol.push_back(KeyWord[j*4+i]);
}
KeyMatrix.push_back(NewCol);
}
for(unsigned i = 0; i < ((RoundNumber+1)*MatDim); i++){
if(i%MatDim != 0){
std::vector<unsigned char> NewCol;
std::vector<unsigned char>::iterator Prev = KeyMatrix[KeyMatrix.size()-1].begin();
std::transform(KeyMatrix[i].begin(),KeyMatrix[i].end(),std::back_inserter(NewCol),[&Prev](const unsigned char Value){
return Value^*(Prev++);
});
KeyMatrix.push_back(NewCol);
}
else{
std::vector<unsigned char> NewCol;
std::vector< std::vector<unsigned> > _SBox = aes_base::_SBox;
std::transform(KeyMatrix[KeyMatrix.size()-1].begin(),KeyMatrix[KeyMatrix.size()-1].end(),std::back_inserter(NewCol),[_SBox](unsigned char & Value){
return aes_base::_SBox[((Value&240) >> 4)][(Value&15)];
});
std::rotate(NewCol.begin(),NewCol.end()-1,NewCol.end());
NewCol[1] ^= _RoundConst[(int)i/MatDim];
std::vector<unsigned char>::iterator Prev = KeyMatrix[i].begin();
std::transform(NewCol.begin(),NewCol.end(),NewCol.begin(),[&Prev](unsigned char & Value){
return Value^*(Prev++);
});
KeyMatrix.push_back(NewCol);
}
}
std::vector< std::vector<unsigned char> > KeyRoundMatrix;
for(unsigned i = 0; i < (RoundNumber+2); i++){
std::vector<unsigned char> RoundKey;
for(unsigned j = 0; j < MatDim; j++){
RoundKey.push_back(KeyMatrix[(i*MatDim)][j]);
RoundKey.push_back(KeyMatrix[(i*MatDim+1)][j]);
RoundKey.push_back(KeyMatrix[(i*MatDim+2)][j]);
RoundKey.push_back(KeyMatrix[(i*MatDim+3)][j]);
}
KeyRoundMatrix.push_back(RoundKey);
}
_KeyRoundMatrix = KeyRoundMatrix;
_InvKeyRoundMat = KeyRoundMatrix;
std::reverse(_InvKeyRoundMat.begin(),_InvKeyRoundMat.end());
}
std::vector< std::vector<unsigned char> > aes::key(){
return _KeyRoundMatrix;
}
std::vector< std::vector<unsigned char> > aes::key_inverse(){
return _InvKeyRoundMat;
}
unsigned aes::value(){
return _Value;
}
void aes::_adjust_key_length(std::vector<unsigned char> & KeyWord){
if(KeyWord.size() > ByteSize){
KeyWord = std::vector<unsigned char>(KeyWord.begin(),KeyWord.begin()+ByteSize);
}
else{
unsigned LeftBitsSide = 240, RightBitsSide = 15, Idx = 0, Row, Col;
while(KeyWord.size() < ByteSize){
Row = ((KeyWord[Idx]^LeftBitsSide) >> MatDim)%ByteSize;
Col = (KeyWord[Idx]^RightBitsSide)%ByteSize;
KeyWord.push_back(aes_base::_SBox[Row][Col]);
Idx = (Row+Col+KeyWord[KeyWord.size()-1])%ByteSize;
}
}
}

41
lib-cp/aes.hpp Executable file
View File

@ -0,0 +1,41 @@
#ifndef AES_HPP_
#define AES_HPP_
#include <algorithm>
#include <iterator>
#include <vector>
#include <string>
#include "aesbase.hpp"
class aes{
public:
aes(std::string && KeyWord);
aes(std::string & KeyWord);
aes(std::vector<unsigned> && KeyWord);
aes(std::vector<unsigned> & KeyWord);
aes(std::vector<unsigned char> && KeyWord);
aes(std::vector<unsigned char> & KeyWord);
std::vector< std::vector<unsigned char> > key();
std::vector< std::vector<unsigned char> > key_inverse();
void set_key(std::vector<unsigned char> && KeyWord);
void set_key(std::vector<unsigned char> & KeyWord);
bool Init;
unsigned value();
private:
void _adjust_key_length(std::vector<unsigned char> & KeyWord);
std::vector< std::vector<unsigned char> > _KeyRoundMatrix;
std::vector< std::vector<unsigned char> > _InvKeyRoundMat;
const std::vector<unsigned> _RoundConst = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1B,0x36};
const unsigned MatDim = 4;
const unsigned Byte = 8;
const unsigned RoundNumber = 9;
const unsigned ByteSize = 16;
const unsigned BitSize = 128;
unsigned _Value;
};
#endif

156
lib-cp/aesbase.cpp Executable file
View File

@ -0,0 +1,156 @@
#include "aesbase.hpp"
std::vector<unsigned char> aes_base::encrypt(std::vector<unsigned char> PlainMatrix, std::vector<unsigned char> & RoundKeyMatrix){
bool Inverse = false;
std::vector<unsigned char> Matrix = PlainMatrix;
_add_round_key(Matrix,RoundKeyMatrix.begin());
for(unsigned i = 1; i <= _RoundNumber; i++){
_byte_sub(Matrix,Inverse);
_shift_row(Matrix,Inverse);
_mix_columns(Matrix,Inverse);
_add_round_key(Matrix,RoundKeyMatrix.begin()+(i*_MatDim));
}
_byte_sub(Matrix,Inverse);
_shift_row(Matrix,Inverse);
_add_round_key(Matrix,RoundKeyMatrix.begin()+(_RoundNumber*_MatDim));
return Matrix;
}
std::vector<unsigned char> aes_base::decrypt(std::vector<unsigned char> CipherMatrix, std::vector<unsigned char> & RoundKeyMatrix){
bool Inverse = true;
std::vector<unsigned char> Matrix = CipherMatrix;
_add_round_key(Matrix,RoundKeyMatrix.begin());
_shift_row(Matrix,Inverse);
_byte_sub(Matrix,Inverse);
for(unsigned i = 1; i <= _RoundNumber; i++){
_add_round_key(Matrix,RoundKeyMatrix.begin()+(i*_MatDim));
_mix_columns(Matrix,Inverse);
_shift_row(Matrix,Inverse);
_byte_sub(Matrix,Inverse);
}
_add_round_key(Matrix,RoundKeyMatrix.begin()+(_RoundNumber*_MatDim));
return Matrix;
}
void aes_base::_byte_sub(std::vector<unsigned char> & Matrix, bool Inverse){
unsigned Left = 240, Right = 15;
if(Inverse == false){
std::transform(Matrix.begin(),Matrix.end(),Matrix.begin(),[Left,Right](unsigned char & Value){
return _SBox[(Value^Left) >> _MatDim][(Value^Right)];
});
}
else{
std::transform(Matrix.begin(),Matrix.end(),Matrix.begin(),[Left,Right](unsigned char & Value){
return _SBoxInv[(Value^Left) >> _MatDim][(Value^Right)];
});
}
}
void aes_base::_shift_row(std::vector<unsigned char> & Matrix, bool Inverse){
unsigned _MatDim = 4;
if(Inverse == false){
for(unsigned i = 1; i < _MatDim; i++){
std::rotate(Matrix.begin()+(i*_MatDim),Matrix.begin()+(i*_MatDim)+i,Matrix.begin()+((i+1)*_MatDim));
}
}
else{
for(unsigned i = 1; i < _MatDim; i++){
std::rotate(Matrix.begin()+(i*_MatDim),Matrix.begin()+((i+1)*_MatDim)-i,Matrix.begin()+((i+1)*_MatDim));
}
}
}
void aes_base::_mix_columns(std::vector<unsigned char> & Matrix, bool Inverse){
if(Inverse == false){
for(unsigned i = 0; i < _MatDim; i++){
Matrix[i*_MatDim+0] = _g_mul(Matrix[i*_MatDim+0],2)^_g_mul(Matrix[i*_MatDim+1],3)^_g_mul(Matrix[i*_MatDim+2],1)^_g_mul(Matrix[i*_MatDim+3],1);
Matrix[i*_MatDim+1] = _g_mul(Matrix[i*_MatDim+0],1)^_g_mul(Matrix[i*_MatDim+1],2)^_g_mul(Matrix[i*_MatDim+2],3)^_g_mul(Matrix[i*_MatDim+3],1);
Matrix[i*_MatDim+2] = _g_mul(Matrix[i*_MatDim+0],1)^_g_mul(Matrix[i*_MatDim+1],1)^_g_mul(Matrix[i*_MatDim+2],2)^_g_mul(Matrix[i*_MatDim+3],3);
Matrix[i*_MatDim+3] = _g_mul(Matrix[i*_MatDim+0],1)^_g_mul(Matrix[i*_MatDim+1],1)^_g_mul(Matrix[i*_MatDim+2],1)^_g_mul(Matrix[i*_MatDim+3],2);
}
}
else{
for(unsigned i = 0; i < _MatDim; i++){
Matrix[i*_MatDim+0] = _g_mul(Matrix[i*_MatDim+0],14)^_g_mul(Matrix[i*_MatDim+1],11)^_g_mul(Matrix[i*_MatDim+2],13)^_g_mul(Matrix[i*_MatDim+3],9);
Matrix[i*_MatDim+1] = _g_mul(Matrix[i*_MatDim+0],9)^_g_mul(Matrix[i*_MatDim+1],14)^_g_mul(Matrix[i*_MatDim+2],11)^_g_mul(Matrix[i*_MatDim+3],13);
Matrix[i*_MatDim+2] = _g_mul(Matrix[i*_MatDim+0],13)^_g_mul(Matrix[i*_MatDim+1],9)^_g_mul(Matrix[i*_MatDim+2],14)^_g_mul(Matrix[i*_MatDim+3],11);
Matrix[i*_MatDim+3] = _g_mul(Matrix[i*_MatDim+0],11)^_g_mul(Matrix[i*_MatDim+1],13)^_g_mul(Matrix[i*_MatDim+2],9)^_g_mul(Matrix[i*_MatDim+3],14);
}
}
}
void aes_base::_add_round_key(std::vector<unsigned char> & Matrix, std::vector<unsigned char>::iterator MatBegin){
std::transform(Matrix.begin(),Matrix.end(),Matrix.begin(),[&MatBegin](unsigned char & Value){
return Value^*(MatBegin++);
});
}
unsigned char aes_base::_g_mul(unsigned char Value, unsigned char Factor){
if(Factor == 1){
return Value;
}
else if(Factor == 2){
if(Value < 128){
return 2*Value;
}
else{
return (2*Value)^(0x11b);
}
}
else if(Factor == 3){
return _g_mul(Value,2)^Value;
}
else if(Factor == 9){
return _g_mul(_g_mul(_g_mul(Value,2),2),2)^Value;
}
else if(Factor == 11){
return _g_mul(_g_mul(_g_mul(Value,2),2)^Value,2)^Value;
}
else if(Factor == 13){
return _g_mul(_g_mul(_g_mul(Value,2)^Value,2),2)^Value;
}
else if(Factor == 14){
return _g_mul(_g_mul(_g_mul(Value,2)^Value,2)^Value,2);
}
}
const std::vector< std::vector<unsigned> > aes_base::_SBox = {
{0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76},
{0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x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0x1F, 0x4B, 0xBD, 0x8B, 0x8A},
{0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E},
{0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF},
{0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16}
};
const std::vector< std::vector<unsigned> > aes_base::_SBoxInv = {
{0x52, 0x09, 0x6A, 0xD5, 0x30, 0x36, 0xA5, 0x38, 0xBF, 0x40, 0xA3, 0x9E, 0x81, 0xF3, 0xD7, 0xFB},
{0x7C, 0xE3, 0x39, 0x82, 0x9B, 0x2F, 0xFF, 0x87, 0x34, 0x8E, 0x43, 0x44, 0xC4, 0xDE, 0xE9, 0xCB},
{0x54, 0x7B, 0x94, 0x32, 0xA6, 0xC2, 0x23, 0x3D, 0xEE, 0x4C, 0x95, 0x0B, 0x42, 0xFA, 0xC3, 0x4E},
{0x08, 0x2E, 0xA1, 0x66, 0x28, 0xD9, 0x24, 0xB2, 0x76, 0x5B, 0xA2, 0x49, 0x6D, 0x8B, 0xD1, 0x25},
{0x72, 0xF8, 0xF6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xD4, 0xA4, 0x5C, 0xCC, 0x5D, 0x65, 0xB6, 0x92},
{0x6C, 0x70, 0x48, 0x50, 0xFD, 0xED, 0xB9, 0xDA, 0x5E, 0x15, 0x46, 0x57, 0xA7, 0x8D, 0x9D, 0x84},
{0x90, 0xD8, 0xAB, 0x00, 0x8C, 0xBC, 0xD3, 0x0A, 0xF7, 0xE4, 0x58, 0x05, 0xB8, 0xB3, 0x45, 0x06},
{0xD0, 0x2C, 0x1E, 0x8F, 0xCA, 0x3F, 0x0F, 0x02, 0xC1, 0xAF, 0xBD, 0x03, 0x01, 0x13, 0x8A, 0x6B},
{0x3A, 0x91, 0x11, 0x41, 0x4F, 0x67, 0xDC, 0xEA, 0x97, 0xF2, 0xCF, 0xCE, 0xF0, 0xB4, 0xE6, 0x73},
{0x96, 0xAC, 0x74, 0x22, 0xE7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E},
{0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B},
{0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4},
{0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F},
{0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF},
{0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61},
{0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D}
};
const unsigned aes_base::_MatDim = 4;
const unsigned aes_base::_ByteSize = 16;
const unsigned aes_base::_RoundNumber = 9;
const unsigned aes_base::_BlockSize = 128;

24
lib-cp/aesbase.hpp Executable file
View File

@ -0,0 +1,24 @@
#ifndef AESBASE_CPP_
#define AESBASE_CPP_
#include <algorithm>
#include <vector>
namespace aes_base{
std::vector<unsigned char> encrypt(std::vector<unsigned char> PlainMatrix, std::vector<unsigned char> & RoundKeyMatrix);
std::vector<unsigned char> decrypt(std::vector<unsigned char> CipherMatrix, std::vector<unsigned char> & RoundKeyMatrix);
void _byte_sub(std::vector<unsigned char> & Matrix, bool Inverse);
void _shift_row(std::vector<unsigned char> & Matrix, bool Inverse);
void _mix_columns(std::vector<unsigned char> & Matrix, bool Inverse);
void _add_round_key(std::vector<unsigned char> & Matrix, std::vector<unsigned char>::iterator MatBegin);
unsigned char _g_mul(unsigned char Value, unsigned char Factor);
extern const std::vector< std::vector<unsigned> > _SBox;
extern const std::vector< std::vector<unsigned> > _SBoxInv;
extern const unsigned _MatDim;
extern const unsigned _ByteSize;
extern const unsigned _RoundNumber;
extern const unsigned _BlockSize;
};
#endif

26
lib-cp/main.cpp Executable file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <string>
#include "aes.hpp"
int main(){
std::vector<unsigned> Key = {0x1a,0x58,0x2a,0x1b,0xa1,0x0e,0xa0,0x18,0xf1,0x4a,0xc3,0x11,0xaa,0x2a,0xc1,0xff};
aes CryptoSystem(Key);
std::vector< std::vector<unsigned char> > AES_Key = CryptoSystem.key();
for(auto RoundKey : AES_Key){
for(auto Value: RoundKey){
std::cout << (char)(Value+'A') << ' ';
}
std::cout << std::endl;
}
AES_Key = CryptoSystem.key_inverse();
for(auto RoundKey : AES_Key){
for(auto Value: RoundKey){
std::cout << (char)(Value+'A') << ' ';
}
std::cout << std::endl;
}
}

292
lib-py/aes.py Executable file
View File

@ -0,0 +1,292 @@
import argparse, itertools, os
from copy import deepcopy
from os import path
class aes_base:
_SBox = [
[0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76],
[0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0],
[0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x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7, 0xAD, 0x35, 0x85, 0xE2, 0xF9, 0x37, 0xE8, 0x1C, 0x75, 0xDF, 0x6E],
[0x47, 0xF1, 0x1A, 0x71, 0x1D, 0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE, 0x1B],
[0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A, 0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4],
[0x1F, 0xDD, 0xA8, 0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xEC, 0x5F],
[0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A, 0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF],
[0xA0, 0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB, 0x3C, 0x83, 0x53, 0x99, 0x61],
[0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D]
]
ByteSize = 8
RoundNumber = 9
BlockSize = 128
def encrypt(PlainTextMatrix,KeyRoundMatrix):
Inverse = False
ARK = aes_base._add_round_key(PlainTextMatrix,aes_base._get_round_key(KeyRoundMatrix,0))
for i in range(aes_base.RoundNumber):
BSB = aes_base._byte_sub(ARK,Inverse)
SR = aes_base._shift_row(BSB,Inverse)
MC = aes_base._mix_columns(SR,Inverse)
ARK = aes_base._add_round_key(MC,aes_base._get_round_key(KeyRoundMatrix,i+1))
BSB = aes_base._byte_sub(ARK,Inverse)
SR = aes_base._shift_row(BSB,Inverse)
ARK = aes_base._add_round_key(SR,aes_base._get_round_key(KeyRoundMatrix,aes_base.RoundNumber+1))
return ARK
def decrypt(CipherTextMatrix,KeyRoundMatrix):
Inverse = True
ARK = aes_base._add_round_key(CipherTextMatrix,aes_base._get_round_key(KeyRoundMatrix,0))
SR = aes_base._shift_row(ARK,Inverse)
BSB = aes_base._byte_sub(SR,Inverse)
for i in range(aes_base.RoundNumber):
ARK = aes_base._add_round_key(BSB,aes_base._get_round_key(KeyRoundMatrix,i+1))
MC = aes_base._mix_columns(ARK,Inverse)
SR = aes_base._shift_row(MC,Inverse)
BSB = aes_base._byte_sub(SR,Inverse)
ARK = aes_base._add_round_key(BSB,aes_base._get_round_key(KeyRoundMatrix,aes_base.RoundNumber+1))
return ARK
def _byte_sub(Matrix,Inverse = False):
if Inverse == False:
return [[aes_base._SBox[int(bin(Value)[2:].zfill(8)[:aes_base.ByteSize//2],2)][int(bin(Value)[2:].zfill(8)[aes_base.ByteSize//2:],2)] for Value in Row] for Row in Matrix]
else:
return [[aes_base._SBoxInv[int(bin(Value)[2:].zfill(8)[:aes_base.ByteSize//2],2)][int(bin(Value)[2:].zfill(8)[aes_base.ByteSize//2:],2)] for Value in Row] for Row in Matrix]
def _shift_row(Matrix,Inverse = False):
if Inverse == False:
return [aes_base._row_rotate(Row,Idx) for Idx,Row in enumerate(Matrix)]
else:
return [aes_base._row_rotate(Row,Idx,True) for Idx,Row in enumerate(Matrix)]
def _mix_columns(Matrix,Inverse = False):
MC = deepcopy(Matrix)
if Inverse == False:
for i in range(aes_base.ByteSize//2):
MC[0][i] = aes_base._g_mul(Matrix[0][i],2)^aes_base._g_mul(Matrix[1][i],3)^aes_base._g_mul(Matrix[2][i],1)^aes_base._g_mul(Matrix[3][i],1)
MC[1][i] = aes_base._g_mul(Matrix[0][i],1)^aes_base._g_mul(Matrix[1][i],2)^aes_base._g_mul(Matrix[2][i],3)^aes_base._g_mul(Matrix[3][i],1)
MC[2][i] = aes_base._g_mul(Matrix[0][i],1)^aes_base._g_mul(Matrix[1][i],1)^aes_base._g_mul(Matrix[2][i],2)^aes_base._g_mul(Matrix[3][i],3)
MC[3][i] = aes_base._g_mul(Matrix[0][i],3)^aes_base._g_mul(Matrix[1][i],1)^aes_base._g_mul(Matrix[2][i],1)^aes_base._g_mul(Matrix[3][i],2)
else:
for i in range(aes_base.ByteSize//2):
MC[0][i] = aes_base._g_mul(Matrix[0][i],14)^aes_base._g_mul(Matrix[1][i],11)^aes_base._g_mul(Matrix[2][i],13)^aes_base._g_mul(Matrix[3][i],9)
MC[1][i] = aes_base._g_mul(Matrix[0][i],9)^aes_base._g_mul(Matrix[1][i],14)^aes_base._g_mul(Matrix[2][i],11)^aes_base._g_mul(Matrix[3][i],13)
MC[2][i] = aes_base._g_mul(Matrix[0][i],13)^aes_base._g_mul(Matrix[1][i],9)^aes_base._g_mul(Matrix[2][i],14)^aes_base._g_mul(Matrix[3][i],11)
MC[3][i] = aes_base._g_mul(Matrix[0][i],11)^aes_base._g_mul(Matrix[1][i],13)^aes_base._g_mul(Matrix[2][i],9)^aes_base._g_mul(Matrix[3][i],14)
return MC
def _add_round_key(Matrix,KeyRoundMatrix):
return [[Value^KeyRoundMatrix[Idx][Jdx] for Jdx,Value in enumerate(Row)] for Idx,Row in enumerate(Matrix)]
def _row_rotate(OldRow,Rotation,Inverse = False):
Row = deepcopy(OldRow)
if Inverse == False: #to left
for i in range(Rotation):
Row.append(Row.pop(0))
else:
for i in range(Rotation):
Row.insert(0,Row.pop())
return Row
def _g_mul(Value,Factor):
if Factor == 1:
return Value
elif Factor == 2:
if Value < 2**7:
return 2*Value
else:
return (2*Value)^(0x11b)
elif Factor == 3:
return aes_base._g_mul(Value,2)^Value
elif Factor == 9:
return aes_base._g_mul(aes_base._g_mul(aes_base._g_mul(Value,2),2),2)^Value
elif Factor == 11:
return aes_base._g_mul(aes_base._g_mul(aes_base._g_mul(Value,2),2)^Value,2)^Value
elif Factor == 13:
return aes_base._g_mul(aes_base._g_mul(aes_base._g_mul(Value,2)^Value,2),2)^Value
elif Factor == 14:
return aes_base._g_mul(aes_base._g_mul(aes_base._g_mul(Value,2)^Value,2)^Value,2)
def _get_round_key(KeyRoundMatrix,Round):
return [[Col[i] for Col in KeyRoundMatrix[Round*(aes_base.ByteSize//2):(Round+1)*aes_base.ByteSize]] for i in range(aes_base.ByteSize//2)]
class aes:
MatDim = 4
Byte = 8
ByteSize = 16
BitSize = 128
RoundConst = [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1B,0x36]
def __init__(self,Key,OpMode = 'ECB'):
self._OpMode = OpMode
self._KeyRoundMatrix = aes._key_expansion(Key)
self._InvKeyRoundMat = aes._key_inversion(self._KeyRoundMatrix)
self._Format = list
def encrypt(self,TextString):
ByteMatrix = self._format_input(TextString)
EncryptMatrix = aes_base.encrypt(ByteMatrix,self._KeyRoundMatrix)
Encryption = self._format_output(EncryptMatrix)
return Encryption
def encrypt_file(self,FileName):
File = open(FileName,'r+b')
Block = 'EMPTY'
TextLeng = 0
while len(Block) != 0:
WritePos = File.tell()
Block = File.read(aes.ByteSize)
TextLeng += len(Block)
if len(Block) != 0:
File.seek(WritePos)
File.write(self.encrypt(Block))
File.write(self.encrypt(aes._size_to_bytes(TextLeng)))
File.close()
def encrypt_folder(self,FolderName):
Files = []
Folders = []
for Item in os.listdir(FolderName):
PathName = path.join(FolderName,Item)
if path.isfile(PathName):
Files.append(PathName)
elif path.isdir(PathName):
Folders.append(PathName)
for File in Files:
self.encrypt_file(File)
File = []
for Folder in Folders:
self.encrypt_folder(Folder)
def decrypt(self,TextString):
ByteMatrix = self._format_input(TextString)
DecryptMatrix = aes_base.decrypt(ByteMatrix,self._InvKeyRoundMat)
Decryption = self._format_output(DecryptMatrix)
return Decryption
def decrypt_file(self,FileName):
File = open(FileName,'r+b')
Block = 'EMPTY'
while len(Block) != 0:
WritePos = File.tell()
Block = File.read(aes.ByteSize)
if len(Block) != 0:
File.seek(WritePos)
File.write(self.decrypt(Block))
File.seek(WritePos-aes.ByteSize)
TextLeng = aes._bytes_to_size(File.read(aes.ByteSize))
if(TextLeng < (2**64-1)):
File.seek(0)
File.truncate(TextLeng)
File.close()
def decrypt_folder(self,FolderName):
Files = []
Folders = []
for Item in os.listdir(FolderName):
PathName = path.join(FolderName,Item)
if path.isfile(PathName):
Files.append(PathName)
elif path.isdir(PathName):
Folders.append(PathName)
for File in Files:
self.decrypt_file(File)
File = []
for Folder in Folders:
self.decrypt_folder(Folder)
def _key_expansion(Key):
KeyMatrix = list(bytes(Key.encode()))
LeftBitsSide = 2**aes.Byte-2**(aes.Byte//2)
RightBitsSide = 2**aes.Byte-1
Idx = 0
while(len(KeyMatrix) != aes.ByteSize):
Row = ((KeyMatrix[Idx]&LeftBitsSide) >> aes.Byte//2)%aes.ByteSize
Col = ((KeyMatrix[Idx]&RightBitsSide))%aes.ByteSize
KeyMatrix.append(aes_base._SBox[Row][Col])
Idx = (Row+Col+KeyMatrix[-1])%aes.ByteSize
KeyRoundMatrix = [KeyMatrix[i*aes.MatDim:(i+1)*aes.MatDim] for i in range(aes.MatDim)]
for i in range(aes.MatDim*(aes_base.RoundNumber+1)):
if i%(aes.MatDim) != 0:
KeyRoundMatrix.append([KeyRoundMatrix[i][Idx]^Value for Idx,Value in enumerate(KeyRoundMatrix[-1])])
else:
KeyTrafo = aes._key_col_trafo(KeyRoundMatrix[-1],i)
KeyRoundMatrix.append([KeyRoundMatrix[i][Idx]^Value for Idx,Value in enumerate(KeyTrafo)])
return KeyRoundMatrix
def _key_col_trafo(Col,Round):
return [aes._key_sbox_trafo(Col[3]), aes._key_sbox_trafo(Col[0])^aes.RoundConst[Round//aes.MatDim], aes._key_sbox_trafo(Col[1]), aes._key_sbox_trafo(Col[2])]
def _key_sbox_trafo(Value):
return aes_base._SBox[((Value^(2**aes.Byte-2**(aes.Byte//2))) >> aes.Byte//2)%aes.ByteSize][((Value^(2**aes.Byte-1)))%aes.ByteSize]
def _key_inversion(KeyMatrix):
return list(itertools.chain.from_iterable(reversed([KeyMatrix[(i*aes.MatDim):(i+1)*aes.MatDim] for i in range(aes_base.RoundNumber+2)])))
def _8bit_to_byte(Bits):
Byte = 0
for Idx,Bit in enumerate(reversed(Bits)):
Byte |= (Bit << Idx)
return Byte
def _size_to_bytes(Size):
BitList = list(map(int,list(bin(Size)[2:].zfill(aes.BitSize))))
return bytes([ aes._8bit_to_byte(BitList[i*aes.Byte:(i+1)*aes.Byte]) for i in range(aes.ByteSize)])
def _bytes_to_size(Bytes):
return int(''.join([ bin(Block)[2:].zfill(aes.Byte) for Block in list(Bytes)]),2)
def _format_input(self,TextString):
ByteList = []
ByteMatrix = []
if type(TextString) is str:
ByteList = list(TextString.encode())
ByteList.extend([255]*(aes.ByteSize-len(ByteList)))
self._Format = bytes
elif type(TextString) is bytes:
ByteList = list(TextString)
ByteList.extend([255]*(aes.ByteSize-len(ByteList)))
self._Format = bytes
elif type(TextString) is list:
ByteList = [int(Sig) for Sig in TextString]+[255]*(aes.ByteSize-len(TextString))
self._Format = list
else:
raise TypeError('Input must be either String, Bytes or List')
if len(ByteList) == aes.ByteSize:
ByteMatrix = [ByteList[i*aes.MatDim:(i+1)*aes.MatDim] for i in range(aes.MatDim)]
elif len(ByteList) == aes.BitSize:
pass #TODO 128-BitList to 16Byte Matrix
else:
raise ValueError('Input must contain 16 Bytes or 128 Bits')
return ByteMatrix
def _format_output(self,ByteMatrix):
TextString = list(itertools.chain.from_iterable(ByteMatrix))
if self._Format is bytes:
TextString = bytes(TextString)
return TextString
Parser = argparse.ArgumentParser(description='En-/Decryption for Strings,Files and Folders')
Parser.add_argument('--file', metavar='<PATH_TO_FILE>', type=str, nargs=1, help='File that should be en-/decrypted.', required = False)
Parser.add_argument('--folder', metavar='<PATH>', type=str, nargs=1, help='Folder with files that should be en-/decrypted; recursive.', required = False)
Parser.add_argument('--text', metavar='<PATH_TO_FILE>', type=str, nargs=1, help='File that should be en-/decrypted.', required = False)
Parser.add_argument('--key', metavar='<KEY>', type=str, nargs=1, help='asdf', required = True)
Parser.add_argument('--decrypt', help='Enable input file decryption', action='store_true', required = False)
Args = Parser.parse_args()
KeyWord = Args.key[0]
Decryption = Args.decrypt
CryptoSystem = aes(KeyWord)
if not (Args.file or Args.folder or Args.text):
Parser.error('one of the following arguments is requested: --file, --folder, --text')
if Decryption == False:
if Args.file:
CryptoSystem.encrypt_file(Args.file[0])
elif Args.folder:
CryptoSystem.encrypt_folder(Args.folder[0])
else:
if Args.file:
CryptoSystem.decrypt_file(Args.file[0])
elif Args.folder:
CryptoSystem.decrypt_folder(Args.folder[0])

2
lib-py/sample.dat Executable file
View File

@ -0,0 +1,2 @@
This is my textfile and I like it very much!
\\Herro!!$$@@

View File

@ -0,0 +1,2 @@
This is my textfile and I like it very much!
\\Herro!!$$@@

View File

@ -0,0 +1,2 @@
This is my textfile and I like it very much!
\\Herro!!$$@@ This is another FILE

View File

@ -0,0 +1,2 @@
This is my textfile and I like it very much!
\\Herro!!$$@@

View File

@ -0,0 +1,2 @@
This is my textfile and I like it very much!
\\Herro!!$$@@ This is another FILE