java - 此代码中 AES key 所需的大小是多少?

标签 java android encryption aes

在下面的代码中,我从外部类初始化安全 key 。是否有必要使用16位的安全 key 长度?是否有可能使用较小长度的字节?

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class MCrypt {

        private String iv = "fedcba9876543210";
        private IvParameterSpec ivspec;
        private SecretKeySpec keyspec;
        private Cipher cipher;

        private String SecretKey ;

        public MCrypt(String s)
        {
               SecretKey=s;
                ivspec = new IvParameterSpec(iv.getBytes());

                keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");

                try {
                        cipher = Cipher.getInstance("AES/CBC/NoPadding");
                } catch (NoSuchAlgorithmException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public byte[] encrypt(String text) throws Exception
        {
                if(text == null || text.length() == 0)
                        throw new Exception("Empty string");

                byte[] encrypted = null;

                try {
                        cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

                        encrypted = cipher.doFinal(padString(text).getBytes());
                } catch (Exception e)
                {                       
                        throw new Exception("[encrypt] " + e.getMessage());
                }

                return encrypted;
        }

        public byte[] decrypt(String code) throws Exception
        {
                if(code == null || code.length() == 0)
                        throw new Exception("Empty string");

                byte[] decrypted = null;

                try {
                        cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

                        decrypted = cipher.doFinal(hexToBytes(code));
                } catch (Exception e)
                {
                        throw new Exception("[decrypt] " + e.getMessage());
                }
                return decrypted;
        }



        public static String bytesToHex(byte[] data)
        {
                if (data==null)
                {
                        return null;
                }

                int len = data.length;
                String str = "";
                for (int i=0; i<len; i++) {
                        if ((data[i]&0xFF)<16)
                                str = str + "0" + java.lang.Integer.toHexString(data[i]&0xFF);
                        else
                                str = str + java.lang.Integer.toHexString(data[i]&0xFF);
                }
                return str;
        }


        public static byte[] hexToBytes(String str) {
                if (str==null) {
                        return null;
                } else if (str.length() < 2) {
                        return null;
                } else {
                        int len = str.length() / 2;
                        byte[] buffer = new byte[len];
                        for (int i=0; i<len; i++) {
                                buffer[i] = (byte) Integer.parseInt(str.substring(i*2,i*2+2),16);
                        }
                        return buffer;
                }
        }



        private static String padString(String source)
        {
          char paddingChar = ' ';
          int size = 16;
          int x = source.length() % size;
          int padLength = size - x;

          for (int i = 0; i < padLength; i++)
          {
                  source += paddingChar;
          }

          return source;
        }
}

最佳答案

您的 key 必须与 AES 算法允许的长度之一匹配:16、24 或 32 字节。您不能使用小于 16 字节的 key 。

对您的代码的一些其他评论:

  • 对所有变量使用 camelCase(例如 secretKey)。

  • 避免使用固定 IV。每次生成一个随机IV并与密文一起存储。

  • 考虑使用填充模式(例如 AES/CBC/PKCS5Padding),否则您的输入必须始终是 16 字节的倍数。

  • 您正在对字符串调用 getBytes(),但未提供字符集。这可能会导致不同平台上的不同结果。始终指定字符集。

关于java - 此代码中 AES key 所需的大小是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21125367/

相关文章:

android - 将数据从android/iOS应用发送到Kafka或Hadoop

android - Google Play 计费 API : How to understand the user is subscribed?

android进度对话框背景颜色

php - 如果没有 ssl,如何保护注册过程

java - 我需要解析字符串并提取信息以输入数据库

java - Maven版本和java版本冲突

Java 1.7 允许使用 '_' 的整数值,为什么在解析 Integer 时不这样做

java - PowerMockRunner 不应用 JUnit ClassRules

security - 保护 MySQL 数据库中敏感信息的步骤

encryption - Unity3D构建代码加密