Java/Android DES加密

本文介绍了一种基于Java的DES加密算法实现方法,包括加密和解密过程,并提供了完整的代码示例。该方法适用于Java及Android平台。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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网络编程》 陈文 郭依正 著

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值