对String的加密是在对byte[]的加密基础上进行的。
就是把String转回byte数组输入,然后在把byte数组转回String输出
加密:
private String getEncString(String msg) {
byte[] ming = null;
byte[] enc = null;
String mStr = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
ming = msg.getBytes("UTF-8");
enc = this.getEncCode(ming);
mStr = base64en.encode(enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
ming = null;
enc = null;
base64en = null;
}
return mStr;
}
解密:
private String getDecString(String msg) {
BASE64Decoder base64dc = new BASE64Decoder();
byte[] enc = null;
byte[] ming = null;
String decStr = "";
try {
enc = base64dc.decodeBuffer(msg);
ming = this.getDecCode(enc);
decStr = new String(ming, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
base64dc = null;
enc = null;
ming = null;
}
return decStr;
}