Coder Social home page Coder Social logo

des's Introduction

DES in C

C implementation of Data Encryption Standard algorithm.

Overview

The Data Encryption Standard (DES) is a block cipher (a form of shared secret encryption) that was selected by the National Bureau of Standards as an official Federal Information Processing Standard (FIPS) for the United States in 1976 and which has subsequently enjoyed widespread use internationally. It is based on a symmetric-key algorithm that uses a 56-bit key.

This implementation of DES is not optimized in any way. The code has been written to provide readability and easy understanding of the algorithm. Padding scheme used in this implementation is [PKCS5]

Compilation & Installation

This implementation has only been tested on Unix platform. But you may be able to compile/ run it on Windows.

  1. Make sure des.c, des.h and run_des.c are in the same directory
  2. Compile using: gcc -O3 des.c run_des.c -o run_des.o

Usage

Say we want to encrypt/ decrypt a file named /home/user/sample.txt

  1. Generate a keyfile using:

    run_des.o -g /tmp/keyfile.key
    
  2. Encrypt sample.txt using:

    run_des.o -e /tmp/keyfile.key /home/user/sample.txt /home/user/sample.enc
    
  3. Decrypt sample.txt using:

    run_des.o -d /tmp/keyfile.key /home/user/sample.enc /home/user/sample_decrypted.txt
    

Don't lose the key file! you won't be able to decrypt an encrypted if you lose the keyfile.

More

DES is provided for educational purposes only. Do not use for any other reason. It has been implemented after J. Orlin Grabbe's DES Algorithm Illustrated

It is possible to use this implementation to facilitate TripleDES encryption process:

  1. Generate keys using:

    run_des.o -g /tmp/keyfile1.key
    run_des.o -g /tmp/keyfile2.key
    run_des.o -g /tmp/keyfile3.key
    
  2. Encrypt using:

    run_des.o -e /tmp/keyfile1.key /home/user/sample.txt /home/user/sample.enc1
    run_des.o -e /tmp/keyfile2.key /home/user/sample.enc1 /home/user/sample.enc2
    run_des.o -e /tmp/keyfile3.key /home/user/sample.enc2 /home/user/sample.enc3
    
  3. Decrypt using:

    run_des.o -d /tmp/keyfile3.key /home/user/sample.enc3 /home/user/sample.dec3
    run_des.o -d /tmp/keyfile2.key /home/user/sample.dec3 /home/user/sample.dec2
    run_des.o -d /tmp/keyfile1.key /home/user/sample.dec2 /home/user/sample_decrypted.txt
    

The primary repository for DES is located at: http://github.com/tarequeh/DES/ The blog post discussing the implementation can be found at: CodeXN This implementation of DES was written by Tareque Hossain

des's People

Contributors

tarequeh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

des's Issues

A bug in function generate_sub_keys

Hi, the code is well-written but I find a bug in the function generate_sub_keys. You don't initialize the key_sets before using it neither before calling the function. Thus the generated keys go wrong in my test.
My suggestion is as follows. Just add memset(key_sets, 0, 17 * sizeof(key_set); in the top of the function.

3DES

I think that 3DES works by first encrypting with k1, then decrypt with k2 and encrypt a second time with k3.

test failed

#include <malloc.h>
#include <string.h>
#include <cstdio>
#include <stdlib.h>
#include <cassert>
#include <cstdlib>
#include "des.h"

void c(const char* const src, char* const dst, const int size, const key_set* const key_sets, int m) {
    char buf_src[8], buf_dst[8];
    int block = size / 8;
    int mod = size % 8;
    memcpy(dst + block * 8, src + block * 8, mod);

    for (int i = 0; i < block; ++i) {
        memcpy(buf_src, src + i * 8, 8);
        process_message((unsigned char *) buf_src, (unsigned char *) buf_dst, (key_set *) key_sets, m);
        memcpy(dst + i * 8, buf_dst, 8);
    }
}

void c(const char* const src, char* const dst, const int size, int m) {
    char key1[] = {-5, 11, 24, -51, 34, 46, 98, -104};
    char key2[] = {-51, 101, 44, -21, 54, 47, 91, -114};
    char key3[] = {-57, 111, 27, -81, 38, 66, 58, -14};

    key_set* key_sets1 = (key_set*)malloc(17 * sizeof(key_set));
    key_set* key_sets2 = (key_set*)malloc(17 * sizeof(key_set));
    key_set* key_sets3 = (key_set*)malloc(17 * sizeof(key_set));

    generate_sub_keys((unsigned char *) key1, key_sets1);
    generate_sub_keys((unsigned char *) key2, key_sets2);
    generate_sub_keys((unsigned char *) key3, key_sets3);

    if (m == ENCRYPTION_MODE) {
        c(src, dst, size, key_sets1, m);
        c(dst, dst, size, key_sets2, m);
        c(dst, dst, size, key_sets3, m);
    } else {
        c(src, dst, size, key_sets3, m);
        c(dst, dst, size, key_sets2, m);
        c(dst, dst, size, key_sets1, m);
    }

    free(key_sets1);
    free(key_sets2);
    free(key_sets3);
}

bool test(char* src, int size) {
    char* dst = new char[size];
    char* dst2 = new char[size];

    c(src, dst, size, ENCRYPTION_MODE);
    c(dst, dst2, size, DECRYPTION_MODE);

    for (int i = 0; i < size; ++i) {
        if (dst2[i] != src[i]) {
            delete [] dst;
            delete [] dst2;
            return false;
        }
    }

    delete [] dst;
    delete [] dst2;
    return true;
}

int main(int argc, char** argv) {
    for (int i = 0; i < 1000; ++i) {
        int size = rand() % 9999;
        unsigned char* src = new unsigned char[size];
        for (int s = 0; s < size; ++s) {
            src[s] = rand() % 256;
        }
        if (!test((char*)src, size)) {
            printf("bug:%d,count:%d\n", i, size);
        }
        delete [] src;
    }
    return 0;
}

result
bug:201,count:3066
os
windows 10
compiler
E:\workplace\sample\jni-classloader\src\main\java>g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/Program\ Files/mingw-w64/x86_64-6.2.0-posix-seh-rt_v5- rev1/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/6.2.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-6.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw620/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/include -I/c/mingw620/prerequisites/x86_64-zlib-static/include -I/c/mingw620/prerequisites/x86_64-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/include -I/c/mingw620/prerequisites/x86_64-zlib-static/include -I/c/mingw620/prerequisites/x86_64-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw620/x86_64-620-posix-seh-rt_v5-rev1/mingw64/opt/lib -L/c/mingw620/prerequisites/x86_64-zlib-static/lib -L/c/mingw620/prerequisites/x86_64-w64-mingw32- static/lib '
Thread model: posix
gcc version 6.2.0 (x86_64-posix-seh-rev1, Built by MinGW-W64 project)

generate_sub_keys depends on initial value of key_sets

There's a bug somewhere in generate_sub_keys. I haven't dug into it. I just noticed that if write a value into key sets before generating the sub keys, the output ciphertext changes.

$ xxd outx
00000000: 8ca6 4de9 c1b1 23a7 7e42 2822 7736 66c0  ..M...#.~B("w6f.
$ vim run_des.c # Add a memset before generate_sub_keys
$ gcc -o mydes des.c run_des.c
$ ./mydes -e key inp outy
Encrypting..
Finished processing inp. Time taken: 0.000106 seconds.
$ xxd outy
00000000: 038b 53ff 9828 9c81 1bb7 97cd 35ab 14ad  ..S..(......5...

input user's kwy

can the user input the key instead of been generated by the program?

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.