Simple encryption and decryption of string by bash and its Application
Generation of passwords and encryption and decryption
1. Original code
Cat gen_en_de_pswd.sh
#! / bin/bash
Case $1 in
1)
# randomly generate n-bit digital strings
Echo-e "Enter your password length:"
Read length
If [$length-gt 10]; then
Pw= `cat / dev/urandom | head-n 10 | md5sum | head-c $roomth`
Echo "The password is:" $pw
Elif [$length-le 10] & & [$length-ge 5]; then
Pw= `cat / dev/urandom | head-n 10000 | sed's / [^ a-zA-Z0-9] / / g'| strings-n $length | head-c $roomth`
Echo "The password is:" $pw
Else
Echo "Your password is too short,Please try again!"
Fi
2)
# simple encryption of strings
Echo-e "Enter your password which need to encrypt:"
Read passwd
En_tmp= `echo $passwd | tr '0123456789 ANzmGTO OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' ANzmGT#$&*0123456789' | tr' echo 'ANzmGTH
Len= `echo ${# en_tmp} `
If [$len-lt 10]; then
Suf_len= `expr 10-$len`
Suffixal= `cat / dev/urandom | head-n 1000 | sed's / [^ a-zA-Z0-9] / / g'| strings-n $suf_len | head-n 1 | head-c $suf_ len`
En_pw= `echo ${suf_len} ${en_tmp} ${suffixal} | Rev`
# echo $en_pw
Elif [$len-eq 10]; then
Suf_len=0
En_pw= `echo ${suf_len} ${en_tmp} | Rev`
Elif [$len-eq 11]; then
Suf_len=$
En_pw= `echo ${suf_len} ${en_tmp} ${suf_len} | Rev`
Else
En_pw= `echo $en_tmp | rev`
# echo $en_pw
Fi
Encrypt_pswd=''
For i in `seq ${# en_pw} `
Do
If [- n "$(echo ${en_pw:$i-1:1} | sed-n" / ^ [Amurz]\ + $/ p ")"]; then
Str= `echo ${en_pw:$i-1:1} | awk-v letter_cnt=26'
BEGIN {
I = 0
For (I = 0; I < letter_cnt; iTunes +)
{
Ch = sprintf ("% c", iTun65)
Trans_ table [ch] = I
Trans [25muri] = ch
}
}
{
If (NF = = 1)
{
A=trans_table [$1]
Printf ("% c", trans [a])
# printf ("% d", trans_table [$1])
}
}'`
Else
Str=$ {en_pw:$i-1:1}
Fi
Encrypt_pswd=$ {encrypt_pswd} ${str}
Done
Echo "Your encrypt password is:" $encrypt_pswd
3)
# decrypt the above two encrypted strings
Echo-e "Enter your password which need to decode:"
Read passwd
Decode_pd=''
For i in `seq ${# passwd} `
Do
If [- n "$(echo ${passwd:$i-1:1} | sed-n" / ^ [Amurz]\ + $/ p ")"]; then
Str= `echo ${passwd:$i-1:1} | awk-v letter_cnt=26'
BEGIN {
I = 0
For (I = 0; I < letter_cnt; iTunes +)
{
Ch = sprintf ("% c", iTun65)
Trans_ table [ch] = I
Trans [25muri] = ch
}
}
{
If (NF = = 1)
{
A=trans_table [$1]
Printf ("% c", trans [a])
# printf ("% d", trans_table [$1])
}
}'`
Else
Str=$ {passwd:$i-1:1}
Fi
Decode_pd=$ {decode_pd} ${str}
# echo $decode_pd
Done
First_str=$ {decode_pd:0:1}
Last_str=$ {decode_pd:-1}
If [${# decode_pd}-eq 11]; then
Tmp= `echo $decode_ pd`
Redundancy=$ {tmp:-1}
Len= `expr 10-$redundancy`
De_pw= `echo ${tmp:$ {redundancy}: ${len}} | rev | tr '0123456789 ANzmGThousand thanks thanks' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Elif [${# decode_pd}-eq 13] & & [$first_str = ='$'] & & [$last_str = ='$']; then
De_pw= `echo ${decode_pd:1:11} | rev | tr '0123456789 ANzmGThumbda' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Else
De_pw= `echo $decode_pd | rev | tr '0123456789 ANzmGThumbda' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Fi
Echo "Your decode password is:" $de_pw
*)
Echo "Please enter one parameter,choose the number.
1.generate code 2.encrypt passwd 3.decode passwd 4.Nothing "
Esac
2. How to use
2.1 generate a random password (enter the password length by yourself)
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 1
Enter your password length:
eight
The password is: rK92pdQR
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 1
Enter your password length:
eleven
The password is: 584253ce235
2.2 encrypt the generated string
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 2
Enter your password which need to encrypt:
RK92pdQR
Your encrypt password is: 0KVWqcz*Ce2
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 2
Enter your password which need to encrypt:
584253ce235
Your encrypt password is: $GmzrpmGzT&G$
2.3 decrypt encrypted strings
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 3
Enter your password which need to decode:
0KVWqcz*Ce2
Your decode password is: rK92pdQR
[oracle@hyhxdb ~] $sh gen_en_de_pswd.sh 3
Enter your password which need to decode:
$GmzrpmGzT&G$
Your decode password is: 584253ce235
Second, simple application
1. Source code
Cat example.sh
#! / bin/bash
Function de_passwd ()
{
Passwd=$1
Decode_pd=''
For i in `seq ${# passwd} `
Do
If [- n "$(echo ${passwd:$i-1:1} | sed-n" / ^ [Amurz]\ + $/ p ")"]; then
Str= `echo ${passwd:$i-1:1} | awk-v letter_cnt=26'
BEGIN {
I = 0
For (I = 0; I < letter_cnt; iTunes +)
{
Ch = sprintf ("% c", iTun65)
Trans_ table [ch] = I
Trans [25muri] = ch
}
}
{
If (NF = = 1)
{
A=trans_table [$1]
Printf ("% c", trans [a])
}
}'`
Else
Str=$ {passwd:$i-1:1}
Fi
Decode_pd=$ {decode_pd} ${str}
Done
First_str=$ {decode_pd:0:1}
Last_str=$ {decode_pd:-1}
If [${# decode_pd}-eq 11]; then
Tmp= `echo $decode_ pd`
Redundancy=$ {tmp:-1}
Len= `expr 10-$redundancy`
De_pw= `echo ${tmp:$ {redundancy}: ${len}} | rev | tr '0123456789 ANzmGThousand thanks thanks' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Elif [${# decode_pd}-eq 13] & & [$first_str = ='$'] & & [$last_str = ='$']; then
De_pw= `echo ${decode_pd:1:11} | rev | tr '0123456789 ANzmGThumbda' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Else
De_pw= `echo $decode_pd | rev | tr '0123456789 ANzmGThumbda' ANzmGT#$&*0123456789' | tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy'' OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl' `
Fi
Echo $de_pw
}
Pswd= `de _ passwd 'iImzMqtcuf2' `
# echo $pswd
Sqlplus-s shpgd/$ {pswd} @ shopguide