ZBLOG

前后端使用base64的方式进行加密和解密

使用 Base64 编码进行数据的加密和解密并不是真正的加密方式,Base64 只是将二进制数据转换为文本格式以便于传输。若要真正实现加密,可以结合 Base64 和对称加密(例如 AES)来进行。

以下是一个示例,展示如何在前端使用 JavaScript 加密明文并生成 Base64 编码,然后在后端用 Java 解密该 Base64 字符串。

1. 前端 JavaScript 代码(使用 AES 加密和 Base64 编码)

  1. async function encrypt(plainText, key) {
  2. const encoder = new TextEncoder();
  3. const data = encoder.encode(plainText);
  4. // 导入 AES 密钥
  5. const importedKey = await window.crypto.subtle.importKey(
  6. "raw",
  7. key,
  8. { name: "AES-GCM" },
  9. false,
  10. ["encrypt"]
  11. );
  12. // 使用 AES-GCM 模式进行加密
  13. const iv = window.crypto.getRandomValues(new Uint8Array(12)); // 随机生成IV
  14. const encryptedData = await window.crypto.subtle.encrypt(
  15. {
  16. name: "AES-GCM",
  17. iv: iv,
  18. },
  19. importedKey,
  20. data
  21. );
  22. // 将 IV 和加密后的数据组合并转为 Base64
  23. const combinedBuffer = new Uint8Array(iv.byteLength + encryptedData.byteLength);
  24. combinedBuffer.set(iv);
  25. combinedBuffer.set(new Uint8Array(encryptedData), iv.byteLength);
  26. return btoa(String.fromCharCode.apply(null, combinedBuffer)); // 返回 Base64 编码字符串
  27. }
  28. // 示例用法
  29. (async () => {
  30. // 确保秘钥长度为16个字节(128位)
  31. const keyMaterial = crypto.getRandomValues(new Uint8Array(16));
  32. const encryptedText = await encrypt("Hello World", keyMaterial);
  33. console.log("Encrypted (Base64):", encryptedText);
  34. })();

2. 后端 Java 解密代码

  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.GCMParameterSpec;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.util.Base64;
  5. public class AESDecryption {
  6. public static String decrypt(String base64CipherText, byte[] secretKey) throws Exception {
  7. byte[] decodedBytes = Base64.getDecoder().decode(base64CipherText);
  8. // 提取IV和加密数据,假设IV长度为12字节(96位)
  9. byte[] iv = new byte[12];
  10. System.arraycopy(decodedBytes, 0, iv, 0, iv.length); // 前12字节为IV
  11. byte[] cipherText = new byte[decodedBytes.length - iv.length];
  12. System.arraycopy(decodedBytes, iv.length, cipherText, 0, cipherText.length);
  13. SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
  14. Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
  15. GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(128, iv); // tag length is set to 128 bits
  16. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, gcmParameterSpec);
  17. byte[] decryptedBytes = cipher.doFinal(cipherText);
  18. return new String(decryptedBytes);
  19. }
  20. public static void main(String[] args) {
  21. try {
  22. String base64CipherText = "..."; // 从前端获取的Base64字符串
  23. byte[] secretKey = ...; // 在此处填入相同的秘密key(16个字节)
  24. String decryptedText = decrypt(base64CipherText, secretKey);
  25. System.out.println("Decrypted text: " + decryptedText);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. }

注意事项:

  1. 安全性:确保选择适当的加密算法和模式,如 AES-GCM,且要随机生成 IV。

  2. 秘钥管理:确保妥善存储秘钥,不应在代码中硬编码。

  3. 字符编码:确认前后端都使用相同字符编码(通常为 UTF-8)。

  4. 异常处理:实际应用中需要添加适当的错误处理机制。

  5. 使用库:在真实项目中推荐使用成熟的库如 Bouncy Castle 来处理复杂的密码学操作,以避免潜在漏洞。

以上就是通过前后端结合使用 AES 加密和 Base64 的方式。如果您有任何进一步的问题,请随时询问!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=20633

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?