在android中存储pdf文件的最佳方式

时间:2013-11-20 05:11:13

标签: android

在我们的应用程序中,如果登录成功,我们将从服务器获取字节数组。我们将这些字节数组转换为PDF格式并将这些文件存储到使用内部存储器的DB中。如果文件是KB,应用程序正常工作但文件大小增加MB然后应用程序发出内存错误。请告诉我如何处理这种情况?如何将文件存储到SD卡中以保持安全性。外部用户看不到它。 请帮忙。

谢谢, AA

2 个答案:

答案 0 :(得分:1)

你应该看看: CipherInputStreamCipherOutputStream。它们用于加密和解密字节流。

编辑:所以你走了!

我有一个名为cleartext的文件。该文件包含:

Hi, I'm a clear text.
How are you?
That's awesome!

现在,您有一个encrypt()功能:

static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream("data/cleartext");
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream("data/encrypted");

    // Length is 16 byte
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

执行此功能后,应该有一个文件名encrypted。该文件包含加密字符。

对于解密,您拥有decrypt功能:

static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream("data/encrypted");

    FileOutputStream fos = new FileOutputStream("data/decrypted");
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}

执行解密后,应该有一个名为decrypted的文件。该文件包含自由文本。

编辑:你写的是“noob”但是根据加密的使用情况,如果你没有以正确的方式做这件事,你可能会造成很大的伤害。了解你的工具!

使用CipherOutputStream Oracle documentation

SecretKeySpec skeySpec = new SecretKeySpec(y.getBytes(),“AES”);

FileInputStream fis;
FileOutputStream fos;
CipherOutputStream cos;
// File you are reading from
fis = new FileInputStream("/tmp/a.txt");
// File output
fos = new FileOutputStream("/tmp/b.txt");

// Here the file is encrypted. The cipher1 has to be created.
// Key Length should be 128, 192 or 256 bit => i.e. 16 byte
SecretKeySpec skeySpec = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); 
Cipher cipher1 = Cipher.getInstance("AES");  
cipher1.init(Cipher.ENCRYPT_MODE, skeySpec);
cos = new CipherOutputStream(fos, cipher1);
// Here you read from the file in fis and write to cos.
byte[] b = new byte[8];
int i = fis.read(b);
while (i != -1) {
    cos.write(b, 0, i);
    i = fis.read(b);
}
cos.flush();

因此,加密应该有效。当您反转该过程时,您应该能够读取解密的字节。

答案 1 :(得分:0)

存储在SD卡上将使精明的用户可以访问该文件。存储在数据库中将为您提供类似于您提到的错误。可能最好的想法是去内部存储。这不是完美的(root用户可以浏览到文件),但它可能是最好的选择。

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

相关问题