JAVA加密算法

一、AES

复制代码
高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥,具体的加密流程如下:
加密:明文P通过AES加密函数和密匙K进行加密,得到秘文C
解密:秘文C通过AES解密函数和密匙K进行解密,得到明文P
密钥,必须是16字节的长度,代码如下:
public class AESExample {

    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, AES!";  // 要加密的明文

        String key = "ThisIsASecretKey";  // 密钥,必须是16字节的长度

        byte[] encrypted = encrypt(plaintext, key);
        System.out.println("加密后的结果:" + encodeBase64(encrypted));

        String decrypted = decrypt(encrypted, key);
        System.out.println("解密后的结果:" + decrypted);
    }

    public static byte[] encrypt(String plaintext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
    }

    public static String decrypt(byte[] ciphertext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(ciphertext);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static String encodeBase64(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }
}

二、RSA

复制代码
 RSA加密算法是一种典型的非对称加密算法,它基于大数的因式分解数学难题,它也是应用最广泛的非对称加密算法。非对称加密是通过两个密钥(公钥-私钥)来实现对数据的加密和解密的。公钥用于加密,私钥用于解密,具体的加密流程如下:
加密方:通过公钥和加密函数对明文进行加密
解密方:通过解密函数和私钥对密文进行解密
public class RSAExample {

    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, RSA!";  // 要加密的明文

        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        byte[] encrypted = encrypt(plaintext, publicKey);
        System.out.println("加密后的结果:" + encodeBase64(encrypted));

        String decrypted = decrypt(encrypted, privateKey);
        System.out.println("解密后的结果:" + decrypted);
    }

    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048, new SecureRandom());
        return keyPairGenerator.generateKeyPair();
    }

    public static byte[] encrypt(String plaintext, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(plaintext.getBytes());
    }

    public static String decrypt(byte[] ciphertext, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(ciphertext);
        return new String(decryptedBytes);
    }

    public static String encodeBase64(byte[] bytes) {
        return Base64.getEncoder().encodeToString(bytes);
    }
}

三、CRC

复制代码
循环冗余校验(Cyclic Redundancy Check, CRC)是一种根据网络数据包或电脑文件等数据产生简短固定位数校验码的一种散列函数,主要用来检测或校验数据传输或者保存后可能出现的错误。它是利用除法及余数的原理来作错误侦测的。
public class CRCExample {

    public static void main(String[] args) {
        String data = "Hello, CRC!";  // 要计算CRC的数据

        long crcValue = calculateCRC(data);
        System.out.println("CRC校验值:" + crcValue);
    }

    public static long calculateCRC(String data) {
        CRC32 crc32 = new CRC32();
        crc32.update(data.getBytes());
        return crc32.getValue();
    }
}

四、MD5

复制代码
MD5常常作为文件的签名出现,我们在下载文件的时候,常常会看到文件页面上附带一个扩展名为.MD5 的文本或者一行字符,这行字符就是就是把整个文件当作原数据通过 MD5 计算后的值,我们下载文件后,可以用检查文件 MD5 信息的软件对下载到的文件在进行一次计算。两次结果对比就可以确保下载到文件的准确性。 另一种常见用途就是网站敏感信息加密,比如用户名密码,支付签名等等。随着 https 技术的普及,现在的网站广泛采用前台明文传输到后台,MD5 加密(使用偏移量)的方式保护敏感数据保护站点和数据安全
public class MD5Example {

    public static void main(String[] args) {
        String plaintext = "Hello, MD5!";  // 要加密的明文

        String encrypted = encryptMD5(plaintext);
        System.out.println("加密后的结果:" + encrypted);
    }

    public static String encryptMD5(String plaintext) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(plaintext.getBytes());
            BigInteger hashNum = new BigInteger(1, hashBytes);
            String encrypted = hashNum.toString(16);
            while (encrypted.length() < 32) {
                encrypted = "0" + encrypted;
            }
            return encrypted;
        } catch (Exception e) {
            // 处理异常
            e.printStackTrace();
        }
        return null;
    }
}
相关推荐
Nerd Nirvana10 天前
Cryptographic algorithms—(Green-Book)加密算法
通讯协议·加密算法·dlms协议·电网标准·cryptographic
dazhong201218 天前
Spring Boot 开发 -- 常用加密算法简介(一)
spring boot·后端·加密算法
Amd7942 个月前
Rabbit加密算法:性能与安全的完美结合
安全分析·加密算法·对称加密·密钥调度·流密码·实际应用·rabbit加密
胡斌附体3 个月前
软考之零碎片段记录(十一)+复习巩固(六)
软考·加密算法·关系模式·各种权·web防火墙
项目中心3 个月前
物联网实战--驱动篇之(五)TEA和AES加密算法
物联网·aes·加密算法·tea
吉量*3 个月前
.NET DES加密算法实现
网络·c#·des·.net·加密算法·文件加密·传输加密
潇洒大舅哥3 个月前
安全算法 - 加密算法
加密算法
Amd7944 个月前
AES算法:加密通信的新选择
信息安全·数据安全·加密算法·对称加密·数据保护·aes算法·密钥管理
初心不忘产学研4 个月前
加密算法 —— 有哪些容易入门且实用的算法?
人工智能·深度学习·算法·机器学习·哈希算法·加密算法·哈希散列