我是android的初学者。我想在android中加密和解密视频文件,所以我在网上搜索了很多,发现下面的代码。该代码生成0 KB文件。这段代码有什么问题?
String filePath = "/storage/emulated/0/Download/r.mp4";
String outPath = "/storage/emulated/0/Download/r-enc.mp4";
String inPath = "/storage/emulated/0/Download/r-dec.mp4";
static void encrypt() throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(filePath);
FileOutputStream fos = new FileOutputStream(outPath);
SecretKeySpec sks = new SecretKeySpec("Programchi".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[128];
while ((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
fis.close();
}
static void decrypt() throws IOException, NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(outPath);
FileOutputStream fos = new FileOutputStream(inPath);
SecretKeySpec sks = new SecretKeySpec("oldfeelwasverynb".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[128];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}
也在logcat中,我得到以下错误
W / System.err:原因:java.lang.IllegalArgumentException:密钥长度不是128/192/256位。