1. Base64
1.1 实现方式
private static String srcTest = "demo test";
@Test
public void testBase64() throws IOException {
//使用jdk自带的方式实现Base64加解密
System.out.println("====使用jdk自带的方式====");
BASE64Encoder encode = new BASE64Encoder();
String encodeText = encode.encode(srcTest.getBytes());
System.out.println("Base64加密后的内容: "+encodeText);
BASE64Decoder decode = new BASE64Decoder();
String decodeText = new String(decode.decodeBuffer(encodeText));
System.out.println("Base64解密后的内容: "+decodeText+"\n");
//使用Apache commons-codec Base64实现加解密
System.out.println("====使用Apache commons-codec的方式====");
String encodeText02 = new String(Base64.encodeBase64(srcTest.getBytes()));
System.out.println("Base64解密后的内容: "+encodeText02);
System.out.println("Base64解密后的内容: "+new String(Base64.decodeBase64(encodeText02))+"\n");
//使用BouncyCastle的方式实现Base64加解密
System.out.println("====使用BouncyCastle的方式====");
String encodeText03 = new String(org.bouncycastle.util.encoders.Base64.encode(srcTest.getBytes()));
System.out.println("Base64解密后的内容: "+encodeText03);
System.out.println("Base64解密后的内容: "+new String(Base64.decodeBase64(encodeText03))+"\n");
}
1.2 测试结果
2. AES对称加密
2.1 应用场景
2.2 具体实现
@Test
public void testAES() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
//构建密钥生成器
KeyGenerator keygen = KeyGenerator.getInstance("AES");
//初始化密钥生成器
keygen.init(128);
//产生原始对称密钥
SecretKey secretkey = keygen.generateKey();
//获得原始对称密钥的字节数组
byte[] keybytes = secretkey.getEncoded();
//根据字节数组生成AES密钥
Key key = new SecretKeySpec(keybytes, "AES");
//根据指定算法AES生成密码器
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
//初始化密码器
cipher.init(Cipher.ENCRYPT_MODE, key);
//获取加密内容的字节数组,进行加密
byte[] encoderesult = cipher.doFinal(srcTest.getBytes());
//因为加密后的内容是二进制的,这里转换为16进制进行显示
System.out.println("AES加密后的16进制密文为: "+Hex.encodeHexString(encoderesult));
//解密
cipher.init(Cipher.DECRYPT_MODE, key);
String decoderesult = new String(cipher.doFinal(encoderesult));
System.out.println("AES解密后的内容为: "+decoderesult);
}
2.3 测试结果
3. 非对称RSA加密算法
3.1 应用场景
3.2 具体实现
@Test
public void testRSA() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
//初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
System.out.println("Public key: "+Base64.encodeBase64String(rsaPublicKey.getEncoded()));
System.out.println("Private key: "+Base64.encodeBase64String(rsaPrivateKey.getEncoded()));
//私钥加密
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("rsa");
PrivateKey privateKey = keyFactory.generatePrivate(encodedKeySpec);
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] encrptresult = cipher.doFinal(srcTest.getBytes());
System.out.println("私钥加密后的内容: "+Base64.encodeBase64String(encrptresult));
//公钥解密
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded());
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] decryptresult = cipher.doFinal(encrptresult);
System.out.println("公钥解密后的内容: "+new String(decryptresult));
}
3.3 测试结果
4. MD加密算法
4.1 应用场景
4.2 具体实现
@Test
public void testMD5() throws NoSuchAlgorithmException{
MessageDigest mdDigest = MessageDigest.getInstance("md5");
byte[] result = mdDigest.digest(srcTest.getBytes());
System.out.println("md5加密后的内容为: "+Hex.encodeHexString(result));
}
4.3 测试结果
5. SHA加密算法
5.1 应用场景
5.2 具体实现
@Test
public void testSHA() throws NoSuchAlgorithmException{
MessageDigest md = MessageDigest.getInstance("sha");
md.update(srcTest.getBytes());
byte[] result = md.digest();
System.out.println("sha加密后的内容为: "+Hex.encodeHexString(result));
}
5.3 测试结果
6. MAC加密算法
6.1 概念
MAC(Message Authentication Code,消息认证码算法)是含有密钥的散列函数算法,兼容了MD和SHA算法的特性,并在此基础上加入了密钥。消息的散列值由只有通信双方知道的秘密密钥K来控制,因次,我们也常把MAC称为HMAC(keyed-Hash Message Authentication Code)
6.2 应用场景
6.3 具体实现
@Test
public void testMAC() throws NoSuchAlgorithmException, InvalidKeyException{
//初始化密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
//生成密钥
SecretKey secretKey = keyGenerator.generateKey();
//获得密钥
byte[] key = secretKey.getEncoded();
//还原密钥
SecretKey restorekey = new SecretKeySpec(key, "HmacMD5");
//实例化MAC
Mac mac = Mac.getInstance(restorekey.getAlgorithm());
//初始化MAC
mac.init(restorekey);
//执行mac加密
byte[] macresult = mac.doFinal(srcTest.getBytes());
System.out.println("MAC加密后的内容为: "+Hex.encodeHexString(macresult));
}
6.4 测试结果
总结:
在进行接口测试的过程中,难免会遇到需要对参数的加密,所以有必要了解加密的一些方式,才能更加方便的构造数据。