Using CipherOutputStream in Java - java

I'm trying to use an AES cipher to encrypt some bytes but it's returning a silent error, meaning I enter something like:
byte[] raw = new String("Test","UTF8").getBytes("UTF8");
and it won't return anything. I think the problem is the ByteArrayInput/OutputStreams but I don't know how to do it any other way..
Here's the code in question.
public byte[] encrypt(byte[] in) {
byte[] encrypted = null;
try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
ByteArrayInputStream bais = new ByteArrayInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream(bais.available());
CipherOutputStream os = new CipherOutputStream(baos, aesCipher);
copy(bais, os);
os.flush();
byte[] raw = baos.toByteArray();
os.close();
encrypted = Base64.encodeBase64(raw);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}
return encrypted;
}
Here's another function in the same class that does work:
public void encrypt(File in, File out) {
try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
FileInputStream is;
is = new FileInputStream(in);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);
copy(is, os);
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void copy(InputStream is, OutputStream os) throws IOException {
int i;
byte[] b = new byte[2048];
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
}

The first thing that catches my eye is this line:
aesCipher.getInstance("AES/CBC/PKCS5Padding");
Assuming aesCipher is a variable of type Cipher, you are calling here the static Cipher.getInstance and throwing away the result (not assigning it to any variable). I.e. this line has no effect at all, aesCipher is the same as before this line.
If it was null before, then it still is null, and the next line (which calls a non-static method) will give you a NullPointerException. If your code is silently gobbling unknown exceptions (this might be outside the code shown), this is a general problem.
Other than this, I suppose that the flush on the CipherOutputStream does not really flush the whole buffer, but only as many blocks that can be written without adding any padding. Use close() instead of flush() here (which also seems to work in the second example).
A general remark: A small self-contained complete compilable example would have enabled me to try it and give you a definite answer, instead of only guessing. For example, "does not return anything" is not a good description for the behavior of your method - does the method return null, an empty array, throw an exception, block eternally?

Related

Java: Not Catching deliberate BadPaddingException

My app prompts the user for the password that was used to encrypt a control file. If the wrong password is entered, the app responds by creating a new control file. Therefore I need to catch a BadPaddingException so I can trigger the appropriate response.
Here's the code snippet that should generate the exception
private void existingHashFile(String file) {
psUI = new passwordUI(new javax.swing.JFrame(), true, "existing");
psUI.setVisible(true);
this.key = passwordUI.key;
try {
hash.decryptHashFile(file, this.key); //this is line 240
} catch (BadPaddingException ex) {
Logger.getLogger(homePage.class.getName()).log(Level.SEVERE, null, ex);
//then the file was not decrypted
System.out.println("BPE 2!");
} catch (Exception ex) {
Logger.getLogger(homePage.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("BPE 3!");
}
For completeness, here's the decryptHashFile method that is called above
public void decryptHashFile(String filename, String key) throws BadPaddingException, UnsupportedEncodingException, Exception {
FileInputStream fis = null;
FileOutputStream fos = null;
CipherInputStream cis = null;
String outFile = filename.replace(".enc", "");
byte[] byteKey = key.getBytes("UTF-8");
Cipher cipher = getCipher(byteKey, "decrypt");
try {
fis = new FileInputStream(filename);
fos = new FileOutputStream(outFile);
cis = new CipherInputStream(fis, cipher);
byte[] buffer = new byte[1024];
int read = cis.read(buffer);
while (read != -1) {
fos.write(buffer, 0, read);
read = cis.read(buffer); //this is line 197
}
} catch (IOException ex) {
Logger.getLogger(hashListClass.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
fos.close();
}
if (cis != null) {
cis.close();
}
if (fis != null) {
fis.close();
}
}
}
When I deliberately enter the wrong password, I see this stack trace, but my code (I've used a println in the example) isn't executed:
Dec 02, 2017 2:31:34 PM appwatch.hashListClass decryptHashFile
SEVERE: null
java.io.IOException: javax.crypto.BadPaddingException: Given final block not properly padded
at javax.crypto.CipherInputStream.getMoreData(CipherInputStream.java:121)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:239)
at javax.crypto.CipherInputStream.read(CipherInputStream.java:215)
at appwatch.hashListClass.decryptHashFile(hashListClass.java:197)
at appwatch.homePage.existingHashFile(homePage.java:240)
CipherInputStream.read (your line 197) throws IOException, not BadPaddingException, therefore the exception is caught by the subsequent catch (IOException ex).
After that you are not explicitly throwing other exceptions, so there is nothing else to catch after decryptHashFile.

Spring service layer write file

I have this method in spring service layer. But it cannot write to file and there is no exception. What's wrong with my code?
public void addLineToFile(String file, String line) {
try {
ClassPathResource cpr = new ClassPathResource(file);
FileOutputStream outputStream = new FileOutputStream(cpr.getFile());
outputStream.write(line.getBytes());
outputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(CustomerToFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CustomerToFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
Should call the flush() of FileOutputStream before close of stream.

How do I resolve the ClassCastException while retrieving an array of objects from a .txt file?

I have created an array of objects of type Employee and i am putting the objects in a .txt file.
Below is the method that accepts Employee objects a parameter and puts it into the .txt file
public void putDataintoFile(Employee[] obj) {
File file = new File("employeedetails.txt");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(obj);
oos.close();
fos.close();
}
catch (IOException ex) {
Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
}
}
This above method is invoked from a seperate "Execetor class " which then calls my getDataFromFile() method below
public void getDataFromFile() {
System.out.println("Reached HERE");
try {
FileInputStream fin = new FileInputStream("employeedetails.txt");
try {
ObjectInputStream oin= new ObjectInputStream(fin);
try {
Employee e =(Employee)oin.readObject();
System.out.println("Reached HERE");
System.out.println(e.toString());
} catch (ClassNotFoundException ex) {
Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
}
oin.close();
fin.close();
}
catch (IOException ex) {
Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
}
}
catch (FileNotFoundException ex) {
Logger.getLogger(EmployeeService1.class.getName()).log(Level.SEVERE, null, ex);
}
}
On executign I get an error which says.
Exception in thread "main" java.lang.ClassCastException: [LMypackage.Employee; cannot
be cast to Mypackage.Employee
at Mypackage.EmployeeService1.getDataFromFile(EmployeeService1.java:225)
at Mypackage.Executor.main(Executor.java:71)
I have implemented Serializable interface in my Employee Classs
Can anyone help me ?
You're writing an array and trying to read a single object.
This is stated in the exception message, which leads to the read/write code, which is fairly obvious despite all the superfluous vertical whitespace.
Also, in general, don't call not-text files .txt.

Open the Keystore file in Java with password

I am trying to open a Password protected keystore file which I have it in the jre security Directory. I am using Bouncy Castel as my JCE. For some reason, I seem to get exceptions for which I have no clue as to why. Please help. Thanks. Here is what I have:
Code Snippet:
String passwd = "abcd123";
try {
String file = "C:\\Program Files\\Java\\jre7\\lib\\security\\keystore.ssm";
System.out.println("file =" + file);
File keystoreFile = new File(file);
if (keystoreFile.exists()) {
KeyStore store = KeyStore.getInstance("PKCS12", "BC");
//KeyStore store = KeyStore.getInstance("PKCS12");
FileInputStream in = new FileInputStream(keystoreFile);
store.load(in, passwd.toCharArray());
// Key key = store.getKey("mykeystore",passwd.toCharArray());
}
} catch (KeyStoreException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchProviderException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
} /*catch (UnrecoverableKeyException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
} */ catch (IOException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
} catch (CertificateException ex) {
Logger.getLogger(SimpleBCTest.class.getName()).log(Level.SEVERE, null, ex);
}
Exception I get:
Exception in thread "main" java.lang.ClassCastException: org.bouncycastle.asn1.DLSequence cannot be cast to org.bouncycastle.asn1.ASN1Integer

"javax.crypto.IllegalBlockSizeException" when decrypt data from database

I want to store data (encrypted with DES) and then take the encrypted data from database and present it as a list. But I have a problem. Here is the code.
public void EncryptDemo(){
try {
FileInputStream keyfis = new FileInputStream("mainkey.key");
byte[] encodedKey = new byte[keyfis.available()];
keyfis.read(encodedKey);
keyfis.close();
Key KeyFromFile = new SecretKeySpec(encodedKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
String text=txtToEncryptData.getText(), output;
cipher.init(Cipher.ENCRYPT_MODE, KeyFromFile);
DataDemo = cipher.doFinal(text.getBytes());
InsertIntoDataBase();
//I store it as varbinary in database
} catch (FileNotFoundException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void DecryptDemo(){
try {
FileInputStream keyfis = new FileInputStream("mainkey.key");
byte[] encodedKey = new byte[keyfis.available()];
keyfis.read(encodedKey);
keyfis.close();
Key KeyFromFile = new SecretKeySpec(encodedKey, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, KeyFromFile);
String sql = "{call SelectAll}";
CallableStatement call = conn.prepareCall(sql);
call.execute();
ResultSet result = call.getResultSet();
DefaultListModel model = new DefaultListModel();
while(result.next()){
DataDemo = result.getBytes("TestData");
byte[] plainText = cipher.doFinal(DataDemo);
String after = new String(plainText);
model.addElement(after);
}
lstDecryptResult.setModel(model);
} catch (SQLException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalBlockSizeException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
} catch (BadPaddingException ex) {
Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
The encrypt and storing is allright. But when I take data from database, I get this error when decrypt (at byte[] plainText = cipher.doFinal(DataDemo);)
Jul 19, 2013 11:40:05 AM databaseencryptdecryptdemo.MainGUI DecryptDemo
SEVERE: null
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
Anyone have a solution for this???
You should divide DataDemo variable into 8 byte pieces.
public List<Byte[]> divideInto8(Byte[] bytes) {
int length = bytes.length;
List<Byte[]> returnValues = new ArrayList<Byte[]>();
for (int i = 0; i < length; i = i + 8) {
Byte[] thebytes = new Byte[8];
for (int j = 0; j < 8; j++) {
thebytes[j] = bytes[i * 8 + j];
}
returnValues.add(thebytes);
}
return returnValues;
}
I found the solution. But I think it's not the best.
I changed the type of table DataDemo from varbinary to image and everything became alright. But my data size store in database is heavier (about 4 times) than the oigin data.
But at least I solved my problem.
Does anyone have a better solution? I willing to hear from you.

Categories

Resources