Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
Android 在另一台设备上读取加密文件?_Android_File_Encryption - Fatal编程技术网

Android 在另一台设备上读取加密文件?

Android 在另一台设备上读取加密文件?,android,file,encryption,Android,File,Encryption,假设应用程序使用以下方法安全地写入文件。用户复制文件并粘贴到另一台设备上的同一目录中。应用程序能否读取新设备上的文件?我的意思是,以下文件读/写过程设备是否特定 static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream

假设应用程序使用以下方法安全地写入文件。用户复制文件并粘贴到另一台设备上的同一目录中。应用程序能否读取新设备上的文件?我的意思是,以下文件读/写过程设备是否特定

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
    // Careful when taking user input!!! http://stackoverflow.com/a/3452620/1188357
    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();
}



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();
}


//put the last part in the method
static void generate() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException{
    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();
}

它不是特定于设备的,而是使用AES加密的,AES是一种常用的加密算法,可用于大多数系统和大多数计算机语言


如果已知加密密钥,则可以在第二个系统上解密文件。

它不是特定于设备的,而是使用AES加密的,AES是一种常用的加密算法,在大多数系统和大多数计算机语言中都可用

如果已知加密密钥,则可以在第二个系统上解密该文件