1
public static String Encrypt(String text){
    try {           
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= KEY.getBytes("UTF-8");
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.ENCRYPT_MODE,keySpec,ivSpec);

        byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
        String result = Base64.encodeToString(results, 0);
        //result = URLEncoder.encode(result.trim(),"UTF-8");
        return result;

    } catch (Exception e) {
        // TODO: handle exception
        return null;
    }
}   

public static String Decrypt(String text){
    try {           
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= KEY.getBytes("UTF-8");
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
        byte[] results = cipher.doFinal(text.getBytes("UTF-8"));
        String result = Base64.encodeToString(results, 0);
        //result = URLEncoder.encode(result.trim(),"UTF-8");
        return result;

    } catch (Exception e) {
        // TODO: handle exception
        Log.d("decryption", e.getMessage());
        return null;
    }
}   

This is code for encryption and decryption, I got encryption algo from some other question and it works fine for me but I need to decrypt too and I tried to mimic it like encryption but its not working. Exception says last block incomplete.

4

1 回答 1

6

问题是您没有以相反的顺序执行相反的顺序。你最终得到了 base64 编码的数据 - 所以你需要做的第一件事就是对它进行 base64 解码以获取二进制加密数据。然后解密,然后使用 UTF-8 将其转换回字符串。

public static String decrypt(String text){
    try {           
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        byte[] keyBytes= new byte[16];
        byte[] b= KEY.getBytes("UTF-8");
        int len= b.length;
        if (len > keyBytes.length) len = keyBytes.length;
        System.arraycopy(b, 0, keyBytes, 0, len);
        SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(keyBytes);
        cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);
        byte[] results = cipher.doFinal(Base64.decode(text, 0));
        return new String(results, "UTF-8");
    } catch (Exception e) {
        // TODO: handle exception
        Log.d("decryption", e.getMessage());
        return null;
    }
}   

当你从某个地方获取代码时,重要的是你要真正了解它在做什么——尤其是了解这里每个阶段的数据是什么样的:

  • 从纯文本开始
  • UTF-8 将其编码为字节数组
  • 加密到另一个字节数组(不是UTF-8 编码的文本)
  • Base64 将加密的二进制数据编码为字符串

每当您进行这样的转换时,当您想要反转它时,您也需要反转步骤的顺序,以便返回原始数据。

于 2013-08-03T09:06:52.407 回答