使用rsa公钥对字符串加密并通过私钥解密全过程

0.生成公钥私钥对
ssh-keygen -t rsa -N "" -f ${HOME}/.ssh/id_getAuthToken_rsa

1:把当前公钥转为pem格式
python ./convert.py <(awk '{ print $1" "$2" "$3; }' < ${HOME}/.ssh/id_getAuthToken_rsa.pub )|sed 2,3d >${HOME}/.ssh/id_getAuthToken_rsa.pem.pub
2.使用pem格式加密输入串
echo -e "CLIENTID=${CLIENTID}\nCLIENTSECRET=${CLIENTSECRET}\nUSERNAME=${USERNAME}\nUSERPASSWORD=${USERPASSWORD}" | openssl rsautl -encrypt -pubin -inkey ${HOME}/.ssh/id_getAuthToken_rsa.pem
.pub -ssl > ${HOME}/.getAuthTokenEnc
3.通过私钥解密
[sasuser8@sasf04 ~]$ USERPARAMETERSARRAY=(`openssl rsautl -decrypt -inkey ${HOME}/.ssh/id_getAuthToken_rsa -in ${HOME}/.getAuthTokenEnc`)
[sasuser8@sasf04 ~]$ echo ${USERPARAMETERSARRAY[@]}
CLIENTID=abc CLIENTSECRET=abc123 USERNAME=cas USERPASSWORD=Sas123

其中convert.py请参考下面链接下载
http://git.oschina.net/anyjack/codes/kmeiawd3v9rg6pnysoh1b15

如果是openssl v5.6,则可以使用下面命令转为pem格式,而无需使用convert.py来转换
sh-keygen -f ${HOME}/.ssh/id_getAuthToken_rsa.pub -e -m PKCS8 > ${HOME}/.ssh/id_getAuthToken_rsa.pem.pub



你可能感兴趣的:(Python)