字节到字符串转换java中的随机字符

字节到字符串转换java中的随机字符,java,encryption,type-conversion,bytearray,Java,Encryption,Type Conversion,Bytearray,我正在将字节[]转换为字符串。每次我将字节数组转换为字符串时,它的前面都有一个前缀类型字符。我试过不同的字符、大写字母等。。仍然有前缀 当我将字节码写入系统输出时,它仍然具有字符 System.out.write(theByteArray); System.out.println(new String(theByteArray, "UTF-8")); 当我将文本写入文件时,似乎字节数组打印得完美无缺,但随后我扫描了它,最后得到了奇怪的前缀符号 要加密的文本> "aaaa" 解密并转换为字符

我正在将字节[]转换为字符串。每次我将字节数组转换为字符串时,它的前面都有一个前缀类型字符。我试过不同的字符、大写字母等。。仍然有前缀

当我将字节码写入系统输出时,它仍然具有字符

System.out.write(theByteArray);

System.out.println(new String(theByteArray, "UTF-8"));
当我将文本写入文件时,似乎字节数组打印得完美无缺,但随后我扫描了它,最后得到了奇怪的前缀符号

要加密的文本>

"aaaa"
解密并转换为字符串时的文本>

“aaaa”

这个角色似乎消失了,这是它的一个图像

我想将给定的字符串与另一个字符串进行比较,有点像解密密码,然后将其与数据库进行比较。如果一个匹配,那么它将提供访问权限

生成此字节码的代码

请记住,我正在查看的字节是decData,而这不是我的代码

byte[] encData;
        byte[] decData;
        File inFile = new File(fileName+ ".encrypted");

        //Generate the cipher using pass:
        Cipher cipher = FileEncryptor.makeCipher(pass, false);

        //Read in the file:
        FileInputStream inStream = new FileInputStream(inFile);

        encData = new byte[(int)inFile.length()];
        inStream.read(encData);
        inStream.close();
        //Decrypt the file data:
        decData = cipher.doFinal(encData);
        //Figure out how much padding to remove

        int padCount = (int)decData[decData.length - 1];

        //Naive check, will fail if plaintext file actually contained
        //this at the end
        //For robust check, check that padCount bytes at the end have same value
        if( padCount >= 1 && padCount <= 8 ) {
            decData = Arrays.copyOfRange( decData , 0, decData.length - padCount);
        }
        FileOutputStream target = new FileOutputStream(new File(fileName + ".decrypted.txt"));
        target.write(decData);
        target.close();
byte[]数据;
字节[]数据;
文件填充=新文件(文件名+“.encrypted”);
//使用pass生成密码:
Cipher Cipher=FileEncryptor.makeCipher(通过,错误);
//在文件中读取:
FileInputStream inStream=新的FileInputStream(填充);
encData=新字节[(int)infle.length()];
流内读取(加密数据);
流内关闭();
//解密文件数据:
decData=cipher.doFinal(encData);
//计算要移除多少填充物
int padCount=(int)decData[decData.length-1];
//如果纯文本文件实际包含,则朴素检查将失败
//这是最后的结果
//要进行健壮性检查,请检查末尾的padCount字节是否具有相同的值

如果(padCount>=1&&padCount看起来像encData contains,我认为Java在读取带有BOM的流时,只会将BOM视为UTF-8字符,这导致了“前缀”。您可以尝试此处建议的解决方案:

另一方面,字节顺序标记是可选的,不建议用于UTF-8编码

  • 原始数据是否使用utf-8编码
  • 如果是这样的话,那么也许有必要先找出BOM为什么会进入原始数据

  • 你说的前缀是什么?我不确定,它只是消失了,对不起,让我获取一个图像。如果不提供链接,请使用编辑器上的图像按钮。这不是比较字符串的方式,请使用
    .equals()