Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

File not getting decrypted ?

843811Dec 22 2008 — edited Dec 22 2008
Hello everyone ,

I have file encryption decryption code which is working perfectly on windows.But when i deploy same code to Linux machine it encrypts file successfully but not able to decrypt it. Can you please suggest something in this regards. In some post i have seen that it could be because of decryption key not properly generated and output could be garbled. But i think as linux machine is having one JVM and as same JVM is used to generated encryption key so decryption key should not be different (i think so). I have used "DES/ECB/PKCS5P" algorithm . and here is the relevant code.

Decryption code
FileInputStream fileInputStream = new FileInputStream(decryptFile);

//Creating KeyGenerator
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

SecureRandom secureRandom = new SecureRandom();
secureRandom.setSeed(userkey.getBytes());

keyGenerator.init(secureRandom);
key = keyGenerator.generateKey();

Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);

decryptedFile = File.createTempFile(decryptFile.getName(), ".decrypted");
FileOutputStream fileOutputStream = new FileOutputStream(decryptedFile);

CipherOutputStream cipherOutputStream = new CipherOutputStream(fileOutputStream, cipher);

byte[] b = new byte[128];
int i = fileInputStream.read(b);
while (i != -1) {
cipherOutputStream.write(b, 0, i);
				i = fileInputStream.read(b);
			}
			fileInputStream.close();
			cipherOutputStream.flush();
			
and encryption code :
FileInputStream fileInputStream = new FileInputStream(originalFile);

			//Creating KeyGenerator
			KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");

			SecureRandom secureRandom = new SecureRandom();
			secureRandom.setSeed(userkey.getBytes());

			keyGenerator.init(secureRandom);
			key = keyGenerator.generateKey();

			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			//String name = "EncryptedFile" + Math.round(Math.random() * 10000);

			//temporary file to right encrypted content
			encryptedFile = File.createTempFile(originalFile.getName(), ".encrypted");

			CipherInputStream cis = new CipherInputStream(fileInputStream, cipher);
			FileOutputStream fileOutputStream = new FileOutputStream(encryptedFile);

			//right content to temporary file 
			byte[] b = new byte[256];
			int i = cis.read(b);
			while (i != -1) {
				fileOutputStream.write(b, 0, i);
				i = cis.read(b);
			}

			//Close open stream
			fileInputStream.close();
			cis.close();
i hope i have made some sense up there and expecting help ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 19 2009
Added on Dec 22 2008
3 comments
267 views