博客
关于我
java加密解密
阅读量:792 次
发布时间:2023-01-28

本文共 3394 字,大约阅读时间需要 11 分钟。

Base64编码与DES加密解密算法

Base64编码是网络上最常见的用于传输8位字节代码的编码方式之一,可用于在HTTP环境下传递较长的标识信息。其运算原理基于计算机是8位存储的特点,通过将24位字符分解为4组6位的字符进行处理,并为每组高位补0,转换成ascii码后根据RFC2045~RFC2049表进行转换。

Base64编码示例

public class Test {    public static void main(String[] args) throws Exception {        Test test = new Test();        test.testEncrypt();        test.testDecrypt();    }    public void testEncrypt() throws Exception {        String pwd = "123456";        byte[] i = pwd.getBytes();        String input = new BASE64Encoder().encodeBuffer(i);        System.out.println(input);    }    public void testDecrypt() throws Exception {        String pwd = "MTIzNDU2";        byte[] o = new BASE64Decoder().decodeBuffer(pwd);        String output = new String(o);        System.out.println(output);    }}

输出结果

123456的Base64编码为:MTIzNDU2解密后的结果为:123456

DES加密算法

DES(Data Encryption Standard)是一种对称加密算法,加密和解密均使用相同密钥进行处理。

public class DESTest {    public static void main(String[] args) {        DESTest t = new DESTest();        String data = "123456";        String encode_data = t.encode(data);        System.out.println(encode_data);        String decode_data = t.decode(encode_data);        System.out.println(decode_data);    }    public static final String ALGORITHM_DES = "DES";    public static final String SIGN_KEY = "20170313EHR";    public String encode(String data) {        if (data == null) {            return null;        }        try {            String key = SIGN_KEY;            DESKeySpec dks = new DESKeySpec(key.getBytes());            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");            Key secretKey = keyFactory.generateSecret(dks);            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);            cipher.init(Cipher.ENCRYPT_MODE, secretKey);            byte[] bytes = cipher.doFinal(data.getBytes());            return byte2str(bytes);        } catch (Exception e) {            e.printStackTrace();            return data;        }    }    public String decode(String data) {        if (data == null) {            return null;        }        try {            String key = SIGN_KEY;            DESKeySpec dks = new DESKeySpec(key.getBytes());            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");            Key secretKey = keyFactory.generateSecret(dks);            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);            cipher.init(Cipher.DECRYPT_MODE, secretKey);            byte[] bytes = cipher.doFinal(str2byte(data.getBytes()));            return new String(bytes);        } catch (Exception e) {            e.printStackTrace();            return data;        }    }    private static byte[] str2byte(byte[] b) {        if ((b.length % 2) != 0) {            throw new IllegalArgumentException();        }        byte[] b2 = new byte[b.length / 2];        for (int n = 0; n < b.length; n += 2) {            String item = new String(b, n, 2);            b2[n / 2] = (byte) Integer.parseInt(item, 16);        }        return b2;    }    private static String byte2str(byte[] b) {        StringBuilder hs = new StringBuilder();        for (int n = 0; n < b.length; n++) {            String stmp = Integer.toHexString(b[n] & 0xFF);            if (stmp.length() == 1) {                hs.append('0');            }            hs.append(stmp);        }        return hs.toString().toUpperCase();    }}

编码与解密后的结果

  • 编码:1730D210F056D4D8123456
  • 解密:123456

转载地址:http://ejryk.baihongyu.com/

你可能感兴趣的文章
2025最全版《安全技术交底》.docx。从零基础到精通,收藏这篇就够了!
查看>>
2025最新大模型技术学习过程梳理,零基础入门到精通,收藏这篇就够了
查看>>
2025版万字长文入门大语言模型(LLM)零基础入门到精通,收藏这篇就够了
查看>>
2025版最新0基础怎么转行网络安全?零基础入门到精通,收藏这篇就够了
查看>>
2025版最新Bash Shell入门指南,零基础入门到精通,收藏这篇就够了
查看>>
2025版最新C++快速入门(适合小白)零基础入门到精通,收藏这篇就够了
查看>>
2025版最新CTF选手必藏的50个实战解题思路,零基础入门到精通,收藏这篇就够了
查看>>
2025版最新Java教程(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
2025版最新Kali Linux渗透测试教程(全面详细)零基础入门到精通,收藏这篇就够了
查看>>
2025版最新LangChain框架快速入门,零基础入门到精通,收藏这篇就够了
查看>>
2025版最新Metasploit安装使用教程(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
2025版最新Nessus 工具介绍与使用教程,零基础入门到精通,收藏这一篇就够了
查看>>
2025版最新wireshark怎么抓包?Wireshark入门指南,零基础入门到精通,收藏这篇就够了
查看>>
2025版最新一文彻底搞懂大模型 - Agent(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
2025版最新关于HW护网行动的一些知识,零基础入门到精通,收藏这篇就够了
查看>>
(建议收藏)2024最新 URL Scheme大全APP跳转界面地址更新中 ios快捷指令快捷方式链接跳转微信小程序必备autojs可用免root (可定制开发和提取URL Scheme 参数提取)
查看>>
2025版最新大模型学习路线,零基础入门到精通,收藏这篇就够了
查看>>
2025版最新大模型开发流程(非常详细)零基础入门到精通,收藏这一篇就够了
查看>>
(干货)数据分析案例--以上海二手房为例
查看>>
(大部分安卓手机通用)一加OnePlus Ace3扬声器优化教程 外放直接媲美苹果
查看>>