六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 58|回复: 0

java 加密解密工具类

[复制链接]

升级  94%

11

主题

11

主题

11

主题

童生

Rank: 1

积分
47
 楼主| 发表于 2013-1-27 05:41:57 | 显示全部楼层 |阅读模式
/** * @param input * @return * @throws Exception */public static String encryptData(String input) throws Exception {SecureRandom sr = new SecureRandom();byte rawKeyData[] = "ABCDEFGH".getBytes();DESKeySpec dks = new DESKeySpec(rawKeyData);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey key = keyFactory.generateSecret(dks);Cipher c = Cipher.getInstance("DES");c.init(Cipher.ENCRYPT_MODE, key, sr);byte[] cipherByte = c.doFinal(input.getBytes());String dec = new BASE64Encoder().encode(cipherByte);return dec;}/** * @param input * @return * @throws Exception */public static String decryptData(String input) throws Exception {byte[] dec = new BASE64Decoder().decodeBuffer(input);SecureRandom sr = new SecureRandom();byte rawKeyData[] = "ABCDEFGH".getBytes();DESKeySpec dks = new DESKeySpec(rawKeyData);SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");SecretKey key = keyFactory.generateSecret(dks);Cipher c = Cipher.getInstance("DES");c.init(Cipher.DECRYPT_MODE, key, sr);byte[] clearByte = c.doFinal(dec);return new String(clearByte);}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表