bcprov-jdk15-139支持的加密怎么调用
发布网友
发布时间:2022-04-24 15:00
我来回答
共1个回答
热心网友
时间:2022-04-27 07:12
package com.albertsong.aes;
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
/**
* @author Albert
* @version 1.0
*
*/
public class AESWithJCE {
/**
* @param args
*/
public static void main(String[] args) {
byte[] keybytes = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 };
byte[] iv = { 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x38,
0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31 };
String content ="TEST1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("Original content:");
System.out.println(content);
try {
Security.addProvider(new BouncyCastleProvider());
Key key = new SecretKeySpec(keybytes, "AES");
Cipher in = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
in.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
byte[] enc = in.doFinal(content.getBytes());
System.out.println("Encrypted Content:");
System.out.println(new String(Hex.encode(enc)));
Cipher out = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
out.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] dec = out.doFinal(enc);
System.out.println("Decrypted Content:");
System.out.println(new String(dec));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}