bash对字符串做简单加解密及应用

一、生成密码及加解密
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 <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

注:这只是简单的加密,在有源码情况下其实没什么效果,后续有待完善,学习中。


当前标题:bash对字符串做简单加解密及应用
分享网址:http://hbruida.cn/article/pidjhi.html