Java-шифр ведет себя по-разному в различных средах - PullRequest
0 голосов
/ 25 июня 2018

Я закодировал систему Netty Server-Client с шифрованием, но если я использую программу на своем ноутбуке, она не работает, ошибки нет, поэтому я не знаю, в чем проблема, My Encryption использует класс:

public static ByteBuf decryptWithRSA(ByteBuf encrypted, PrivateKey mykey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, mykey);
    return wrightbytes(encrypted.alloc().buffer(), cipher.doFinal(readbytes(encrypted))); 
}

public static ByteBuf encryptWithRSA(ByteBuf encrypted, PublicKey mykey) throws Exception {
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, mykey);
    return wrightbytes(encrypted.alloc().buffer(), cipher.doFinal(readbytes(encrypted))); 
}






public static ByteBuf encryptWithAES(SecretKey key, ByteBuf message) throws Exception  {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return wrightbytes(message.alloc().buffer(), cipher.doFinal(readbytes(message)));
}

public static ByteBuf dycryptWithAES(SecretKey key, ByteBuf message) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, key);
    return wrightbytes(message.alloc().buffer(), cipher.doFinal(readbytes(message)));
}

public static SecretKey generateAESKey(int size) throws NoSuchAlgorithmException {
     KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(size);
        return kgen.generateKey();
}

private static ByteBuf wrightbytes(ByteBuf wright, byte[] data) {
    int i = 0;
    while (i < data.length) {
        wright.writeByte(data[i]);
        i++;
    }
    return wright;
}

private static byte[] readbytes(ByteBuf buf) {
    int read = buf.readableBytes();
    byte[] data = new byte[read];
    int i = buf.readerIndex();
    while (i < read) {
        data[i] = buf.readByte();
        i++;
    }
    return data;
}

Может, кто-нибудь заметит ошибку, которую я получил?Вот тебе помощь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...