}
上述代码实现相对通用,可用于DES,DESede(3DES),RC2,RC4等算法都可以参照上述代码实现,只需对算法名称稍作调整即可。
测试用例代码:
package cn.itcast.testUtils;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
import com.sun.enterprise.security.auth.login.AssertedCredentials;
import cn.itcast.coderUtils.DESCoder;
public class AESCoderTest {
@Test
public void testAES() throws Exception {
String inputStr = "AES";
byte[] inputData = inputStr.getBytes();
System.out.println("原文:\t"+ inputStr);
byte[] key = DESCoder.initKey();
System.out.println("秘钥:\t" + Base64.encodeBase64String(key));
//加密
inputData = DESCoder.encrypt(inputData, key);
System.out.println("加密后:\t" + Base64.encodeBase64String(inputData));
byte[] outputDtat = DESCoder.decrypt(inputData, key);
String outputStr = new String(outputDtat);
System.out.println("解密后:\t" + outputStr);
Boolean bool = inputStr.equals(outputStr);
System.out.println(bool);
}
}
原文地址:http://blog.csdn.net/u014682413/article/details/38943611