ZBLOG

使用web浏览器的方式进行数据加密,java后端实现解密

为了在Web浏览器中进行数据加密,并让Java后端进行解密,您可以使用AES对称加密。在浏览器中,我们将使用JavaScript的Crypto API来加密数据,并在Java后端中实现解密逻辑。

以下是一个完整的示例,包括如何在Web浏览器中加密数据以及在Java后端解密数据。

1. 在浏览器中的 JavaScript 加密代码

  1. async function encrypt(plainText, key) {
  2. const encoder = new TextEncoder();
  3. const data = encoder.encode(plainText);
  4. // 导入AES密钥(注意这里我们用的是Uint8Array)
  5. const importedKey = await window.crypto.subtle.importKey(
  6. "raw",
  7. key,
  8. { name: "AES-CBC" },
  9. false,
  10. ["encrypt"]
  11. );
  12. // 生成随机初始化向量(IV)
  13. const iv = window.crypto.getRandomValues(new Uint8Array(16));
  14. // 使用 AES-CBC 模式进行加密
  15. const encryptedData = await window.crypto.subtle.encrypt(
  16. {
  17. name: "AES-CBC",
  18. iv: iv,
  19. },
  20. importedKey,
  21. data
  22. );
  23. // 将 IV 和 密文合并并返回 Base64 编码格式
  24. const combinedBuffer = new Uint8Array(iv.length + encryptedData.byteLength);
  25. combinedBuffer.set(iv);
  26. combinedBuffer.set(new Uint8Array(encryptedData), iv.length);
  27. return btoa(String.fromCharCode.apply(null, combinedBuffer));
  28. }
  29. // 示例用法
  30. (async () => {
  31. // 随机生成16字节的AES秘钥
  32. const keyMaterial = crypto.getRandomValues(new Uint8Array(16));
  33. // 加密文本
  34. const encryptedText = await encrypt("Hello World", keyMaterial);
  35. console.log("Encrypted:", encryptedText);
  36. })();

2. Java 后端解密代码

对于 Java 后端,您需要使用 Java 的 Cryptography API 来进行 AES 解密。请确保您的后端能够接收 Base64 编码的数据。

  1. import javax.crypto.Cipher;
  2. import javax.crypto.spec.IvParameterSpec;
  3. import javax.crypto.spec.SecretKeySpec;
  4. import java.util.Base64;
  5. public class AESDecryption {
  6. public static String decrypt(String encryptedBase64, byte[] secretKey) throws Exception {
  7. // 解码 Base64 字符串为字节数组
  8. byte[] decodedBytes = Base64.getDecoder().decode(encryptedBase64);
  9. // 提取 IV 和 密文部分
  10. byte[] iv = new byte[16]; // AES CBC模式下IV长度固定为16字节
  11. System.arraycopy(decodedBytes, 0, iv, 0, iv.length);
  12. byte[] cipherText = new byte[decodedBytes.length - iv.length];
  13. System.arraycopy(decodedBytes, iv.length, cipherText, 0, cipherText.length);
  14. SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
  15. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  16. IvParameterSpec ivParams = new IvParameterSpec(iv);
  17. cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParams);
  18. byte[] decryptedBytes = cipher.doFinal(cipherText);
  19. return new String(decryptedBytes); // 返回解密后的字符串
  20. }
  21. public static void main(String[] args) {
  22. try {
  23. String encryptedBase64 = "经过前端加密得到的Base64字符串";
  24. byte[] secretKey = ...; // 在此处填入相同的秘密key(16个字节)
  25. String decryptedText = decrypt(encryptedBase64, secretKey);
  26. System.out.println("Decrypted text: " + decryptedText);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

注意事项:

  1. 秘钥管理:确保安全存储和传递秘钥。在生产环境中,请不要硬编码秘钥。
  2. 初始化向量 (IV):在前面的 JavaScript 示例中,IV 是与每次加密相关联并随机生成的。务必将其附加到密码文本,以便于后续解码。
  3. 字符编码:请确保前后端使用相同的字符编码(通常为 UTF-8)。
  4. 错误处理:实际应用中需要完善错误处理机制。

通过这种方式,您就能实现从前端到后端的数据安全传输。希望这能帮助到你!如果有进一步的问题,请随时询问。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?