Shell encrypt and decrypt

  

#!/bin/sh

     

# path to virtualenv

cd /home/srvc_nextgen_hitg/cp360

source cp360venv/bin/activate

echo "Virtualenv started"

cd /home/cp360/python_scripts

#Read python code

export AES_SECRET_KEY="K{;5%A5yHL&^efe-"

export apiacc="yen.why.saw-99"


encrypt_pswd=`python - <<END

import os

from AES import AES_ENCRYPT

aes_obj = AES_ENCRYPT()

encpt = aes_obj.encrypt(os.environ['apiacc'],os.environ['AES_SECRET_KEY'])

print(encpt)

END` 


echo "------encrypt_pswd------------"

echo $encrypt_pswd

echo "-------------------------------"


export encrypt_pswd=$encrypt_pswd

decrypt_pswd=`python - <<END

import os

from AES import AES_ENCRYPT

aes_obj = AES_ENCRYPT()

dencpt = aes_obj.decrypt(os.environ['encrypt_pswd'],os.environ['AES_SECRET_KEY'])

print(dencpt)

END` 


echo "------decrypt_pswd------------"

echo $decrypt_pswd

echo "-------------------------------"

#################################################

python

python_scripts]$ cat AES.py

import base64

from Crypto.Cipher import AES


class AES_ENCRYPT:

    def encrypt(self, text,secretkey):

        BS = len(secretkey)

        pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)

        cryptor = AES.new(secretkey.encode("utf8"), AES.MODE_ECB)

        self.ciphertext = cryptor.encrypt(bytes(pad(text), encoding="utf8"))

        # The strings obtained during AES encryption are not necessarily ASCII character sets. There may be problems when they are output to the terminal or saved. Base64 encoding is used

        return base64.b64encode(self.ciphertext).decode()

    def decrypt(self, text,secretkey):

        unpad = lambda s: s[0:-ord(s[-1:])]

        decode = base64.b64decode(text)

        cryptor = AES.new(secretkey.encode("utf8"), AES.MODE_ECB)

        plain_text = cryptor.decrypt(decode).decode()

        return unpad(plain_text)




###########activate

cat /bin/activate

# This file must be used with "source bin/activate" *from bash*

# you cannot run it directly



if [ "${BASH_SOURCE-}" = "$0" ]; then

    echo "You must source this script: \$ source $0" >&2

    exit 33

fi


deactivate () {

    unset -f pydoc >/dev/null 2>&1 || true


    # reset old environment variables

    # ! [ -z ${VAR+_} ] returns true if VAR is declared at all

    if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then

        PATH="$_OLD_VIRTUAL_PATH"

        export PATH

        unset _OLD_VIRTUAL_PATH

    fi

    if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then

        PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"

        export PYTHONHOME

        unset _OLD_VIRTUAL_PYTHONHOME

    fi


    # The hash command must be called to get it to forget past

    # commands. Without forgetting past commands the $PATH changes

    # we made may not be respected

    hash -r 2>/dev/null


    if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then

        PS1="$_OLD_VIRTUAL_PS1"

        export PS1

        unset _OLD_VIRTUAL_PS1

    fi


    unset VIRTUAL_ENV

    if [ ! "${1-}" = "nondestructive" ] ; then

    # Self destruct!

        unset -f deactivate

    fi

}


# unset irrelevant variables

deactivate nondestructive


VIRTUAL_ENV='/home/srvc_nextgen_hitg/cp360/cp360venv'

if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then

    VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")

fi

export VIRTUAL_ENV


_OLD_VIRTUAL_PATH="$PATH"

PATH="$VIRTUAL_ENV/bin:$PATH"

export PATH


# unset PYTHONHOME if set

if ! [ -z "${PYTHONHOME+_}" ] ; then

    _OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"

    unset PYTHONHOME

fi


if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then

    _OLD_VIRTUAL_PS1="${PS1-}"

    if [ "x" != x ] ; then

        PS1="${PS1-}"

    else

        PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"

    fi

    export PS1

fi


# Make sure to unalias pydoc if it's already there

alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true


pydoc () {

    python -m pydoc "$@"

}


# The hash command must be called to get it to forget past

# commands. Without forgetting past commands the $PATH changes

# we made may not be respected

hash -r 2>/dev/null


Comments

Popular posts from this blog

cdc

incrementally update