/** 加密 */
public byte[] enDES(byte[] datasource, String password) {
byte[] data = null;
try {
Log.e("Haj", "data:"+datasource.length+" password:" + password.length());
SecretKeySpec key = new SecretKeySpec(getKey(password), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key);
data = cipher.doFinal(datasource);
} catch (Throwable e) {
e.printStackTrace();
}
return data;
}
/** 解密 */
public byte[] deDES(byte[] src, String password){
byte[] data = null;
try {
SecretKeySpec key = new SecretKeySpec(getKey(password), "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key);
data = cipher.doFinal(src);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return data;
}
/**
* 自定义一个key
* @param string
*/
public static byte[] getKey(String keyRule) {
Key key = null;
byte[] keyByte = keyRule.getBytes();
// 创建一个空的八位数组,默认情况下为0
byte[] byteTemp = new byte[8];
// 将用户指定的规则转换成八位数组
for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {
byteTemp[i] = keyByte[i];
}
key = new SecretKeySpec(byteTemp, "DES/ECB/NoPadding");
return key.getEncoded();
}
android des 加密解密
最新推荐文章于 2024-04-28 09:51:01 发布