我正在尝试在Netbeans Java中加密捕获的IP地址,但是在运行表单时会收到消息addr is of illegal length。为什么会出现该错误?

这是代码:

if (packet instanceof IPPacket) {

    IPPacket ipp = (IPPacket) packet;
    InetAddress dest = ipp.dst_ip;
    KeyGenerator keygenerator;

    try {
        keygenerator = KeyGenerator.getInstance("DES");
        SecretKey myDesKey = keygenerator.generateKey();
        Cipher desCipher;
        // Create the cipher
        desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
        byte[] ipEncrypted = desCipher.doFinal(ipp.dst_ip.getAddress());
        InetAddress src = ipp.src_ip;
        //   System.out.println(dest);
        try {
            ipp.dst_ip = InetAddress.getByAddress(ipEncrypted);
        } catch(Exception e) {
             System.out.println(e.getMessage());
        }
        ipp.src_ip = src;
    } catch(Exception ex ) {
        System.out.println(ex.getMessage());
    }

最佳答案

因为DES输出8字节块,所以IPv4和IPv6地址分别需要4字节或16字节。

10-04 10:00