安卓端加密代码:

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);

收藏 打印