Pages

2013年8月13日 星期二

在Android 使用 base64 的 class

這次要在Android 上跑一個AES的範例,出現一些問題
在Android 上無法使用
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
解決辦法是要import 不同的函式
首先 先下載:apache.commons的元件
我抓的版本為 :commons-codec-1.8-bin.zip (反正抓最新版就好)
在以下路徑 匯入函式庫:
project > Build Path > Add External Archives >
匯入commons-codec-1.8.jar
接著就改成import下面的函式
import org.apache.commons.codec.binary.Base64;

加密的寫法:
String outputString = new String(Base64.encodeBase64(inputString.getBytes()));

解密的寫法:
byte[] data = Base64.decodeBase64(msg.getBytes());
byte[] result = getCipher(CryptMode.DECODE).doFinal(data);
參考: base64 AES範例

沒有留言:

張貼留言

 
 
Blogger Templates