Java在新数组或ArrayList AES加密中存储45个字符

标签 java arrays encryption arraylist aes

我有一个 AES 128 位加密方法来加密消息。但它不会加密长度超过 45 个字符的消息。因此,我在想是否可以通过将长度超过 45 个字符的消息拆分为大小为 45 个字符的数组或数组列表来实现这一点,然后一次加密每个 block 。

因此,如果我有一条长度为 90 的消息,我应该有两个大小分别为 45 的数组(或者大小为 2 且每个大小为 45 个字符的数组)。或一个数组列表来做到这一点。如果我有一条长 180 的消息,那么我应该有一个由 4 个数组组成的 block 。

更新:所以我最初的问题是无法在客户端-服务器聊天中使用 AES 加密发送长消息。

以下是AES实现的代码,但其中没有任何填充或IV。

首先我有一个客户;谁发送使用下面的 AESmsgEncryption 类加密的消息。

客户端类

    public void send(String sendMessage) {

        userName = GUI.getUserName();

        //each index of the array will contain a substring of the send message (maximum length 45 char)
        if (sendMessage.length() > 45) {
//      String [] arrayOfStrings = new String[sendMessage.length()/45];
        int sizeOfArray = sendMessage.length()/45;
        System.out.println("CANT SEND MESSAGES LONGER THAN 45");

        } else {    

            String ciphertext  = msgAESEnDe.encryptAES(sendMessage, scrtkey);
            System.out.println(userName + ": " + ciphertext + "  <-- encrypted before sending");
            out.println(userName + "#ciphertext$" + ciphertext);
            out.flush();

        }
}

一旦客户端发出消息,接收者就会按原样接收该消息(加密)并将其发送回在线客户端。一旦在线客户端收到加密的文本消息,他们就会在客户端类的另一个接收方法中使用以下代码解密该消息

String username = recMessage.substring(0, recMessage.indexOf(' '));
                String ciphertxtWithNoName = recMessage.substring(recMessage.indexOf(':') + 2);

                scrtkey = new SecretKeySpec(secrtKeyByte, "AES");

                String plaintext = msgAESEnDe.decryptedPlain(ciphertxtWithNoName, scrtkey);
                System.out.println(username +" " + plaintext +"  <-- after decryption");

                //current time
                String time = log.format(new Date());

                // Displaying received message.
                ClientGUI.TA_CONVERSATION.append(time+ " ["+username.substring(0, username.length()-1) + "]: " + plaintext + "\n");

所以在 AESmsgEncryption 类中我有一个方法如下;

public final String encryptAES(final String plaintext, SecretKey key) {
        String ciphertext = new String();

        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] bytePlaintext = plaintext.getBytes();
            byte[] byteCiphertext = cipher.doFinal(bytePlaintext);
            ciphertext = new BASE64Encoder().encode(byteCiphertext);

        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException: " + e);
        } catch (NoSuchPaddingException e) {
            System.out.println("NoSuchPaddingException: " + e);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            System.out.println("InvalidKeyException: " + e);
        } catch (IllegalBlockSizeException e) {
            System.out.println("IllegalBlockSizeException: " + e);
        } catch (BadPaddingException e) {
            System.out.println("BadPaddingException: " + e);
        }
        return ciphertext;
    }

public final String decryptedPlain(String ciphertext, SecretKey key) {
        try {
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);


            byte [] decodedValue = new Base64().decode(ciphertext.getBytes());
            plaintext = cipher.doFinal(decodedValue);

        } catch (NoSuchAlgorithmException e) {
            System.out.println("NoSuchAlgorithmException: " + e);
        } catch (NoSuchPaddingException e) {
            System.out.println("NoSuchPaddingException: " + e);
        } catch (InvalidKeyException e) {
            System.out.println("InvalidKeyException: " + e);
        } catch (IllegalBlockSizeException e) {
            System.out.println("IllegalBlockSizeException: " + e);
        } catch (BadPaddingException e) {
            System.out.println("BadPaddingException: " + e);
        }

        return new String(plaintext);
    }

AES key 是在服务器端生成的,并使用 RSA 加密发送,然后发送到每个客户端,这就是为什么在客户端中我必须在调用 encryptAES 和 decryptedPlain 方法时提供 scrtkey。

重新陈述我的问题;上面的代码无法发送超过 45 个字符的消息,我怎样才能让它发送超过 45 个字符的消息?如果我需要使用填充和 IV,服务器应该将 IV 与 key 一起发送,对吗?

我希望上面的描述是清楚的。

如何在 JAVA 中做到这一点?

谢谢

最佳答案

您获得的这些填充通常与密码的任何限制无关。这是因为:

  1. 您在通信过程中丢失了数据;
  2. 在编码/解码过程中丢失数据。

最后一个是最有可能的,如果你使用随机的 IV 或 key ,它很可能是随机发生的。在这种情况下,拆分字符串可能只能暂时隐藏问题而不是解决问题。

如果密文作为字符串数据处理,则应使用 Base 64 编码。通过将其视为十六进制字符串,确保加密方法之后和解密方法之前的完整密文(以字节为单位)相同。

AES 可以轻松加密千兆字节的信息(尽管安全性取决于用例和操作模式)。不需要分割明文。

关于Java在新数组或ArrayList AES加密中存储45个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781179/

相关文章:

java - 如何根据带有 "0' s"的文本字段输入查询 SQLite 数据库?

java - 我可以将这个语句放在for循环中吗?

java - 计算唯一组合

java - 为什么 Jersey 会导致单例类无法工作?

javascript - 如何使用两个数组创建 json 数据并将其返回给 ajax

python - base64 编码二进制字符串在 NodeJS 和 Python 中返回不同的值

c# - 在 C# 中使用 Bouncy CaSTLe 在 CCM 中加密 AES 密码

随机生成的唯一整数的 Java 数组

c# - 如何将两个对象连接到数组中?

C++:在 Windows RAS API 中禁用 RequireCHAP 和 RequireMsCHAP2