数据结构-加解密算法

引言

加解密算法是信息安全领域的重要组成部分,它们用于保护数据的机密性、完整性和可用性。

对称加密算法

对称加密算法使用相同的密钥进行加密和解密。

特性:

加密和解密速度快,适合处理大量数据。

优点:

效率高,速度快。

缺点:

密钥管理困难,需要安全地分发和存储密钥。

运用场景:

文件加密、网络通信等。

Java代码示例(使用AES加密):

java 复制代码
import javax.crypto.Cipher;  
import javax.crypto.spec.SecretKeySpec;  
import java.nio.charset.StandardCharsets;  
import java.util.Base64;  
  
public class AESExample {  
    private static final String ALGORITHM = "AES";  
    private static final byte[] keyValue =   
        new byte[]{'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y'};  
  
    public static String encrypt(String valueToEnc) throws Exception {  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyValue, ALGORITHM));  
        byte[] encrypted = cipher.doFinal(valueToEnc.getBytes());  
        return Base64.getEncoder().encodeToString(encrypted);  
    }  
  
    public static String decrypt(String encryptedValue) throws Exception {  
        Cipher cipher = Cipher.getInstance(ALGORITHM);  
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyValue, ALGORITHM));  
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedValue));  
  
        return new String(original);  
    }  
}

非对称加密算法

非对称加密算法使用一对密钥:公钥用于加密,私钥用于解密。

特性:

安全性高,适合密钥交换和数字签名。

优点:

解决了密钥分发和管理的问题。

缺点:

加密和解密速度相对较慢。

运用场景:

SSL/TLS协议、数字签名等。

Java代码示例(使用RSA加密和解密):

java 复制代码
import java.security.*;  
import javax.crypto.Cipher;  
import java.util.Base64;  
  
public class RSAExample {  
    public static void main(String[] args) throws Exception {  
        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");  
        keyPairGen.initialize(2048);  
        KeyPair pair = keyPairGen.generateKeyPair();  
        PublicKey pub = pair.getPublic();  
        PrivateKey priv = pair.getPrivate();  
  
        String plainText = "Hello, RSA!";  
        Cipher encryptCipher = Cipher.getInstance("RSA");  
        encryptCipher.init(Cipher.ENCRYPT_MODE, pub);  
        byte[] cipherText = encryptCipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));  
        String encryptedString = Base64.getEncoder().encodeToString(cipherText);  
  
        Cipher decryptCipher = Cipher.getInstance("RSA");  
        decryptCipher.init(Cipher.DECRYPT_MODE, priv);  
        byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedString));  
        String decryptedString = new String(decryptedBytes, StandardCharsets.UTF_8);  
  
        System.out.println("Plain Text : " + plainText);  
        System.out.println("Encrypted Text : " + encryptedString);  
        System.out.println("Decrypted Text : " + decryptedString);  
    }  
}

哈希算法

哈希算法将任意长度的输入数据转换为固定长度的哈希值。

特性:

单向性,即无法通过哈希值反推出原始数据。

优点:

快速、高效。

缺点:

存在哈希碰撞的可能性。

运用场景:

数据完整性校验、密码存储等。

Java代码示例(使用SHA-256哈希):

java 复制代码
import java.security.MessageDigest;  
import java.nio.charset.StandardCharsets;  
import java.math.BigInteger;  
  
public class SHA256Example {  
    public static String getSHA256(String input) {  
        try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");  
            byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8));  
            BigInteger number = new BigInteger(1, messageDigest);  
            String hashtext = number.toString(16);  
            while (hashtext.length() < 32) {  
                hashtext = "0" + hashtext;  
            }  
            return hashtext;  
        } catch (NoSuchAlgorithmException e) {  
            throw new RuntimeException(e);  
        }  
    }  
  
    public static void main(String[] args) {  
        String input = "Hello, SHA-256!";  
        String sha256 = getSHA256(input);  
        System.out.println("SHA-256 hash of \"" + input + "\": " + sha256);  
    }  
}

总结

每种加密算法都有其特定的用途和适用场景。在实际应用中,您应该根据数据的敏感性、处理性能要求以及安全需求来选择合适的加密算法。此外,还需要考虑算法的安全性、兼容性以及是否受到已知的攻击或漏洞影响。

相关推荐
程序趣谈5 分钟前
算法随笔_36: 复写零
数据结构·python·算法
轩情吖16 分钟前
二叉树-堆(补充)
c语言·数据结构·c++·后端·二叉树··排序
九亿AI算法优化工作室&27 分钟前
GWO优化LSBooST回归预测matlab
人工智能·python·算法·机器学习·matlab·数据挖掘·回归
爱是小小的癌2 小时前
Java-数据结构-优先级队列(堆)
java·前端·数据结构
sjsjs112 小时前
【数据结构-字典树】力扣14. 最长公共前缀
数据结构·leetcode
python算法(魔法师版)2 小时前
基于机器学习鉴别中药材的方法
深度学习·线性代数·算法·机器学习·支持向量机·数据挖掘·动态规划
JNU freshman3 小时前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)3 小时前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
赵鑫亿5 小时前
7.DP算法
算法·dp
iqay5 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#