PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

One Time Pads For Linux Bash TUT

Go down

One Time Pads For Linux Bash TUT Empty One Time Pads For Linux Bash TUT

Post by jamied_uk 7th September 2020, 15:04

Vid

Github Link:
https://github.com/jamieduk/OneTimePad-For-Linux-Bash

Box Link
https://app.box.com/s/lbu639i34327scpgmq2g7zjnzs90b8oj

Notes.txt
Code:
(c) J~Net 2020
Site
jnet.sytes.net
Forum
jnet.forumotion.com
Radio
radio2020.ddns.net

Welcome To J~Net Dynamic Pad For Encrypting / Decrypting With OneTime 2020 Menu
https://jnet.forumotion.com/t1709-one-time-pads-for-linux-bash-tut#2649

sudo chmod +x *.sh

./menu.sh

When asked put filename of file you want to encrypt and then you will need to export the cipher and otp.txt to be decrypted later!
Every file uses a new one time pad so all pads and ciphers should be kept seperate and removed when not required anymore!


aio.sh
Code:
#!/bin/bash
# (c) J~Net 2020
# jnet.sytes.net
#
# Simple script to encrypt text with a one-time pad
# https://jnet.forumotion.com/t1709-one-time-pads-for-linux-bash-tut#2649
#
# Example:
# Generate a One Time Pad based on the length of the plaintext
# ./aio.sh -g testfiletoenc.txt otp.txt > otp.txt
#
# Encrypt the plaintext with the otp
# ./aio.sh -e testfiletoenc.txt otp.txt > cipher.txt
#
# Decrypt the cipher text using the otp
# ./aio.sh -d cipher.txt otp.txt > dec.txt
#
function gen_otp(){
    # Generate a One Time Pad based on the length of the string to be encrypted
    plaintext=$1
    plainlen=${#plaintext}
    # Generate the One Time Pad
    otp=$(cat /dev/urandom | tr -dc "a-zA-Z0-9" |fold -w $plainlen | head -n 1)
    echo -n $otp
}

function otp_encrypt(){
    # XOR the plaintext string against the One Time Pad
    plaintext="$1"
    plainlen=${#plaintext}
    # Grab the OTP and base64 decode it
    otp=$2

    for (( i=0; i<${#plaintext}; i++ )); do
        char=$(printf '%d' "'${plaintext:$i:1}" )
        otpchar=$(printf '%d' "'${otp:$i:1}" )
        # XOR the two
        cipherchar=$(( $char ^ $otpchar ))
        # we convert to hex.
        # Converting back to an ASCII char is possible, but can give unpredictable results
        cipherstr=$cipherstr$(printf "%x " $cipherchar)
    done
    # Return the string, but base64 it for convenience in copy/pasting
    echo -n $cipherstr | base64
}

function otp_decrypt(){
    # Reverse the process applied in otp_encrypt to recover the plaintext
    ciphertext=$1
    ciphertext=$( echo -n "$ciphertext" | base64 -d )
    otp=$2
    pos=0
    for i in $ciphertext
    do
        cipherchar=$(printf '%d' "0x$i" )
        otpchar=$(printf '%d' "'${otp:$pos:1}" )
        cipherchar=$(( $cipherchar ^ $otpchar ))
        if [ "$cipherchar" == "10" ]
        then
            # We do this because BASH command substitution strips trailing newlines
            newchar=$(echo -e -n "\n"; echo -n "extrachar")
            newchar=${newchar%extrachar}
        else
            newchar="`printf "\x$(printf %x $cipherchar)"`"
        fi
        plainstr="${plainstr}${newchar}"
        pos=$(( $pos + 1 ))
    done
    echo "$plainstr"
}
# func main
mode=$1
# There are better ways to to usage checks, but this is quick
if [ ! "$mode" == "-e" ] && [ ! "$mode" == "-d" ] && [ ! "$mode" == "-g" ]
then
    echo "Usage $0 [-e|-d|-g] [string/file] [[otp]]"
    exit 1
fi
# Grab the arguments
str=${2:-"Hello World"}
otp=${3:-""}
if [ -f "$str" ]
then
    # User passed in a path to a file, read that in instead
    str=$( cat "$str" )
fi

if [ "$mode" == "-g" ]
then
    # Simply generate and output an OTP based on the provided text
    gen_otp "$str"
    exit 0
fi
if [ "$mode" == "-e" ]
then
    GEN=0
    if [ "$otp" == "" ]
    then
        # OTP hasn't been provided, so return one
        GEN=1
        otp=$( gen_otp "$str" )
    elif [ -f "$otp" ]
    then
        otp=$(cat "$otp")
    fi

    ciphered=$( otp_encrypt "$str" "$otp" )

    if [ "$GEN" == 1 ]
    then
        echo "OTP: $otp"
        echo ""
    fi
    echo "$ciphered"

else

    if [ -f "$otp" ]
    then
        otp=$(cat "$otp")
    fi

    decrypted=$( otp_decrypt "$str" "$otp" )
    echo "$decrypted"

fi



Sample Cypher for you to test

otp.txt
Code:
7cs5J2Ywx2amVlyaY7OospqQ

Cypher
Code:
NDIgNWUgMzAgMzkgNmQgM2MgNDcgNTEgMTkgNGUgMWEgMTEgMTAgNWUgNiA2NyAyMiAxYyAxIDE4
IDU1IGEgMiA2Mg==
Attachments
One Time Pads For Linux Bash TUT Attachment
OneTime_Linux_Bash_AIO_2020.zip You don't have permission to download attachments.(5 Kb) Downloaded 0 times


Last edited by jamied_uk on 7th September 2020, 16:42; edited 7 times in total
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

One Time Pads For Linux Bash TUT Empty Re: One Time Pads For Linux Bash TUT

Post by jamied_uk 7th September 2020, 16:01

Nice menu and helper files to make it easy with remove file options!

menu.sh
Code:
#!/bin/bash
# (c) J~Net 2020
# jnet.sytes.net
#
# https://jnet.forumotion.com/t1709-one-time-pads-for-linux-bash-tut#2649
#
# ./menu.sh
#
echo "Welcome To J~Net Dynamic Pad For Encrypting / Decrypting With OneTime 2020 Menu"
echo ""
#!/bin/bash
# Bash Menu Script Example
PS3='Please enter your choice: '
options=("Encrypt" "Decrypt" "Remove Files" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Encrypt")
            ./encrypt.sh
            echo ""
            echo "1) Encrypt
2) Decrypt
3) Remove Files
4) Quit"
            echo ""
            ;;
        "Decrypt")
            ./decrypt.sh
            echo ""
            echo "1) Encrypt
2) Decrypt
3) Remove Files
4) Quit"
            echo ""
            ;;
        "Remove Files")
            echo "You chose choice $REPLY which is $opt"
            sudo rm otp.txt
            sudo rm cipher.txt
            sudo rm dec.txt
            echo "Files Removed!"
            echo ""
            echo "1) Encrypt
2) Decrypt
3) Remove Files
4) Quit"
            echo ""
            ;;
        "Quit")
            break
            ;;
        *) echo "invalid option $REPLY";;
    esac
done


Last edited by jamied_uk on 7th September 2020, 16:28; edited 2 times in total
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

One Time Pads For Linux Bash TUT Empty Re: One Time Pads For Linux Bash TUT

Post by jamied_uk 7th September 2020, 16:09

encrypt.sh
Code:
#!/bin/bash
# (c) J~Net 2020
# jnet.sytes.net
#
# https://jnet.forumotion.com/t1709-one-time-pads-for-linux-bash-tut#2649
#
# ./encrypt.sh filename
#
echo "Welcome To J~Net Dynamic Pad For Encrypting / Decrypting With OneTime 2020"
echo ""
if [ -z "$1" ]
  then
    echo "Enter File To Encrypt"
    read filenameoftobedone
  else
    filenameoftobedone=$1
    echo "File To Be Encrypted $filenameoftobedone"
fi
#
echo ""
echo "Size of File $filenameoftobedone"
sizeoffile=$( ls -sh $filenameoftobedone )
echo "${sizeoffile}"
echo "Here we go, Encrypting $filenameoftobedone"
echo ""
./aio.sh -g $filenameoftobedone otp.txt > otp.txt
./aio.sh -e $filenameoftobedone otp.txt > cipher.txt
echo ""
echo "$filenameoftobedone Encrypted."
echo""
cat cipher.txt



decrypt.sh

Code:
#!/bin/bash
# (c) J~Net 2020
# jnet.sytes.net
#
# https://jnet.forumotion.com/t1709-one-time-pads-for-linux-bash-tut#2649
#
# ./decrypt.sh filename
#
echo "Welcome To J~Net Dynamic Pad For Encrypting / Decrypting With OneTime 2020"
echo ""
if [ -z "$1" ]
  then
    read -e -p "Enter File To Decrypt Or leave Blank For " -i "cipher.txt" filenameoftobedone
  else
    filenameoftobedone=$1
    echo "File To Be Decrypted $filenameoftobedone"
fi
#
echo ""
echo "Size of File $filenameoftobedone"
sizeoffile=$( ls -sh $filenameoftobedone )
echo "${sizeoffile}"
echo "Here we go, Decrypting $filenameoftobedone"
echo ""
./aio.sh -d $filenameoftobedone otp.txt > dec.txt

echo ""
echo "$filenameoftobedone Decrypted."
echo ""
cat dec.txt
jamied_uk
jamied_uk
Admin

Posts : 3053
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

One Time Pads For Linux Bash TUT Empty Re: One Time Pads For Linux Bash TUT

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum