您的位置:首页 > 移动开发 > Android开发

Java/Android DES加密

2015-03-10 16:08 134 查看
DES的简单封装,适用于Java和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网络编程》 陈文 郭依正 著
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: