Android
来源:互联网 发布:怎么找放单的淘宝商家 编辑:程序博客网 时间:2024/05/23 23:18
1.字符串的加密
/** * 加密 **/ private String encryptPassword(String clearText) { try { DESKeySpec keySpec = new DESKeySpec( MyConstant.PASSWORD_ENC_SECRET.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText .getBytes("UTF-8")), Base64.DEFAULT); return encrypedPwd; } catch (Exception e) { } return clearText; }
2.字符串的解密
/** * 解密 **/ private String decryptPassword(String encryptedPwd) { try { DESKeySpec keySpec = new DESKeySpec(MyConstant.PASSWORD_ENC_SECRET.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64); return new String(plainTextPwdBytes); } catch (Exception e) { } return encryptedPwd; }
备注:常量PASSWORD_ENC_SECRET可自行定义。
public class MyConstant { public static final String PASSWORD_ENC_SECRET = "mythmayor";}
3.下载Demo
字符串加密和解密Demo下载
阅读全文
0 0
- Android
- android
- Android
- android
- android
- Android
- Android
- android
- android
- android
- Android
- Android
- android!!!
- android
- android
- android
- android
- android:
- mysql如何实现oracle的start with
- 30天自制操作系统------图形处理相关
- 将postgresql数据库内容导出至sqlite数据库
- 程序方法
- 快速排序及其改进
- Android
- LintCode 97 验证二叉查找树
- STL学习笔记-迭代器
- python中 Jupter的使用 notebook
- 各大巨头纷纷出手 数据中心市场硝烟弥漫
- java解析json格式数据
- 算法系列——Linked List Cycle II
- Cookie的路径和域
- SimpleDateFormat非线程安全