java代码
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
/***
* @description: 密码加密工具类
* @author: chjwdzhr
* @date:2015-06-06
* */
public class EncryptUtil{
/**
* 加密KEY: xxxxxxxx,
* 加密方式: DES/CBC/PKCS5PADDING
* */
public static String encryptStr(String originalPwd) {
byte[] result = null;
try {
String content = "xxxxxxxx";
byte[] tt = StringToByte(content);
IvParameterSpec ivinfo = new IvParameterSpec(tt);
SecretKeySpec key = new SecretKeySpec(tt, "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5PADDING");
byte[] byteContent = originalPwd.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key, ivinfo);
result = cipher.doFinal(byteContent);
} catch (NoSuchAlgorithmException nsae) {
nsae.printStackTrace();
} catch (NoSuchPaddingException nspe) {
nspe.printStackTrace();
} catch (UnsupportedEncodingException uee) {
uee.printStackTrace();
} catch (IllegalBlockSizeException ibse) {
ibse.printStackTrace();
} catch (BadPaddingException bpe) {
bpe.printStackTrace();
} catch (InvalidKeyException ike) {
ike.printStackTrace();
} catch (InvalidAlgorithmParameterException iape) {
iape.printStackTrace();
}
BASE64Encoder base64en = new BASE64Encoder();
return base64en.encode(result);
}
/**
* 字符串转byte数组
* */
private static byte[] StringToByte(String str) {
int len = str.length();
byte[] bytes = new byte[len];
for (int i = 0; i < len; i++) {
bytes[i] = (byte) (str.charAt(i));
}
return bytes;
}
public static void main(String[] args) throws NoSuchAlgorithmException{
System.out.println(EncryptUtil.encryptStr("12345678"));
}
}
python代码
# -*- coding: utf-8 -*-
from Crypto.Cipher import DES
import base64
# 加密KEY: xxxxxxxx,
# 加密方式: DES / CBC / PKCS5PADDING
PADDING = '\0'
def _pad(s): return s + (DES.block_size - len(s) % DES.block_size) * chr(DES.block_size - len(s) % DES.block_size)
def _cipher():
key = 'xxxxxxxx'
iv = 'xxxxxxxx'
return DES.new(key=key, mode=DES.MODE_CBC, IV=iv)
def encrypt_token(data):
return _cipher().encrypt(_pad(data))
def decrypt_token(data):
return _cipher().decrypt(data)
if __name__ == '__main__':
print('Python encrypt: ' + base64.b64encode(encrypt_token('12345678')))
python代码2
# coding=utf-8
'''''
加密的一方和解密的一方必须提前确定好key值
'''
from Crypto.Cipher import DES
from binascii import b2a_base64, a2b_base64
class MyCrypto():
def __init__(self, key):
self.key = key
self.iv = key
self.mode = DES.MODE_CBC # 这种模式更加安全
def encrypt(self, text):
'''''
被加密的明文长度必须是key长度的整数倍,如果不够,则用\0进行填充
转成64进制字符串,是因为避免不可见的ascii在显示的时候捣乱
'''
cryptor = DES.new(self.key, self.mode, self.key)
count = len(text)
add = DES.block_size - (count % DES.block_size)
text = text + ('\0' * add)
self.ciphertext = cryptor.encrypt(text)
return b2a_base64(self.ciphertext)
def decrypt(self, text):
'''''
解密后需注意,加密时有可能填充\0,因此要去掉右侧的\0
'''
cryptor = DES.new(self.key, self.mode, self.key)
plain_text = cryptor.decrypt(a2b_base64(text))
#去掉最后的'\x00'非常重要,要不总是不能正常显示
return plain_text.rstrip('\x00')
if __name__ == '__main__':
mc = MyCrypto('xxxxxxxx')
e = mc.encrypt('123456a?')
d = mc.decrypt(e)
print e, d
解密后密码一致
另外
repr(str)可以看到一个字符串的真实样子
(representation,表达,表示)或反引号操作符(``)可以方便地以字符串的方式获取对象的内容、类型、数值属性等信息。