| | |
| | | |
| | | import android.util.Base64; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | |
| | | /** |
| | | * AES 是一种可逆加密算法,对用户的敏感信息加密处理 对原始数据进行AES加密后,在进行Base64编码转化; |
| | | */ |
| | |
| | | SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); |
| | | IvParameterSpec iv = new IvParameterSpec(Base64.decode(ivParameter, Base64.DEFAULT));// 使用CBC模式,需要一个向量iv,可增加加密算法的强度 |
| | | cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); |
| | | byte[] encrypted = cipher.doFinal(data.getBytes("utf-8")); |
| | | byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); |
| | | return Base64.encodeToString(encrypted, Base64.DEFAULT);// 此处使用BASE64做转码。 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | IvParameterSpec iv = new IvParameterSpec(Base64.decode(ivParameter, Base64.DEFAULT)); |
| | | cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv); |
| | | byte[] original = cipher.doFinal(Base64.decode(data, Base64.DEFAULT)); |
| | | return new String(original, "utf-8"); |
| | | return new String(original, StandardCharsets.UTF_8); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |