javaaes详解_Java的AES加密解密详解pto.Cipher;
pto.spec.IvParameterSpec;
pto.spec.SecretKeySpec;
import org.junit.Test;
...
@Test
public void testCrossLanguageEncrypt() throws Exception{
System.out.println(encrypt());
System.out.println(desEncrypt());
}
public static String encrypt() throws Exception {
try {
String data = "Test String";
String key = "1234567812345678";java加密方式有哪些
String iv = "1234567812345678";
Cipher cipher = Instance("AES/CBC/NoPadding");
int blockSize = BlockSize();
byte[] dataBytes = Bytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new Bytes(), "AES");
IvParameterSpec ivspec = new Bytes());
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
return new sun.misc.BASE64Encoder().encode(encrypted);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static String desEncrypt() throws Exception {
try
{
String data = "2fbwW9+8vPId2/foafZq6Q==";
String key = "1234567812345678";
String iv = "1234567812345678";
byte[] encrypted1 = new sun.misc.BASE64Decoder().decodeBuffer(data); Cipher cipher = Instance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new Bytes(), "AES"); IvParameterSpec ivspec = new Bytes());
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论