import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class TestSecurity {
/**
* base64 编码解码
*
* @throws Exception
*/
@Test
public void testBASE64() throws Exception {
String str = "abcd123";
BASE64Encoder encoder = new BASE64Encoder();
// 编码
String encode = encoder.encode(str.getBytes());
System.out.println("encode: " + encode);
// 解码
BASE64Decoder decoder = new BASE64Decoder();
try {
String decode = new String(decoder.decodeBuffer(encode));
System.out.println("decode: " + decode);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* DES 加解密
*
* @throws Exception
*/
@Test
public void testDES() throws Exception {
String src = "TestDES";
try {
// 生成密钥Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();
// KEY转换
DESKeySpec deSedeKeySpec = new DESKeySpec(bytesKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
Key convertSecretKey = factory.generateSecret(deSedeKeySpec);
// 加密
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, convertSecretKey);
byte[] encodeResult = cipher.doFinal(src.getBytes());
System.out.println("DESEncode :" + Hex.encodeHexString(encodeResult));
// 解密
cipher.init(Cipher.DECRYPT_MODE, convertSecretKey);
byte[] DecodeResult = cipher.doFinal(encodeResult);
System.out.println("DESDncode :" + new String(DecodeResult));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
/**
* AES加解密
*
* @throws Exception
*/
@Test
public void testAES() throws Exception {
String src = "TestAES";
try {
// 生成Key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
// keyGenerator.init(128, new
// SecureRandom("seedseedseed".getBytes()));
// 使用上面这种初始化方法可以特定种子来生成密钥,这样加密后的密文是唯一固定的。
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
// Key转换
Key key = new SecretKeySpec(keyBytes, "AES");
// 加密
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encodeResult = cipher.doFinal(src.getBytes());
System.out.println("AESencode : " + Hex.encodeHexString(encodeResult));
// 解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodeResult = cipher.doFinal(encodeResult);
System.out.println("AESdecode : " + new String(decodeResult));
} catch (NoSuchAlgorithmException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
java加解密实例
最新推荐文章于 2021-12-23 10:28:36 发布