Java/Android DES加密
2015-03-10 16:08
134 查看
DES的简单封装,适用于Java和Android。
先简单定义几个常量
加密代码:
解密代码:
调用:
返回结果:
说明:
加密密钥不能少于8位,但是就算大于8,也只会用前8位进行加密。原因是,DESKeySpec的构造函数中,有如下声明:
下载地址:
https://github.com/unicorn1990/Crypto
参考:
《深入理解Android网络编程》 陈文 郭依正 著
先简单定义几个常量
/** * DES工具类 * @author unicorn * @version @2015年3月10日下午3:45:05 */ public class DES { // 初始化向量,必须八位 private static byte[] iv = { 1, 2, 3, 4, 5, 6, 7, 8 }; static final String DES = "DES"; static final String DES_TRANSFORMATION = "DES/CBC/PKCS5Padding"; //.... }
加密代码:
/** * {@link DES}加密 * @param encryptString 要加密的内容 * @param encryptKey 加密密钥(不少于8位) * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeySpecException */ public static String encrypt(String encryptString, String encryptKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException { // 实例化IvParameterSpec对象,使用指定的初始化向量 IvParameterSpec zeroIv = new IvParameterSpec(iv); // 实例化DESKeySpec类,根据字节数组前8位来构造DESKeySpec DESKeySpec key = new DESKeySpec(encryptKey.getBytes()); // 用密匙工厂获取DES密匙工厂实例,并根据keySpec生成secretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey secretKey = keyFactory.generateSecret(key); // 创建密码器 Cipher cipher = Cipher.getInstance(DES_TRANSFORMATION); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, secretKey, zeroIv); // 执行des加密操作 byte[] encryptedData = cipher.doFinal(encryptString.getBytes()); // Base64.encode(encryptedData); // base64加密后返回 return new BASE64Encoder().encode(encryptedData); }
解密代码:
/** * {@link DES}解密 * @param decryptString 要解密的内容 * @param decryptKey 解密密钥(不少于8位) * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidKeySpecException */ public static String decrypt(String decryptString, String decryptKey) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException { // 实例化IvParameterSpec对象,使用指定的初始化向量 IvParameterSpec zeroIv = new IvParameterSpec(iv); // 实例化DESKeySpec类,根据字节数组前8位来构造DESKeySpec DESKeySpec keySpec = new DESKeySpec(decryptKey.getBytes()); // 用密匙工厂获取DES密匙工厂实例,并根据keySpec生成secretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey secretKey = keyFactory.generateSecret(keySpec); // 创建密码器 Cipher cipher = Cipher.getInstance(DES_TRANSFORMATION); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, secretKey, zeroIv); // 用Base64解密 byte[] byteMi = new BASE64Decoder().decodeBuffer(decryptString); // 执行des解密操作 byte[] encryptedData = cipher.doFinal(byteMi); return new String(encryptedData); }
调用:
public class Main { static String key="12345678"; static String text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void main(String[] args) { try { String encryptResult=DES.encrypt(text, key); String decryptResult=DES.decrypt(encryptResult, key); System.out.println("encryptResult:"+encryptResult); System.out.println("decryptResult:"+decryptResult); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } } }
返回结果:
说明:
加密密钥不能少于8位,但是就算大于8,也只会用前8位进行加密。原因是,DESKeySpec的构造函数中,有如下声明:
下载地址:
https://github.com/unicorn1990/Crypto
参考:
《深入理解Android网络编程》 陈文 郭依正 著
相关文章推荐
- android或java用DES加密解密文件
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因 .
- android(java) DES加密、解密详解
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- 关于 Des加密(Android与ios 与后台java服务器之间的加密解密)
- 关于 Des加密(Android与ios 与后台java服务器之间的加密解密)
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- Android DES加密,解决JAVA平台与android平台间加密结果不一致
- android或java用DES加密解密文件
- 关于 Des加密(Android与ios 与后台java服务器之间的加密解密)
- android或java用DES加密解密文件
- 关于Des加密(Android与ios 与后台java服务器之间的加密解密
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- Java/Android中的DES加密
- android和java平台统一的DES加密解决方案,解决加密不一样的问题
- Android和java平台 DES加密解密互通程序及其不能互通的原因
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- Android平台和java平台 DES加密解密互通程序及其不能互通的原因
- 【代码】Android和java平台 DES加密解密互通程序及其不能互通的原因