引言:本文主要解决Java中用AES加密及解密,同时可通过Python脚本对Java加密后的字符进行解密的操作。
由于近期工作中用到需要使用Java对一串密钥进行加密,并且后台通过Python语言读取加密后的密钥并解密,搜索了很多文章,索性最后实现了,自己整理一下。
Java语言实现AES加密及解密
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
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 org.apache.commons.lang.StringUtils; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; /**
* <p>Title: TestAES3</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2012 All rights reserved.</p>
* <p>Company: Neusoft Corporation, China, Ltd.</p>
* @author chendch
*
*/
public class TestAES3
{
private String ALGO = "AES";
private String ALGO_MODE = "AES/CBC/NoPadding";
private String akey = "keyskeyskeyskeys";
private String aiv = "keyskeyskeyskeys";
private String padding = " ";
private int blockSize = 16; /**
* @param content
* @return password
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws UnsupportedEncodingException
* @throws InvalidAlgorithmParameterException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
public String encrypt(String content)
throws NoSuchAlgorithmException, NoSuchPaddingException, UnsupportedEncodingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException
{
String encryptContent = content;
if (encryptContent.length() < blockSize)
{
encryptContent = StringUtils.rightPad(content, blockSize, padding);
System.out.println("补位后:" + encryptContent);
} Cipher cipher = Cipher.getInstance(ALGO_MODE);
SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8"));
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] byteEncode = encryptContent.getBytes("utf-8");
byte[] byteAES = cipher.doFinal(byteEncode); String AESEncode = new String(new BASE64Encoder().encode(byteAES)); return AESEncode;
} /**
* @param content
* @return content
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws InvalidKeyException
* @throws InvalidAlgorithmParameterException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws IOException
*/
public String decrypt(String content)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException, IOException
{
byte[] byte_content = new BASE64Decoder().decodeBuffer(content); Cipher cipher = Cipher.getInstance(ALGO_MODE);
SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
IvParameterSpec ivspec = new IvParameterSpec(aiv.getBytes("utf-8")); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] byte_decode = cipher.doFinal(byte_content);
String AES_decode = new String(byte_decode, "utf-8");
return AES_decode.trim();
} /**
* @param args
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidAlgorithmParameterException
* @throws InvalidKeyException
* @throws IOException
*/
public static void main(String[] args)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException, IOException
{
TestAES3 testAES3 = new TestAES3();
String content = "chenduoc";
System.out.println("加密前:" + content); String encryptContent = testAES3.encrypt(content);
System.out.println("加密后:" + encryptContent); String decryptContent = testAES3.decrypt(encryptContent);
System.out.println("解密后:" + decryptContent);
}
}
Python实现AES加密及解密
#!/usr/bin/python
#coding=utf-8 from Crypto.Cipher import AES
import base64 BLOCK_SIZE = 16
PADDING = " "
key = "keyskeyskeyskeys"
iv = "keyskeyskeyskeys" pad_it = lambda s : s+(BLOCK_SIZE - len(s)) * PADDING def encrypt(content):
generator = AES.new(key, AES.MODE_CBC, iv)
encrypt_content = generator.encrypt(pad_it(content))
encode_content = base64.b64encode(encrypt_content) return encode_content def decrypt(content):
generator = AES.new(key, AES.MODE_CBC, iv)
decode_content = base64.b64decode(content)
decrypt_content = generator.decrypt(decode_content) return decrypt_content.rstrip(PADDING) content = "neteye"
print "加密前:",content encrypt_contnet = encrypt(content)
print "加密后:",encrypt_contnet decrypt_content = decrypt(encrypt_contnet)
print "解密后:",decrypt_content
注意点:Java中定义的key和iv变量和python中定义的必须一致,且加密的字符串需满足16位或24位等,不满足时需要进行补位,以上程序都已实现且可用。