Java面试黄金宝典45

1. 非对称加密 RSA

 

  • 定义:RSA 是一种广泛使用的非对称加密算法,其安全性基于大整数分解的困难性。它使用一对密钥,即公钥和私钥。公钥可公开用于加密消息,而私钥必须保密,用于解密由相应公钥加密的消息。
  • 要点
    1. 公钥公开,私钥保密,二者成对出现。
    2. 加密和解密使用不同的密钥,保证了数据传输的安全性。
    3. 安全性依赖于对极大整数进行因式分解的困难程度。
  • 应用:常用于数字签名、SSL/TLS 协议中的密钥交换和身份验证等场景。
  • Java 代码示例

java

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.util.Base64;

public class RSADemo {
    public static void main(String[] args) throws Exception {
        // 生成密钥对
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        // 待加密的明文
        String plainText = "Hello, RSA!";

        // 加密
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println("Encrypted Text: " + encryptedText);

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

 

2. 对称密钥 DES

 

  • 定义:DES(Data Encryption Standard)是一种对称加密算法,它将 64 位的明文块通过一系列的置换、替换和异或操作,转换为 64 位的密文块。加密和解密使用相同的密钥。
  • 要点
    1. 密钥长度为 56 位(实际使用时包含 8 位奇偶校验位)。
    2. 属于分组加密算法,每次处理 64 位的数据块。
    3. 由于密钥长度较短,安全性相对较低,如今已较少使用。
  • 应用:早期用于金融、政府等领域的数据加密。
  • Java 代码示例

java

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DESDemo {
    public static void main(String[] args) throws Exception {
        // 生成DES密钥
        KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
        keyGenerator.init(56);
        SecretKey secretKey = keyGenerator.generateKey();

        // 待加密的明文
        String plainText = "Hello, DES!";

        // 加密
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println("Encrypted Text: " + encryptedText);

        // 解密
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        String decryptedText = new String(decryptedBytes, Sta
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值