android Base64 AES加密解密

Android Base64代碼如下:

// 加密傳入的數(shù)據(jù)是byte類型的,并非使用decode方法將原始數(shù)據(jù)轉(zhuǎn)二進(jìn)制,String類型的數(shù)據(jù) 使用 str.getBytes()即可
String str = "Hello!";
// 在這里使用的是encode方式,返回的是byte類型加密數(shù)據(jù),可使用new String轉(zhuǎn)為String類型
String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
Log.i("Test", "encode >>>" + strBase64);
        
// 這里 encodeToString 則直接將返回String類型的加密數(shù)據(jù)
String enToStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
Log.i("Test", "encodeToString >>> " + enToStr);
        
// 對(duì)base64加密后的數(shù)據(jù)進(jìn)行解密
Log.i("Test", "decode >>>" + new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT)));

Android AES代碼如下:

/**
  * @param
  * @return AES加密算法加密
  * @throws Exception
  */
 public static String encrypt(String seed, String cleartext)
   throws Exception {
  byte[] rawKey = getRawKey(seed.getBytes());
  byte[] result = encrypt(rawKey, cleartext.getBytes());
  return toHex(result);
 }
 /**
  * @param
  * @return AES加密算法加密
  * @throws Exception
  */
 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
  SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  byte[] encrypted = cipher.doFinal(clear);
  return encrypted;
 }
 private static String toHex(byte[] buf) {
  final String HEX = "0123456789ABCDEF";
  if (buf == null)
   return "";
  StringBuffer result = new StringBuffer(2 * buf.length);
  for (int i = 0; i < buf.length; i++) {
   result.append(HEX.charAt((buf[i] >> 4) & 0x0f)).append(
     HEX.charAt(buf[i] & 0x0f));
  }
  return result.toString();
 }
 /**
  * @param raw
  * @param encrypted
  * @return AES加密算法解密
  * @throws Exception
  */
 public static String decrypt(String seed, String encrypted)
   throws Exception {
  byte[] rawKey = getRawKey(seed.getBytes());
  byte[] enc = toByte(encrypted);
  byte[] result = decrypt(rawKey, enc);
  return new String(result);
 }
 private static byte[] decrypt(byte[] raw, byte[] encrypted)
   throws Exception {
  SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.DECRYPT_MODE, skeySpec);
  byte[] decrypted = cipher.doFinal(encrypted);
  return decrypted;
 }
 private static byte[] toByte(String hexString) {
  int len = hexString.length() / 2;
  byte[] result = new byte[len];
  for (int i = 0; i < len; i++)
   result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
     16).byteValue();
  return result;
 }
 private static byte[] getRawKey(byte[] seed) throws Exception {
  KeyGenerator kgen = KeyGenerator.getInstance("AES");
  SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
  sr.setSeed(seed);
  kgen.init(128, sr); // 192 and 256 bits may not be available
  SecretKey skey = kgen.generateKey();
  byte[] raw = skey.getEncoded();
  return raw;
 }

// 使用該方法
String masterPassword = "test";    
String originalText = "0123456789";  
String encryptingCode = SimpleCrypto.encrypt(masterPassword,originalText);    
Log.i("加密結(jié)果為 ",encryptingCode);    
String decryptingCode = SimpleCrypto.decrypt(masterPassword, encryptingCode);  
Log.i("解密結(jié)果",decryptingCode);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容