Android/Java与Web/JavaScript之间的AES-GCM加密兼容性
创始人
2024-08-20 06:30:04
0

在Android/Java和Web/JavaScript之间使用AES-GCM加密算法,可以通过以下步骤来实现兼容性:

  1. 生成随机的16字节密钥和12字节的随机IV:
SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[16];
secureRandom.nextBytes(key);

byte[] iv = new byte[12];
secureRandom.nextBytes(iv);
  1. 在Android/Java端使用javax.crypto库进行加密和解密:
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;

public class AesGcmEncryption {

    public static byte[] encrypt(String plaintext, byte[] key, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        SecretKey secretKey = new SecretKeySpec(key, "AES");
        GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
        return cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
    }

    public static String decrypt(byte[] ciphertext, byte[] key, byte[] iv) throws Exception {
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        SecretKey secretKey = new SecretKeySpec(key, "AES");
        GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, parameterSpec);
        byte[] plaintextBytes = cipher.doFinal(ciphertext);
        return new String(plaintextBytes, StandardCharsets.UTF_8);
    }
}
  1. 在Web/JavaScript端使用Crypto.subtle库进行加密和解密:
async function encrypt(plaintext, key, iv) {
    const encodedKey = await crypto.subtle.importKey(
        'raw', key, 'AES-GCM', false, ['encrypt']
    );
    const encryptedData = await crypto.subtle.encrypt(
        {name: 'AES-GCM', iv}, encodedKey, new TextEncoder().encode(plaintext)
    );
    return new Uint8Array(encryptedData);
}

async function decrypt(ciphertext, key, iv) {
    const encodedKey = await crypto.subtle.importKey(
        'raw', key, 'AES-GCM', false, ['decrypt']
    );
    const decryptedData = await crypto.subtle.decrypt(
        {name: 'AES-GCM', iv}, encodedKey, ciphertext
    );
    return new TextDecoder().decode(decryptedData);
}

这样,你就可以在Android/Java和Web/JavaScript之间使用相同的AES-GCM加密算法进行数据加密和解密了。请注意,这只是一个简单的示例,实际使用时还需要考虑更多的安全性和错误处理。

相关内容

热门资讯

关于!微信微乐自建房辅助工具,... 关于!微信微乐自建房辅助工具,微乐家乡麻将自建房辅助app(开挂自建房)方法1)微乐家乡麻将自建房辅...
大家学习交流!微信卡五星辅助器... 大家学习交流!微信卡五星辅助器,微乐小程序免费黑科技(开挂自建房)方法1、很好的工具软件,可以解锁游...
三分钟了解!微乐微信自建房辅助... 三分钟了解!微乐微信自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)工具1、三分钟了解...
一分钟教会你!微乐家乡麻将自建... 一分钟教会你!微乐家乡麻将自建房辅助app,微乐自建房脚本免费入口(开挂自建房)方法1、用户打开应用...
分享实测!微信微乐自建房辅助工... 分享实测!微信微乐自建房辅助工具,微乐自建房脚本免费入口(开挂自建房)器1、起透看视 微信微乐自建房...
重大通报!微乐自建房辅助工具脚... 重大通报!微乐自建房辅助工具脚本,微乐小程序免费黑科技(开挂自建房)app微乐自建房辅助工具脚本破解...
如何分辨真伪!微乐微信自建房辅... 如何分辨真伪!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)技巧1、微乐微信自建房辅...
我来教教你!微乐自建房辅助工具... 我来教教你!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)工具1、玩家可以在微乐家...
重大来袭!微乐自建房辅助工具神... 重大来袭!微乐自建房辅助工具神器,微乐小程序免费黑科技(开挂自建房)器1、游戏颠覆性的策略玩法,独创...
带你了解!微信卡五星辅助器,微... 带你了解!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)方法1、带你了解!微信卡五星辅助器,...