Android开发中如何保存用户名密码在本地
来源:互联网 发布:手机h5页面制作软件 编辑:程序博客网 时间:2024/04/30 14:07
Android开发中用户登录后用户名密码需要保存到本地,方便下次免登陆打开APP。但密码肯定不能直接明文保存,所以需要简单加密,下面分享几个函数,方便给密码加解密,可随意设置加密使用的key,操作简单。
代码分享如下:
import java.security.Key;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;/** * Date: 2016/2/19 10:22 * * @author hornsey */public class DES { /** * 获取加密密钥Key * @param keyString * @return */ public static Key getKey(String keyString) { byte[] keyStringByte = keyString.getBytes(); byte[] keyByte = new byte[8]; for(int i = 0; i<keyStringByte.length && i < keyByte.length; i++) { keyByte[i] = keyStringByte[i]; } Key key = new SecretKeySpec(keyByte,"DES"); return key; } /** * 将byte数组转换成16进制String * @param bytes * @return * @throws Exception */ public static String byteArr2HexStr(byte[] bytes) throws Exception { StringBuffer sb = new StringBuffer(bytes.length*2); for(int i = 0; i < bytes.length; i++){ if((bytes[i] & 0xFF) < 0x10) sb.append("0"); sb.append(Integer.toHexString(bytes[i]&0xFF)); } return sb.toString(); } /** * 将16进制string转换成byte数组 * @param str * @return * @throws Exception */ public static byte[] hexStr2ByteArr(String str) throws Exception { byte[] bytes = str.getBytes(); int len = bytes.length; byte[] arr = new byte[len/2]; for(int i = 0; i < len; i=i+2) { String tmp = new String(bytes,i,2); arr[i/2] = (byte) Integer.parseInt(tmp,16); } return arr; } /** * 对字符串进行DES加密 * @param val 原始字符串 * @param key 加密使用的key * @return * @throws Exception */ public static String getDES(String val, String key) throws Exception { if(val == null || key == null) return null; Cipher encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE,getKey(key)); byte[] cipherByte = encryptCipher.doFinal(val.getBytes()); return byteArr2HexStr(cipherByte); } /** * 对DES加密后的16进制字符串进行解密 * @param val * @param key * @return * @throws Exception */ public static String getDESOri(String val, String key) throws Exception { if(val == null || key == null) return null; Cipher decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE,getKey(key)); byte[] originalByte = decryptCipher.doFinal(hexStr2ByteArr(val)); return new String(originalByte); } public static void main(String[] args) throws Exception{ String s = "Hello,World!"; String b = getDES(s,"hello"); System.out.println("DES(" + s +") = " + b); System.out.println("original(" + b +") = " + getDESOri(b,"hello")); }}
运行后结果如下:
DES(Hello,World!) = 1022707424afc28b80b9878d1fbdecf2
original(1022707424afc28b80b9878d1fbdecf2) = Hello,World!
参考文章:http://lichune88.blog.163.com/blog/static/63027068201102114153360/
0 1
- Android开发中如何保存用户名密码在本地
- android开发时保存用户名和密码
- 用javcscript记住用户名和密码保存在本地储存中,然后实现前端获取
- 如何在IE中设置不保存表单中的用户名和密码
- iOS应用中如何保存用户名和密码?
- Android保存用户名和密码
- Android保存用户名和密码
- Android保存用户名和密码
- Android保存用户名和密码
- Android保存用户名和密码
- Android保存用户名和密码
- Android保存用户名和密码
- Android开发基础,保存用户输入的用户名和密码
- 把用户名和密码保存在cookie中
- 把用户名和密码保存在cookie中
- Android 对用户名密码进行加密操作存储在本地
- Windows7下如何删除本地已保存的SVN用户名和密码
- PLSQL如何保存用户名和密码
- Eclipse创建Maven Project和Maven Module
- Linux如何修改主机名
- 新浪笔试题(二)
- flickr的分表全局唯一id实现方式
- wqw
- Android开发中如何保存用户名密码在本地
- leetcode笔记--Delete Node in a Linked List
- vector 的删除操作pop_back、erase效率对比 http://blog.csdn.net/efeics/article/details/8059690
- Masonry使用
- android中ListView performItemClick()方法参数说明
- IOS开发证书变成“此证书的签发者无效”解决方法
- Android数据库高手秘籍(五)——LitePal的存储操作
- Java笔记--面试题
- 旺佐茶铺港式奶茶加盟