bash對字串做簡單加解密及應用

ahfhuang發表於2017-11-07

一、生成密碼及加解密
1、原始碼

cat  gen_en_de_pswd.sh

 

#!/bin/bash

case $1 in
        1 )
           # 隨機產生n位數字符串
           echo -e "Enter your password length: "
           read length
           if [ $length -gt 10 ] ;then
              pw=`cat /dev/urandom | head -n 10 | md5sum | head -c $length`
              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 $length`
              echo "The password is:" $pw
           else
              echo "Your password is too short,Please try again!"
           fi
           ;;
          
        2 )
           # 對字串做簡單的加密
           echo -e "Enter your password which need to encrypt: "
           read passwd
           en_tmp=`echo $passwd|tr '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`
           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 "/^[A-Z]\+$/p")" ];then

           str=`echo ${en_pw:$i-1:1} | awk -v letter_cnt=26 '
                                   BEGIN{
                                       i = 0;
                                       for(i = 0; i < letter_cnt; i++)
                                       {
                                           ch = sprintf("%c", i+65);
                                           trans_table[ch] = i;
                                           trans[25-i]=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 )
           # 對上述2項加密的字串進行解密
           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 "/^[A-Z]\+$/p")" ];then

           str=`echo ${passwd:$i-1:1} | awk -v letter_cnt=26 '
                                   BEGIN{
                                       i = 0;
                                       for(i = 0; i < letter_cnt; i++)
                                       {
                                           ch = sprintf("%c", i+65);
                                           trans_table[ch] = i;
                                           trans[25-i]=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 '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`
             
           elif [ ${#decode_pd} -eq 13 ] && [ $first_str == '$' ] && [ $last_str == '$' ]; then
              de_pw=`echo ${decode_pd:1:11}|rev|tr '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`

           else
              de_pw=`echo $decode_pd|rev|tr '0123456789ANzmGT#$&*' '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、如何使用
2.1 生成隨機密碼(密碼長度自己輸入)
[oracle@hyhxdb ~]$ sh gen_en_de_pswd.sh 1
Enter your password length:
8
The password is: rK92pdQR
[oracle@hyhxdb ~]$ sh gen_en_de_pswd.sh 1
Enter your password length:
11
The password is: 584253ce235

2.2 對生成的字串加密
[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 對加密字串進行解密
[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

二、簡單的應用
1、原始碼
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 "/^[A-Z]\+$/p")" ];then

           str=`echo ${passwd:$i-1:1} | awk -v letter_cnt=26 '
                                   BEGIN{
                                       i = 0;
                                       for(i = 0; i < letter_cnt; i++)
                                          {
                                              ch = sprintf("%c", i+65);
                                              trans_table[ch] = i;
                                              trans[25-i]=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 '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`
             
           elif [ ${#decode_pd} -eq 13 ] && [ $first_str == '$' ] && [ $last_str == '$' ]; then
              de_pw=`echo ${decode_pd:1:11}|rev|tr '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`

           else
              de_pw=`echo $decode_pd|rev|tr '0123456789ANzmGT#$&*' 'ANzmGT#$&*0123456789'|tr 'BCDEFHIJKLMOPQRSUVWXYZabcdefghijklnopqrstuvwxy' 'OPQRSUVWXYZBCDEFHIJKLMnopqrstuvwxyabcdefghijkl'`
           fi
           echo $de_pw
}
pswd=`de_passwd 'iImzMqtcuf2'`
# echo $pswd

sqlplus -s shpgd/${pswd}@shopguide <<EOF
select sysdate from dual;
exit
EOF

2、如何使用
2.1 先對原密碼加密
[oracle@hyhxdb ~]$ sh gen_en_de_pswd.sh 2
Enter your password which need to encrypt:
shpgd123
Your encrypt password is: iImzMqtcuf2

2.2 如何應用
在原始碼中修改如下的引數即可
pswd=`de_passwd 'iImzMqtcuf2'`

執行指令碼即可

[oracle@hyhxdb ~]$ sh example.sh

SYSDATE
-------------------
2017-11-07 11:59:59

注:這只是簡單的加密,在有原始碼情況下其實沒什麼效果,後續有待完善,學習中。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30590361/viewspace-2146928/,如需轉載,請註明出處,否則將追究法律責任。

相關文章