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 <<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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 字串函式的應用及做題總結字串函式
- Lucene介紹及簡單應用
- 01 . SaltStack部署配置及簡單應用
- UDP 協議簡單瞭解及應用UDP協議
- Ubuntu下docker安裝及簡單應用UbuntuDocker
- ZwQuerySystemInformation列舉核心模組及簡單應用ORM
- geohash簡單應用-面對面匹配好友
- 對AVPlayer的一些簡單應用
- cJSON學習及簡單應用小結JSON
- EasyReact的簡單試用及和RAC的對比React
- 字串-簡單字串排序字串排序
- Bash 單命令列解釋(2)--字串操作命令列字串
- 用onsubmit做簡單表單驗證(37)MIT
- thymeleaf 簡單應用
- div 簡單應用
- ajax簡單應用
- smbclient 簡單應用client
- sqlserver拆分字串函式 及應用SQLServer字串函式
- 設計模式 | 簡單工廠模式及典型應用設計模式
- Kafka應用實戰——Kafka安裝及簡單使用Kafka
- 正規表示式理解及簡單應用舉例
- 字串-簡單字串比較字串
- vlc的應用:用vlc做單播,組播及點播伺服器伺服器
- Android應用及應用管理簡介Android
- 用Python做遊戲有多簡單?Python遊戲
- 01 . Shell詳細入門介紹及簡單應用
- Managed DirectX中的DirectShow應用(簡單Demo及原始碼)原始碼
- websoket的簡單應用Web
- npm的簡單應用NPM
- appfabric 簡單應用APP
- ORACLE簡單應用 (轉)Oracle
- 使用 HeadlessChrome 做單頁應用 SEOChrome
- jquery datatables各引數詳細說明及簡單應用jQuery
- 自己用CUDA做的簡單的加密程式加密
- C#.NET 字串加解密程式C#字串解密
- 單調棧理解及應用
- jsp的簡單應用JS
- 快應用簡單瞭解