任何加密和解密Android内部文件的优化方式

时间:2012-05-10 14:05:16

标签: java android file

这是用于加密和解密过程的当前代码,它的工作方式也是如此,除了加密和解密的时间很长。

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class EncryptAndDecrypt{

   public static void main(String[] args) {
    try {
        String key = "myencryptedpass123";

        FileInputStream fis = new FileInputStream("File_to_encrypt.mp4");
        FileOutputStream fos = new FileOutputStream("Encrypted_file.mp4");
        encrypt(key, fis, fos);

        FileInputStream fis2 = new FileInputStream("Encrypted_file.mp4");
        FileOutputStream fos2 = new FileOutputStream("File_to_decrypt.mp4");
        decrypt(key, fis2, fos2);
    } catch (Throwable e) {
        e.printStackTrace();
    }
}

public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable {
    encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os);
}

public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable {
    encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os);
}

public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable {

    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey desKey = skf.generateSecret(dks);
    Cipher cipher = Cipher.getInstance("DES"); 

    if (mode == Cipher.ENCRYPT_MODE) {
        cipher.init(Cipher.ENCRYPT_MODE, desKey);
        CipherInputStream cis = new CipherInputStream(is, cipher);
        doCopy(cis, os);
    } else if (mode == Cipher.DECRYPT_MODE) {
        cipher.init(Cipher.DECRYPT_MODE, desKey);
        CipherOutputStream cos = new CipherOutputStream(os, cipher);
        doCopy(is, cos);
    }
}

public static void doCopy(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[64];
    int numBytes;
    while ((numBytes = is.read(bytes)) != -1) {
        os.write(bytes, 0, numBytes);
    }
    os.flush();
    os.close();
    is.close();
}

}

现在我希望有更好的解决方案......

2 个答案:

答案 0 :(得分:2)

绝对使用更大的缓冲区,但加密主要受CPU限制,因此不太可能加快速度。另外

  • 如果您不担心安全漏洞,最好完全放弃加密
  • 您正在使用DES,不要
  • 您使用人类可读的字符串作为密钥 - 不要
  • 使用单独的Cipher
  • 实例进行加密和解密后,该代码可能无效
  • 如果您使用默认设置,则表示您正在设置自己遇到麻烦。使用类似Cipher.getInstance("AES/CBC/PKCS5Padding")
  • 的内容
  • 如果您希望在Android上运行,为什么要在桌面上进行测试?

是的,如果你将密钥包含在应用中,那么你的DRM'用处不大。

答案 1 :(得分:0)

绝对使用更大的缓冲区,但加密主要受CPU限制,因此不太可能加快速度。另外

如果你“不打扰安全漏洞”,最好完全放弃加密 你使用的是DES,不要 你使用人类可读的字符串作为键 - 不要 一旦使用单独的Cipher实例进行加密和解密,该代码可能无效 如果你使用默认设置,那么你就是在设置自己的麻烦。使用像Cipher.getInstance(“AES / CBC / PKCS5Padding”)

这样的东西
public static void encrypt(File inputFile, File outputFile) throws CryptoException {
        doCrypto(Cipher.ENCRYPT_MODE, inputFile, outputFile);
    }

    public static void decrypt(File inputFile, File outputFile) throws CryptoException {
        doCrypto(Cipher.DECRYPT_MODE, inputFile, outputFile);
    }

    private static void doCrypto(int cipherMode, File inputFile, File outputFile) throws CryptoException {
        try {
            Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
            Cipher cipher = Cipher.getInstance(TRANSFORMATION);
            cipher.init(cipherMode, secretKey);

            FileInputStream inputStream = new FileInputStream(inputFile);
            byte[] inputBytes = new byte[(int) inputFile.length()];
            inputStream.read(inputBytes);

            byte[] outputBytes = cipher.doFinal(inputBytes);

            FileOutputStream outputStream = new FileOutputStream(outputFile);
            outputStream.write(outputBytes);


            inputStream.close();
            outputStream.close();
        } catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
            e.getMessage();
         throw new CryptoException("Error encrypting/decrypting file", e);
        }
    }