不兼容的类型:byte []不能转换为byte

时间:2014-03-06 17:57:15

标签: java byte rsa

我目前正在尝试使用RSA加密用户输入数据(我知道它不是最好的方式,但它用于分配,我需要这样做)我有加密和解密小数据串但我现在正试图通过在每个单词处拆分字符串来移动到任意长度的数据,但是当我尝试以这种方式加密数据时,我得到一个错误说"不兼容的类型:byte []无法转换为字节"

我不知道为什么会这样或者如何修复它。任何帮助都会很棒,所以即使是关于如何以不同方式进行这样做的想法

final String originalText = "New Class NewClass NewClass NewClass ";
        String[] splited = originalText.split("\\s+");

        ObjectInputStream inputStream = null;

        // Encrypt the string using the public key
        inputStream = new ObjectInputStream(new FileInputStream(PUBLIC_KEY_FILE));
        final PublicKey publicKey = (PublicKey) inputStream.readObject();
        final byte[] cipherText = null;
        for (int i = 0; i < splited.length; i++) {
            LINE ERROR APPEARS ON
            cipherText[i] = encrypt(splited[i], publicKey);
            System.out.println(cipherText[i]);

        }

1 个答案:

答案 0 :(得分:1)

致电encrypt后,您会获得字节数组,因此cipherText可以是byte[][] cipherText,然后只需拨打System.out.println(new String(cipherText[i]));

即可看到它 由于您未初始化NPE数组,因此引发

编辑 cipherText。请尝试以下方法:

byte[][] cipherText = new byte[splited.length][];
相关问题