des加密解密数据

 时间:2026-02-14 03:36:49

1、加密数据,返回string类型的数据:

public static String encrypt(String data) {  //对string进行BASE64Encoder转换

        byte[] bt = encryptByKey(data.getBytes(), password);

        BASE64Encoder base64en = new BASE64Encoder();

        String strs = new String(base64en.encode(bt));

        return strs;

    }

des加密解密数据

2、解密数据,返回string类型的数据:

public static String decryptor(String data) throws Exception {  //对string进行BASE64Encoder转换

        sun.misc.BASE64Decoder base64en = new sun.misc.BASE64Decoder();

        byte[] bt = decrypt(base64en.decodeBuffer(data), password);

        String strs = new String(bt);

        return strs;

    }

des加密解密数据

3、加密:

private static byte[] encryptByKey(byte[] datasource, String key) {

        try{

            SecureRandom random = new SecureRandom();

            

            DESKeySpec desKey = new DESKeySpec(key.getBytes());

            //创建一个密匙工厂,然后用它把DESKeySpec转换成

            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

            SecretKey securekey = keyFactory.generateSecret(desKey);

            //Cipher对象实际完成加密操作

            Cipher cipher = Cipher.getInstance("DES");

            //用密匙初始化Cipher对象

            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);

            //现在,获取数据并加密

            //正式执行加密操作

            return cipher.doFinal(datasource);

        }catch(Throwable e){

            e.printStackTrace();

        }

        return null;

    }

des加密解密数据

4、解密:

 private static byte[] decrypt(byte[] src, String key) throws Exception {

        // DES算法要求有一个可信任的随机数源

        SecureRandom random = new SecureRandom();

        // 创建一个DESKeySpec对象

        DESKeySpec desKey = new DESKeySpec(key.getBytes());

        // 创建一个密匙工厂

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

        // 将DESKeySpec对象转换成SecretKey对象

        SecretKey securekey = keyFactory.generateSecret(desKey);

        // Cipher对象实际完成解密操作

        Cipher cipher = Cipher.getInstance("DES");

        // 用密匙初始化Cipher对象

        cipher.init(Cipher.DECRYPT_MODE, securekey, random);

        // 真正开始解密操作

        return cipher.doFinal(src);

    }

des加密解密数据

5、测试:

private static String password = "9588888888880288";

    //测试

    public static void main(String args[]) {

       //待加密内容

        String str = "12";

        

        String result = DESUtil.encrypt(str);

        

        BASE64Encoder base64en = new BASE64Encoder();

//         String strs = new String(base64en.encode(result));

        

        System.out.println("加密后:"+result);

        //直接将如上内容解密

        try {

            String decryResult = DESUtil.decryptor(result);

            System.out.println("解密后:"+new String(decryResult));

        } catch (Exception e1) {

            e1.printStackTrace();

        }

    }

des加密解密数据

6、显示结果:

des加密解密数据

  • 脸上拼音怎么拼
  • 柚子浏览器APP如何启用安全浏览
  • 数码宝贝新世纪排行榜怎么查看
  • 储备怎么解释
  • 儿童读物的重要性
  • 热门搜索
    春节短途旅游推荐 旅游专列 去泰国旅游穿什么 5月旅游推荐 闸坡旅游攻略 临海旅游景点大全 沈阳有什么旅游景点 去哪儿旅游网官网 莽山旅游 台湾旅游地图全图