java-加密算法

在这里插入图片描述

Java的加密算法

对称加密算法:AES(Advanced Encryption Standard)

定义
AES是一种广泛使用的对称加密算法,采用分组密码体制,每个分组数据的长度为128位(16个字节),密钥长度可以是128位、192位或256位。AES支持多种加密模式,常用的有ECB、CBC等模式。

特点

  • 对称加密,即加密和解密使用相同的密钥。
  • 速度快,适合大量数据的加密。
  • 安全性高,是目前最常用的对称加密算法之一。

Java实现示例

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {
    private static final String AES = "AES";

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(key, AES);
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        return cipher.doFinal(data);
    }

    public static void main(String[] args) throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(AES);
        keyGen.init(128); // 初始化密钥长度为128位
        SecretKey secretKey = keyGen.generateKey(); // 生成密钥
        byte[] key = secretKey.getEncoded(); // 获取密钥的字节数组
        String originalString = "Hello, World!"; // 原始字符串
        byte[] originalBytes = originalString.getBytes(); // 将字符串转换为字节数组
        byte[] encryptedBytes = encrypt(originalBytes, key); // 加密
        byte[] decryptedBytes = decrypt(encryptedBytes, key); // 解密
        System.out.println("原始字符串: " + originalString);
        System.out.println("加密后: " + new String(encryptedBytes));
        System.out.println("解密后: " + new String(decryptedBytes));
    }
}
非对称加密算法:RSA(Rivest–Shamir–Adleman)

定义
RSA是一种非对称加密算法,它依赖于大整数因数分解的难度。在RSA算法中,每个用户都有一对密钥:一个公钥用于加密数据,一个私钥用于解密数据。

特点

  • 加密和解密使用不同的密钥。
  • 安全性较高,但速度慢于对称加密算法。
  • 常用于加密小块数据,如密钥交换。

Java实现示例

import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;

public class RSAUtil {
    private static final String ALGO = "RSA";

    public static void main(String[] args) {
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGO);
            keyPairGen.initialize(1024, new SecureRandom());
            KeyPair keyPair = keyPairGen.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();

            String originalData = "Hello, World!";
            Cipher cipher = Cipher.getInstance(ALGO);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedData = cipher.doFinal(originalData.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedData = cipher.doFinal(encryptedData);

            System.out.println("Original: " + originalData);
            System.out.println("Encrypted: " + new String(encryptedData));
            System.out.println("Decrypted: " + new String(decryptedData));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
哈希算法:SHA(Secure Hash Algorithm)

定义
SHA是一系列密码散列函数,可以将任意长度的输入数据转换为固定长度的输出。SHA-1产生160位(20字节)的散列值,而SHA-2(包括SHA-224、SHA-256、SHA-384和SHA-512)提供更高的安全性。

特点

  • 单向性,不能从输出反推出输入数据。
  • 用于数据完整性校验和数字签名。

Java实现示例

import java.security.MessageDigest;

public class SHAUtil {
    public static String encode(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(str.getBytes("utf-8"));
            byte[] digest = md.digest();
            return byteToHexString(digest);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String byteToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String content = "Hello, World!";
        System.out.println(SHAUtil.encode(content));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方巴黎~Sunsiny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值