温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-09 10:43:53 来源:ITPUB博客 阅读:169 作者:ahfhuang 栏目:建站服务器

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI