安卓端AES加密数据后发送到wifi模块,wifi模块使用polarssl解密aes加密的数据。遇到的坑

安卓端加密代码:

public class CipherUtil {
    public static byte[] encrypt(byte content2[]) throws Exception {
        int len=content2.length;
        byte content[] =new byte[len+(16-len%16)];
        System.arraycopy(content2,0,content,0,len);
//这里注意我们要手动填充字节数到16的整数倍
        for(int i=0;i<(16-len%16);i++)
        {
            content[len+i]=0;
        }
        byte[] byte_key={0x70,0x2d, (byte) 0xc1, (byte) 0x8b, (byte) 0xb1,0x3d,0x5e, (byte) 0xc3,
                (byte) 0xd7,0x59, (byte) 0xed, (byte) 0x89,0x54, (byte) 0xfc, (byte) 0xc8,0x40};
        //byte[] byte_key={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
        // 创建AES秘钥
        SecretKeySpec key = new SecretKeySpec(byte_key, "AES");
        // 创建密码器
        Cipher cipher = Cipher.getInstance("AES/CBC/NOPADDING");
        // 初始化加密器
        byte[] iv={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        // 加密
        byte[] res=cipher.doFinal(content);
        return res;
    }
}

wifi模块 polarssl解密代码:

       aes_init(&n->aes_ctx);
        unsigned char key[16]={0x70,0x2d,0xc1, 0x8b,  0xb1,0x3d,0x5e,  0xc3,0xd7,0x59,  0xed,  0x89,0x54,  0xfc,  0xc8,0x40};

        aes_setkey_enc(&n->aes_ctx,key,128);//注意了虽然是解密,但这句代码不可少
        aes_setkey_dec(&n->aes_ctx,key,128);
        unsigned char IV[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        aes_crypt_cbc(&n->aes_ctx, AES_DECRYPT, len, IV, en_buff, de_buff);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无v邪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值