android AES256Util class
public class AES256Util { public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; public static String AES_Encode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { byte[] textBytes = str.getBytes("UTF-8"); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec); return Base64.encodeToString(cipher.doFinal(textBytes), 0); } public static String AES_Decode(String str, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { byte[] textBytes =Base64.decode(str,0); //byte[] textBytes = str.getBytes("UTF-8"); AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes); SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec); return new String(cipher.doFinal(textBytes), "UTF-8"); } }
php function
$AES256KEY = "12345678123456781234567812345678"; function AES_Encode($plain_text) { global $AES256KEY; return base64_encode(openssl_encrypt($plain_text, "AES-256-CBC", $AES256KEY, true, str_repeat(chr(0), 16))); } function AES_Decode($base64_text) { global $AES256KEY; return openssl_decrypt(base64_decode($base64_text), "AES-256-CBC", $AES256KEY, true, str_repeat(chr(0), 16)); }
webview 로 데이터 전달
try { encoeText = Uri.encode(AES256Util.AES_Encode(palinText, aes256key)); } catch (Exception e) {} String postdata = "enText="+encodeText; webView.postUrl(URL, postdata.getBytes());
webview 로 전달된 데이터가 복호화가 안된다면 전달하는 값에 +가 포함되어 그럴수도 있더라... 그래서 Uri.encode() 로 전달했다.
java 와 php 간의 암호화시에 유용하게 사용하였습니다. 감사합니다~
답글삭제