Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

java - Netty StreamCorruptedException After Keyexchange

I am trying to build an encrypted Netty connection using AES. RSA is helping me to transmit the AES key and the iv.

Server and client have the same key and iv after the exchange. I am creating ciphers to actually de- and encrypt stuff with it. When I am running the server on my pc in eclipse with the Cp1252 encoding it's working fine. As soon as I change the encoding to UTF-8 (encoding my client is written in) or run the server on my Linux system its not working anymore.

I saw that i can get the iv from the cipher with cipher.getIV(); unfortunately they're not the same so that might be the problem.

Exception (client)

Server output

As you can see the AES key and IV are the same.

This is how I'm generating the Cipher

public static Cipher generateCipher(byte[] secret, int mode, byte[] iv) {
        KeySpec spec = new PBEKeySpec(new String(secret).toCharArray(), iv, 65536, 128);
        try {
            byte[] key = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(spec).getEncoded();
            SecretKeySpec secretKey = new SecretKeySpec(key, "AES");

            Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
            IvParameterSpec parameterSpec = new IvParameterSpec(secretKey.getEncoded());

            cipher.init(mode, secretKey, parameterSpec);
            return cipher;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

This is how I print the Strings

byte[] aesKey = CryptionUtils.decryptRSA(packet.getKey(), rsaKeys.getPrivate());
byte[] iv = CryptionUtils.decryptRSA(packet.getIv(), rsaKeys.getPrivate());

if (iv.length != 12 || aesKey.length != 16) {
        server.sendPacket(ip, new KickPacket(EnumKickPacket.INVALID_AES_INFO.ordinal()));
        server.disconnectClient(ip, EnumKickPacket.INVALID_AES_INFO.name());
}

Cipher decryptCipher = CryptionUtils.generateCipher(aesKey, Cipher.DECRYPT_MODE, iv);
Cipher encryptCipher = CryptionUtils.generateCipher(aesKey, Cipher.ENCRYPT_MODE, iv);

System.out.println("IV: " + Base64.getEncoder().encodeToString(iv));
System.out.println("AESKey: " + Base64.getEncoder().encodeToString(aesKey));
System.out.println("DecryptCipher: " + Base64.getEncoder().encodeToString(decryptCipher.getIV()));
System.out.println("EncryptCipher: " + Base64.getEncoder().encodeToString(encryptCipher.getIV()));
question from:https://stackoverflow.com/questions/65602215/netty-streamcorruptedexception-after-keyexchange

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

new String(secret, Charset.forName("UTF-8") Fixed the issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...