数据结构-加解密算法

引言

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

对称加密算法

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

特性:

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

优点:

效率高,速度快。

缺点:

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

运用场景:

文件加密、网络通信等。

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);  
    }  
}

总结

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

相关推荐
m0_720245016 小时前
1543.统计好三元组(简单)
开发语言·算法
To_OC6 小时前
LC 560 和为 K 的子数组:前缀和配哈希表,这对组合我是真的服了
javascript·算法·程序员
hans汉斯7 小时前
《软件工程与应用》期刊推荐&10月版面征稿中
图像处理·人工智能·深度学习·算法·音视频·软件工程
叠层归一研究院7 小时前
AGI 系统(八):符号范畴嵌入函子 — SymCat ↪ C_M107 严格化
c语言·开发语言·人工智能·算法·transformer·agi
其美杰布-富贵-李8 小时前
08 进阶评估指标与算法特定指标
人工智能·算法
.道阻且长.8 小时前
5.LeetCode算法习题讲解--双指针--有效三角形的个数
算法·leetcode·职场和发展
我是海飞9 小时前
杰理JL703N SSD1306 OLED(128x64) 点阵屏移植说明
单片机·算法·嵌入式·音频·杰理
wabs6669 小时前
关于图论【最短路径之Bellman_ford 算法(单源有限最短路)|卡码网96.城市间货物运输III的思考】
数据结构·算法·图论·卡码网·bellman_ford·单源有限最短路
wuyk55510 小时前
2.队列:先进先出的线性数据结构
c语言·数据结构·stm32·单片机
lsylalalala12 小时前
常见的排序算法1
数据结构·算法·排序算法