public static String decodeToString(String encoded) { try { return new String(decode(encoded),"utf-8") ; } catch (UnsupportedEncodingException e) { throw new MRuntimeException(encoded,e); } }
public static String encode(byte[] raw) { return encode(raw, 0, raw.length); }
public static String encode(byte[] raw, int offset, int length) { int lastIndex = Math.min(raw.length, offset + length); StringBuilder sb = new StringBuilder( ((lastIndex - offset) / 3 + 1) * 4); for (int i = offset; i < lastIndex; i += 3) { sb.append(encodeBlock(raw, i, lastIndex)); } return sb.toString(); }
protected static char[] encodeBlock(byte[] raw, int offset, int lastIndex) { int block = 0; int slack = lastIndex - offset - 1; int end = slack < 2 ? slack : 2; for (int i = 0; i <= end; i++) { byte b = raw[offset + i]; int neuter = b >= 0 ? ((int) (b)) : b + 256; block += neuter << 8 * (2 - i); } char[] base64 = new char[4]; for (int i = 0; i < 4; i++) { int sixbit = block >>> 6 * (3 - i) & 0x3f; base64[i] = getChar(sixbit); } if (slack < 1) { base64[2] = '='; } if (slack < 2) { base64[3] = '='; } return base64; }
public static byte[] decode(String base64) { if (MString.isEmpty(base64)) { return new byte[0]; } int pad = 0; for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) { pad++; } int length = (base64.length() * 6) / 8 - pad; byte[] raw = new byte[length]; int rawindex = 0; for (int i = 0; i < base64.length(); i += 4) { int block = getValue(base64.charAt(i)) << 18; block += getValue(base64.charAt(i + 1)) << 12; block += getValue(base64.charAt(i + 2)) << 6; block += getValue(base64.charAt(i + 3)); for (int j = 0; j < 3 && rawindex + j < raw.length; j++) { raw[rawindex + j] = (byte)(block >> 8 * (2 - j) & 0xff); } rawindex += 3; } return raw; }
public static String encode(String value) { if (value == null) return ""; return encode(value.getBytes()); }
public static String decrypt(String strEncrypted,String strKey) throws Exception{ return new String(decrypt(Base64.decode(strEncrypted), strKey), "utf-8"); }
public static String objectToString(Object o) { if (o == null) { return null; } ByteArrayOutputStream ubaos = new ByteArrayOutputStream( 32000); try { ObjectOutputStream os = new ObjectOutputStream(ubaos); os.flush(); os.writeObject(o); os.flush(); } catch (Exception e) { log.e(e, e); } return encode(ubaos.toByteArray(), 0, ubaos.size()); }
public static String decrypt(String strEncrypted,String strKey) throws Exception{ return new String(decrypt(Base64.decode(strEncrypted), strKey), "utf-8"); }
public static String encrypt(String strClearText,String strKey) throws Exception{ return Base64.encode(encrypt(strClearText.getBytes("utf-8"), strKey)); }
byte[] b = Base64.decode(aesEncKey);
public static String encrypt(String strClearText,String strKey) throws Exception{ return Base64.encode(encrypt(strClearText.getBytes("utf-8"), strKey)); }
byte[] b = Base64.decode(aesEncKey);
CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding); out.setInt("AesLength", aesLength); out.setString("AesKey", Base64.encode(aesKeyEncoded)); return out;
String data = a.getAttribute("encoding"); if ("base64".equals(data)) { String value = new String( Base64.decode(a.getAttribute("string")) ); attr.set(to, value); } else {
else { a.setAttribute("encoding", "base64"); a.setAttribute("string", Base64.encode( (String)value));
CryptUtil.prepareCipherOut(key, out, getName(), stringEncoding); out.setInt("AesLength", aesLength); out.setString("AesKey", Base64.encode(aesKeyEncoded)); return out;