如此用法:传一个字符串进去
password = password.toLowerCase();
String _password = Encrypter.encrypter(password);
_password = TextUtils.isEmpty(_password) ? "" : _password;
Encrypter此类:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public class Encrypter {
public static final String SECRET_KEY = "PkmJygVfrDxsDeeD";
/*
* Encrypter method
*
* @param password, public text need to be encrypted
*
* @return encrypt text
*/
public static String encrypter(String str) {
String string = null;
try {
string = AES128_ECB_HEX.encode(str.getBytes(), 0, SECRET_KEY.getBytes(), 0);
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return string;
}
/*
* Decrypter method
*
* @param password, secret text need to be decrypted
*
* @return public text
*/
public static String decrypter(String str) {
String string = "";
try {
string = new String(AES128_ECB_HEX.decode(str, SECRET_KEY.getBytes(), 0));
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return string;
}
}
AES128_ECB_HEX此类:
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
public final class AES128_ECB_HEX {
/*
* encode method
*
* @param btPlain, public text need to be encrypted
*
* @param iLen, the length of public text锟斤拷it equals the length of btPlain
* when is 0
*
* @param btKey, secret key
*
* @param iKeyLen, length of secret key锟斤拷it equals the length of btKey when
* is 0
*/
public static String encode(byte[] btPlain, int iLen, byte[] btKey, int iKeyLen)
throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
return HEX.encode(AES128_ECB.encode(btPlain, iLen, btKey, iKeyLen), 0);
}
/*
* decode method
*
* @param stHex, hex cryptograph
*
* @param btKey, secret key
*
* @param iKeyLen, length of secret key锟斤拷it equals the length of btKey when
* is 0
*/
public static byte[] decode(String stHex, byte[] btKey, int iKeyLen)
throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
return AES128_ECB.decode(HEX.decode(stHex), 0, btKey, iKeyLen);
}
private AES128_ECB_HEX() {}
}
此类HEX:
public class HEX {
/*
* encode method
*
* @param btData, public text need to be hex
*
* @param iLen, the length of public text锟斤拷it equals the length of btData
* when is 0
*/
public static String encode(byte[] btData, int iLen) {
String l_stTmp = null;
StringBuffer l_stHex = new StringBuffer();
int ii;
if (btData == null) {
return null;
}
if ((iLen <= 0) || (iLen > btData.length)) {
iLen = btData.length;
}
for (ii = 0; ii < iLen; ii++) {
l_stTmp = Integer.toHexString(btData[ii] & 0xFF);
if (l_stTmp.length() == 1) {
l_stTmp = "0" + l_stTmp;
}
l_stHex.append(l_stTmp.toUpperCase());
}
return l_stHex.toString();
}
/*
* decode method
*
* @return null if conversion failed
*/
public static byte[] decode(String stData) {
byte[] l_btData = null;
byte[] l_btTmp = null;
String l_stData = null;
int l_iLen;
int ii;
int jj;
int kk;
char l_cTmp;
if (stData == null) {
return null;
}
l_iLen = stData.length();
if ((l_iLen % 2) != 0) {
return null;
}
l_stData = stData.toUpperCase();
for (ii = 0; ii < l_iLen; ii++) {
l_cTmp = l_stData.charAt(ii);
if (!((('0' <= l_cTmp) && (l_cTmp <= '9')) || (('A' <= l_cTmp) && (l_cTmp <= 'F')))) {
return null;
}
}
l_iLen /= 2;
l_btData = new byte[l_iLen];
l_btTmp = new byte[2];
for (ii = 0, jj = 0, kk = 0; ii < l_iLen; ii++) {
l_btTmp[0] = (byte) (l_stData.charAt(jj++));
l_btTmp[1] = (byte) (l_stData.charAt(jj++));
for (kk = 0; kk < 2; kk++) {
if (('A' <= l_btTmp[kk]) && (l_btTmp[kk] <= 'F')) {
l_btTmp[kk] -= 55;
} else {
l_btTmp[kk] -= 48;
}
}
l_btData[ii] = (byte) ((l_btTmp[0] << 4) | l_btTmp[1]);
}
return l_btData;
}
private HEX() {}
}
此类:AES128_ECB
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;
public final class AES128_ECB {
private static final int AES_128_KEY_LEN = 16; // 128 bit
/*
* encode method
*
* @param btPlain, public text need to be encrypted
*
* @param iLen, the length of public text锟斤拷it equals the length of btPlain
* when is 0
*
* @param btKey, secret key
*
* @param iKeyLen, length of secret key锟斤拷it equals the length of btKey when
* is 0
*/
public static byte[] encode(byte[] btPlain, int iLen, byte[] btKey, int iKeyLen)
throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
return encode_decode(btPlain, iLen, btKey, iKeyLen, 0);
}
/*
* decode method
*
* @param btCipher, secret text
*
* @param iLen, the length of secret text锟斤拷it equals the length of btCipher
* when is 0
*
* @param btKey, secret key
*
* @param iKeyLen, length of secret key锟斤拷it equals the length of btKey when
* is 0
*/
public static byte[] decode(byte[] btCipher, int iLen, byte[] btKey, int iKeyLen)
throws BadPaddingException, IllegalBlockSizeException, InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
return encode_decode(btCipher, iLen, btKey, iKeyLen, 1);
}
private static byte[] encode_decode(byte[] btData, int iLen, byte[] btKey, int iKeyLen,
int iFlag) throws BadPaddingException, IllegalBlockSizeException,
InvalidKeyException,
NoSuchAlgorithmException, NoSuchPaddingException {
int ii;
int l_iMode;
byte[] l_btKey = null;
Cipher l_oCipher = null;
if ((btData == null) || (btKey == null)) {
return null;
}
if ((iLen <= 0) || (iLen > btData.length)) {
iLen = btData.length;
}
if ((iKeyLen <= 0) || (iKeyLen > btKey.length)) {
iKeyLen = btKey.length;
}
if (iKeyLen > AES_128_KEY_LEN) {
// 16 Bytes
iKeyLen = AES_128_KEY_LEN; // 16 Bytes
}
l_btKey = new byte[AES_128_KEY_LEN]; // 16 Bytes
for (ii = 0; ii < AES_128_KEY_LEN; ii++) {
l_btKey[ii] = (byte) 0x00;
}
for (ii = 0; ii < iKeyLen; ii++) {
l_btKey[ii] = btKey[ii];
}
l_oCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
if (iFlag == 0) {
l_iMode = Cipher.ENCRYPT_MODE;
} else {
l_iMode = Cipher.DECRYPT_MODE;
}
l_oCipher.init(l_iMode, new SecretKeySpec(l_btKey, 0, AES_128_KEY_LEN, "AES"));
return l_oCipher.doFinal(btData, 0, iLen);
}
private AES128_ECB() {}
}
解密
password = Encrypter.decrypter(password);
以上所有都是加解密用到的类,自己也不懂很多代码为什么这么写