我正在使用下面的代码来使用 ECB 和 PKSC5 填充实现三重 DES 编码。我使用的密钥存储在原始文件夹中名为 key 的文件中。我遇到了异常-
java.security.InvalidKeyException: key size must be 128 or 192 bits
为什么我会得到这个异常,我哪里出错了?
public byte[] encrypt(String message) throws Exception {
getResources().getIdentifier("key",
"raw", getPackageName());
byte[] bytes = new byte[1024];
try {
BufferedInputStream buf = new BufferedInputStream(getResources().openRawResource(
getResources().getIdentifier("key",
"raw", getPackageName())));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final SecretKey key = new SecretKeySpec(bytes, "DESede/ECB/PKCS5Padding");
final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
return cipherText;
}