I am using below code to implement Triple DES encoding with ECB and PKSC5 Padding.The secret key which i am using is stored in a file named key in raw folder.I am getting below exception-
java.security.InvalidKeyException: key size must be 128 or 192 bits
Why am i getting this exception and where am i going wrong?
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;
}