package com.baidu.wallet.bdwallet.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
public
class
Test {
private
static
final
String AES="AES";
private
static
final
String UTF8="UTF-8";
private
static
byte[] encrypt(String content, String pkey) throws DecoderException {
try
{
String private_key=pkey;
byte[] encodeFormat=null;
try
{
encodeFormat = Hex.decodeHex(private_key.toCharArray());
}
catch
(DecoderException e) {
e.printStackTrace();
}
SecretKeySpec key =
new
SecretKeySpec(encodeFormat, AES);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
byte[] byteContent = content.getBytes(UTF8);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
return
result;
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
e.printStackTrace();
}
catch
(UnsupportedEncodingException e) {
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
e.printStackTrace();
}
catch
(BadPaddingException e) {
e.printStackTrace();
}
return
null;
}
private
static
byte[] decrypt(String contents, String password) throws DecoderException {
try
{
byte[]content = Hex.decodeHex(contents.toCharArray());
byte[] encodeFormat = Hex.decodeHex(password.toCharArray());
SecretKeySpec key =
new
SecretKeySpec(encodeFormat, AES);
Cipher cipher = Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
return
result;
}
catch
(NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch
(NoSuchPaddingException e) {
e.printStackTrace();
}
catch
(InvalidKeyException e) {
e.printStackTrace();
}
catch
(IllegalBlockSizeException e) {
e.printStackTrace();
}
catch
(BadPaddingException e) {
e.printStackTrace();
}
return
null;
}
public
static
String encryption(String context,String private_key) throws DecoderException{
byte[] encryptResult = encrypt(context, private_key);
String encryptResultStr = Hex.encodeHexString(encryptResult);
return
encryptResultStr;
}
public
static
String decryption(String context,String private_key) throws DecoderException, UnsupportedEncodingException{
byte[] decryptResult = decrypt(context, private_key);
String result =
new
String(decryptResult, UTF8);
return
result;
}
public
static
void main(String[] args) throws UnsupportedEncodingException, DecoderException {
String content = "123456787654321";
String password = "这个值一般都是给定的,双发都知道";
System.out.println("加密前:" + content);
String encryptResultStr = encryption(content, password);
System.out.println("加密后:" + encryptResultStr);
String result = decryption(encryptResultStr, password);
System.out.println("解密后:" + result);
}
}