Android 3DES 암호 화/복호화 도구 클래스
1541 단어 Android 도구 클래스
public class AndroidDes3Util {
private static final String ALGORITHM = "DESede";
/**
*
*/
private final static String encoding = "utf-8";
/**
* 3DES
*
* @param plainText
* @return
*/
public static String encode(String plainText, String secretKey) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
deskey = keyFactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
return Base64.encodeToString(encryptData, Base64.DEFAULT);
}
/**
* 3DES
*
* @param encryptText
* @param secretKey
* @return
*/
public static String decode(String encryptText, String secretKey) throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
deskey = keyFactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] decryptData = cipher.doFinal(Base64.decode(encryptText, Base64.DEFAULT));
return new String(decryptData, encoding);
}
}