Java和JS加密与解密

在软件开发中,数据安全始终是一个重要的话题。加密和解密是保护数据安全的重要手段之一。Java和JavaScript是两种常用的编程语言,在加密和解密方面也有着丰富的功能和库。

加密和解密的基本概念

加密是将原始数据转换为一种不易被理解的形式,以保护数据的安全性。解密则是将加密后的数据恢复为原始数据的过程。加密算法中包含了密钥,密钥是用来进行加密和解密的关键。

Java中的加密与解密

Java提供了丰富的加密和解密功能,其中比较常用的是javax.crypto包。下面是一个使用AES算法进行加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESEncryption {

    public static String encrypt(String key, String data) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            byte[] encryptedData = cipher.doFinal(data.getBytes());
            return Base64.getEncoder().encodeToString(encryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decrypt(String key, String encryptedData) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] decryptedData = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
            return new String(decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        String key = "thisisakey";
        String data = "Hello, World!";
        String encryptedData = encrypt(key, data);
        String decryptedData = decrypt(key, encryptedData);

        System.out.println("Original Data: " + data);
        System.out.println("Encrypted Data: " + encryptedData);
        System.out.println("Decrypted Data: " + decryptedData);
    }
}

JavaScript中的加密与解密

JavaScript中通过Crypto API提供了加密和解密的功能。下面是一个使用Crypto API进行AES加密和解密的示例代码:

function encrypt(key, data) {
    const iv = window.crypto.getRandomValues(new Uint8Array(16));
    const encodedKey = new TextEncoder().encode(key);
    const encodedData = new TextEncoder().encode(data);
    window.crypto.subtle.encrypt(
        {
            name: "AES-CBC",
            iv: iv
        },
        encodedKey,
        encodedData
    ).then(encryptedData => {
        console.log("Encrypted Data: " + new Uint8Array(encryptedData));
    });
}

function decrypt(key, encryptedData) {
    const iv = window.crypto.getRandomValues(new Uint8Array(16));
    const encodedKey = new TextEncoder().encode(key);
    window.crypto.subtle.decrypt(
        {
            name: "AES-CBC",
            iv: iv
        },
        encodedKey,
        encryptedData
    ).then(decryptedData => {
        console.log("Decrypted Data: " + new TextDecoder().decode(decryptedData));
    });
}

const key = "thisisakey";
const data = "Hello, World!";
encrypt(key, data);

加密与解密的应用

加密和解密在实际应用中有着广泛的用途,比如保护用户的隐私数据、传输敏感信息等。通过合理地使用加密算法,可以有效地保护数据的安全性,避免数据泄露和篡改。

总的来说,Java和JavaScript都提供了丰富的加密和解密功能,开发人员可以根据具体需求选择合适的算法和库来保护数据的安全。


gantt
    title 加密与解密示例
    dateFormat  YYYY-MM-DD
    section Java
    AES加密 :done, aes_encrypt, 2022-01-01, 2022-02-01
    AES解密 : done, aes_decrypt, after aes_encrypt, 10d
    section JavaScript
    AES加密 : active, js_encrypt, 2022-01-15, 2022-02-15
    AES解密 : js_decrypt, after js_encrypt, 10d