对称加密

来源:互联网 发布:小米无人机淘宝加价 编辑:程序博客网 时间:2024/05/20 11:46

对称加密

        String src="";        try {            String str = "向金海10086abc@#$%^&*?";    // 待加密            String key = "12345678";            SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"),"DES");  // 生成密钥,参数是密钥字符串转化而来的字节数组,算法            Cipher cipher = Cipher.getInstance("DES");            cipher.init(Cipher.ENCRYPT_MODE,secretKey);            byte[] secretByte = cipher.doFinal(str.getBytes("UTF-8"));            src = Base64.encode(secretByte);            System.out.println("密文:"+src);        } catch (Exception e) {            e.printStackTrace();        }        try {            // 解密            String key = "12345678";            SecretKey secretKey = new SecretKeySpec(key.getBytes("UTF-8"),"DES");  // 生成密钥,参数是密钥字符串转化而来的字节数组            Cipher cipher = Cipher.getInstance("DES");            cipher.init(Cipher.DECRYPT_MODE,secretKey);            byte[] bytes = cipher.doFinal(Base64.decode(src));            System.out.println("解密:"+new String(bytes));        } catch (Exception e) {            e.printStackTrace();        }
原创粉丝点击