- A+
所属分类:编程茶楼
项目中需要做一个Des加密,牵扯好几家单位,但人家都用java来实现,只用调用相同的代码就好,而我们后台用的python。我初步想到有两种办法来解决这个问题:1、用python来实现des加密功能,保证相同的字符串通过java和python加密出来的密文相同;2、如果第一条行不通,就需要直接利用他们的加密算法,然后想办法用python来调用,以实现加密。
首先对Des算法做简单了解,见百度百科:数据加密算法。此处对具体实现过程不做讨论。
接下来看java实现Des加密的算法:
package comcoreutil; javasecurityKey; javatextSimpleDateFormat; javautilDate; javaxcryptoBadPaddingException; javaxcryptoCipher; javaxcryptoSecretKeyFactory; javaxcryptospecDESedeKeySpec; javaxcryptospecIvParameterSpec; sunmiscBASE64Decoder; sunmiscBASE64Encoder; public DesUtil private final static String encoding ; private static String getIVTodaynew SimpleDateFormatformatnew Date; private static String getIVYesterdaynew SimpleDateFormatformatnew Datenew DategetTime; private static String getIVTomorrownew SimpleDateFormatformatnew Datenew DategetTime; private static String getSecretKeyTodaygetIVToday; private static String getSecretKeyYesterdaygetIVYesterday; private static String getSecretKeyTomorrowgetIVTomorrow; param plainText public static String encodeString plainTextKey deskey null; byteencryptData null; String secretKey getSecretKeyToday; DESedeKeySpec spec new DESedeKeySpecsecretKeygetBytes; SecretKeyFactory keyfactory SecretKeyFactorygetInstance; deskey keyfactorygenerateSecretspec; Cipher cipher CiphergetInstance; IvParameterSpec ips new IvParameterSpecgetIVTodaygetBytes; cipherinitCipherENCRYPT_MODEdeskeyips; encryptData cipherdoFinalplainTextgetBytesencoding; BASE64Encoder base64Encoder new BASE64Encoder; base64EncoderencodeencryptData; catch eeprintStackTrace; ; param data public static String decodeString dataKey deskey null; String secretKey getSecretKeyToday; DESedeKeySpec spec new DESedeKeySpecsecretKeygetBytes; SecretKeyFactory keyfactory SecretKeyFactorygetInstance; deskey keyfactorygenerateSecretspec; Cipher deCipher CiphergetInstance; IvParameterSpec ips new IvParameterSpecgetIVTodaygetBytes; deCipherinitCipherDECRYPT_MODEdeskeyips; BASE64Decoder base64Decoder new BASE64Decoder; bytepasByte null; pasBytedeCipherdoFinalbase64DecoderdecodeBufferdata; catchBadPaddingException esecretKey getSecretKeyYesterday; spec new DESedeKeySpecsecretKeygetBytes; deskey keyfactorygenerateSecretspec; ips new IvParameterSpecgetIVYesterdaygetBytes; deCipherinitCipherDECRYPT_MODEdeskeyips; pasBytedeCipherdoFinalbase64DecoderdecodeBufferdata; catchBadPaddingException e1secretKey getSecretKeyTomorrow; spec new DESedeKeySpecsecretKeygetBytes; deskey keyfactorygenerateSecretspec; ips new IvParameterSpecgetIVTomorrowgetBytes; deCipherinitCipherDECRYPT_MODEdeskeyips; pasBytedeCipherdoFinalbase64DecoderdecodeBufferdata; new StringpasByte; catch e; public static void mainStringargsSystemoutprintlnDesUtilencode;
以上代码中,最为关键的就是了解Cipher类,3.3.5 Cipher类。
python实现Des加密的基本思路:1、自己重写Des加密算法 python实现DES加密 2、调用pyDes:
pyDes data k triple_desCBCpadpadmodePAD_PKCS5d kencryptdatad kdecryptdkdecryptdpadmodePAD_PKCS5data
其中triple_des中要求秘钥为16位或24位,des方法需要8位。
但两种形式中,可传递的参数只有需加密字符串和秘钥(长度为8的倍数),并没有java中那个随机向量的概念。由于没有时间研究Cipher的源码,所以用python来实现以上java算法算是泡汤了。下一篇文章说明如何使用JPype实现python调用java代码。
- 我的微信
- 这是我的微信扫一扫
- 我的微信公众号
- 我的微信公众号扫一扫