byte[] ct = null;
try {
IPad padding = PadFactory.getInstance("PKCS7");
padding.init(16);
IMode mode = ModeFactory.getInstance("CBC", "AES", 16);
Map attributes = new HashMap();
byte[] pt1 = source;
byte[] pad = padding.pad(pt1, 0, pt1.length);
byte[] pt = null;
// 判断是否要补空
if (pad.length == 16) {
pt = new byte[pt1.length];
System.arraycopy(pt1, 0, pt, 0, pt1.length);
} else {
pt = new byte[pt1.length + pad.length];
System.arraycopy(pt1, 0, pt, 0, pt1.length);
System.arraycopy(pad, 0, pt, pt1.length, pad.length);
}
ct = new byte[pt.length];
byte[] iv = i_iv;
byte[] key = mykey;
attributes.put(IMode.KEY_MATERIAL, key);
attributes.put(IMode.CIPHER_BLOCK_SIZE, new Integer(16));
attributes.put(IMode.IV, iv);
attributes.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
mode.init(attributes);
for (int i = 0; i + 16 <= pt.length; i += 16) {
mode.update(pt, i, ct, i);
}
} catch (Exception e) {
e.printStackTrace();
}