java web与android互通的aes算法

本文探讨了如何在Java Web和Android应用之间使用AES加密算法进行数据安全通信,详细阐述了Java实现AES加密的代码示例以及加密模式和填充方式的选择。
Java实现代码
    //可自定义保证16btye即可
    private static final byte[] IV = {16, 26, -35, 23, 34, 125, -5, -4, -8, -9, -15, -78, 90, -8, -99, 100};

    public static byte[] encrypt(String content, String password) {
        try {
            SecretKeySpec key = getKey(password);//根据密码生成key
            if(key == null){
                return null;
            }
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");// 创建密码器"算法/模式/补码方式"
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));//初始化,使用该模式,需要一个向量16byte的IvParameterSpec
            byte[] result = cipher.doFinal(byteContent);// 加密
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String encryptToStr(String content, String password) {
        byte[] bytes = encrypt(content, password);
        if(bytes == null){
            return null;
        }
        return Base64Util.encode(bytes);
    }

    public static byte[] decrypt(byte[] content, String password) {
        try {
            SecretKeySpec key = getKey(password);//根据密码生成key
            if(key == null){
                return null;
            }
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");//创建密码器"算法/模式/补码方式"
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));//初始化,使用该模式,需要一个向量16byte的IvParameterSpec
            byte[] result = cipher.doFinal(content);// 解密
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decryptFromStr(String encodeStr, String password) {
        byte[] encodeBytes = Base64Util.decodeToBytes(encodeStr);
        byte[] bytes = decrypt(encodeBytes, password);
        if(bytes == null){
            return null;
        }
        return new String(bytes);
    }

    //根据密码生成16byte的key
    private static SecretKeySpec getKey(String password){
        try {
            byte[] passwdBytes = password.getBytes("utf-8");
            if(passwdBytes.length < 16){
                return null;
            }
            //简单转换为16byte,建议用复杂的转换以防被知道密码后破解
            byte[] bytes = new byte[16];
            for(int i = 0; i < 16; i++){
                bytes[i] = passwdBytes[i];
            }
            return new SecretKeySpec(bytes, "AES");
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
AES加密模式和填充方式
算法/模式/填充16字节加密后数据长度不满16字节加密后长度
AES/CBC/NoPadding16不支持
AES/CBC/PKCS5Padding3216
AES/CBC/ISO10126Padding3216
AES/CFB/NoPadding16原始数据长度
AES/CFB/PKCS5Padding3216
AES/CFB/ISO10126Padding3216
AES/ECB/NoPadding16不支持
AES/ECB/PKCS5Padding3216
AES/ECB/ISO10126Padding3216
AES/OFB/NoPadding16原始数据长度
AES/OFB/PKCS5Padding3216
AES/OFB/ISO10126Padding3216
AES/PCBC/NoPadding16不支持
AES/PCBC/PKCS5Padding3216
AES/PCBC/ISO10126Padding3216
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值