Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 包含表和结构的文本的加密_Java_X509certificate_Public Key Encryption - Fatal编程技术网

Java 包含表和结构的文本的加密

Java 包含表和结构的文本的加密,java,x509certificate,public-key-encryption,Java,X509certificate,Public Key Encryption,我通过java中本地repo的证书对文件进行加密解密。 但是对于包含表格的字处理文件,图表文件与实际文件并不相同。我使用的是普通的文件输入/输出流。 任何帮助都会有用的,谢谢 public int encryptFileWithpubkey(String filepath,PublicKey pubkey){ int retval=0; FileInputStream fis = null; File file=null; final String locatio

我通过java中本地repo的证书对文件进行加密解密。 但是对于包含表格的字处理文件,图表文件与实际文件并不相同。我使用的是普通的文件输入/输出流。 任何帮助都会有用的,谢谢

public int encryptFileWithpubkey(String filepath,PublicKey pubkey){
    int retval=0;
    FileInputStream fis = null;
    File file=null;
    final String location = filepath;
    PublicKey pubKey= pubkey;
    try{
    try {
        fis = AccessController.doPrivileged(
         new PrivilegedExceptionAction<FileInputStream>() {
             public FileInputStream run() throws FileNotFoundException {

                 return new FileInputStream(location);
             }
         });
     } catch (PrivilegedActionException e) {
         throw (FileNotFoundException) e.getException();
     }
        InputStream is =  fis;
      //long length = file.length();

        byte[] bytes = new byte[fis.available()];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        is.close();
        file=null;




            String encString="";        
            int iFixedLen=110;
            if(bytes.length>=iFixedLen){

                int noOfBlocks=(int)Math.ceil((bytes.length/110.0));
            //  System.out.println("Noof blocks :"+noOfBlocks);
                for(int i=0;i<noOfBlocks;i++){
                    byte[] tempStr=null;
                    if(i==noOfBlocks-1){
                        //System.out.println("Last block");
                        tempStr=new byte[(bytes.length-(i*iFixedLen))];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,(bytes.length-(i*iFixedLen)));
                    }
                    else
                    {   
                        //System.out.println("i : "+i);
                        tempStr=new byte[iFixedLen];
                        //tempStr=new byte[iFixedLen];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,iFixedLen);
                        //tempStr=plainText.substring(0,110) ;
                        //plainText=plainText.substring(110);
                    }
                    encString+= encryptBytes(tempStr,pubKey)+"    ";
                }
                encString=encString.substring(0,encString.length()-1);
                retval=noOfBlocks;
            }else{
                encString=encryptBytes(bytes,pubKey);
                retval=1;

            }

        FileOutputStream fos = null;
    try {
        fos = AccessController.doPrivileged(
         new PrivilegedExceptionAction<FileOutputStream>() {
             public FileOutputStream run() throws FileNotFoundException {

                 return new FileOutputStream(location);
             }
         });
     } catch (PrivilegedActionException e) {
         throw (FileNotFoundException) e.getException();
     }
    fos.write(encString.getBytes());
    fos.close();
    }catch(Exception e){
        e.printStackTrace();
        return 0;
    }
return retval;
}

你几乎犯了书中的每一个错误

        InputStream is =  fis;
      //long length = file.length();

        byte[] bytes = new byte[fis.available()];
InputStream.available()
不是输入流长度的指示器。请参阅Javadoc,它包含一个针对您在这里所做操作的特定警告。在执行任何类型的I/O时,实际上都可以使用固定大小的缓冲区,而且几乎不需要将整个文件读入内存。编译器不会这么做:为什么要这么做

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
               && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
            offset += numRead;
        }
        is.close();
        file=null;
String
不是二进制数据的容器

            int iFixedLen=110;
这个神奇的数字
110
是从哪里来的

            if(bytes.length>=iFixedLen){

                int noOfBlocks=(int)Math.ceil((bytes.length/110.0));
            //  System.out.println("Noof blocks :"+noOfBlocks);
                for(int i=0;i<noOfBlocks;i++){
                    byte[] tempStr=null;
                    if(i==noOfBlocks-1){
                        //System.out.println("Last block");
                        tempStr=new byte[(bytes.length-(i*iFixedLen))];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,(bytes.length-(i*iFixedLen)));
                    }
                    else
                    {   
                        //System.out.println("i : "+i);
                        tempStr=new byte[iFixedLen];
                        //tempStr=new byte[iFixedLen];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,iFixedLen);
                        //tempStr=plainText.substring(0,110) ;
                        //plainText=plainText.substring(110);
                    }
                    encString+= encryptBytes(tempStr,pubKey)+"    ";
                }
                encString=encString.substring(0,encString.length()-1);
                retval=noOfBlocks;
            }else{
                encString=encryptBytes(bytes,pubKey);
                retval=1;

            }

文件是否包含表或其他结构与现代加密完全无关,因为它处理的是已经从“文档”序列化的二进制数据

您正在尝试对原始文件的某些块应用RSA。这是非常棘手的,因为您必须确保输入小于RSA模,并考虑到填充。然后,您必须确保每个密文块具有完全相同的长度,以便在解密期间知道哪些字节属于哪个块。在RSA中,不能保证输出总是具有相同的字节数。与填充输入相比,它可以更小。因此,您需要一致地填充密文块

更好的办法是使用。您可以使用AES加密实际数据,并使用RSA加密随机生成的AES密钥。一个简单的格式应该很容易做到:

4 bytes - Length of the RSA-encrypted AES key (= x)
x bytes - RSA-encrypted AES key
remaining bytes - AES-encrypted file

当然,如果您要进行AES加密,您必须考虑操作模式、填充模式、如何处理初始化向量以及是否要添加经过身份验证的加密。

代码中的某个地方存在错误。即使看不到也可以这么说。@Henry简单的文本内容没有问题,但在图表和表格的情况下,它不能正常工作。因此,在加密图表和表格时,代码中有一个错误。可能还需要什么样的答案?那么如何对包含这些内容的文本文件进行编码解码?@R.chow以正确的方式进行编码。函数encryptBytes()返回加密数据的字符串值。“幻数”110来自一种错误的方法,试图绕过并非所有RSA区块都具有相同长度的事实。可能是110除法,我添加的填充空间正在进行模拟。好吧,感谢Artjom,我将研究混合加密。但是对称密钥有障碍,无法向所有人共享密钥,但在我的情况下,这是不够合适的。你能帮我更有效地填充吗?(在这种情况下)不,你误解了。AES密钥只能由持有RSA私钥的人员恢复。
            if(bytes.length>=iFixedLen){

                int noOfBlocks=(int)Math.ceil((bytes.length/110.0));
            //  System.out.println("Noof blocks :"+noOfBlocks);
                for(int i=0;i<noOfBlocks;i++){
                    byte[] tempStr=null;
                    if(i==noOfBlocks-1){
                        //System.out.println("Last block");
                        tempStr=new byte[(bytes.length-(i*iFixedLen))];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,(bytes.length-(i*iFixedLen)));
                    }
                    else
                    {   
                        //System.out.println("i : "+i);
                        tempStr=new byte[iFixedLen];
                        //tempStr=new byte[iFixedLen];
                        System.arraycopy(bytes, (i*iFixedLen), tempStr, 0,iFixedLen);
                        //tempStr=plainText.substring(0,110) ;
                        //plainText=plainText.substring(110);
                    }
                    encString+= encryptBytes(tempStr,pubKey)+"    ";
                }
                encString=encString.substring(0,encString.length()-1);
                retval=noOfBlocks;
            }else{
                encString=encryptBytes(bytes,pubKey);
                retval=1;

            }
        FileOutputStream fos = null;
    try {
        fos = AccessController.doPrivileged(
         new PrivilegedExceptionAction<FileOutputStream>() {
             public FileOutputStream run() throws FileNotFoundException {

                 return new FileOutputStream(location);
             }
         });
     } catch (PrivilegedActionException e) {
         throw (FileNotFoundException) e.getException();
     }
    fos.write(encString.getBytes());
    fos.close();
    }catch(Exception e){
        e.printStackTrace();
        return 0;
    }
return retval;
}
4 bytes - Length of the RSA-encrypted AES key (= x)
x bytes - RSA-encrypted AES key
remaining bytes - AES-encrypted file