我正在尝试在 Android 中加密音频文件。然后这个文件在服务器端被一个 PHP 脚本解密。在加密文件时,我得到一个 Java.security.InvalidKeyException: Key length not 128/192/256 bits
我的代码如下:
if(getFileToEncrypt.exists()){
System.out.println("-----------FILE EXISTS--------------");
secRand = SecureRandom.getInstance("SHA1PRNG");
key = new BigInteger(128, secRand).toString(16);
rawKey = key.getBytes();
sKeySpec = new SecretKeySpec(rawKey, "AES");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
inputStream = new FileInputStream(getFileToEncrypt);
byte[] byts = new byte[(int) getFileToEncrypt.length()];
inputStream.read(byts);
inputStream.close();
encrypted = cipher.doFinal(byts);
encryptedFile.createNewFile();
ostr = new FileOutputStream(encryptedFile);
ostr.write(encrypted);
ostr.close();
System.out.println("--------FILE ENCRYPTION COMPLETE----------");
}
这有时会起作用,但否则会以异常告终。 堆栈跟踪如下:
07-08 15:38:02.865: W/System.err(16391): java.security.InvalidKeyException: Key length not 128/192/256 bits.
07-08 15:38:02.865: W/System.err(16391): at com.android.org.bouncycastle.jce.provider.JCEBlockCipher.engineInit(JCEBlockCipher.java:570)