Skip to Main Content

Java Programming

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!

Blowfish Algorithm

807603May 12 2006 — edited Dec 17 2007
In my previous mail I had sent the code i used for encryption and decryption. For our project, Decryption will not neccessary be done at the same time as encryption.
Thus I wanted to know how do I save the key. But that will cause an overhead as there will be many files encrypted. Is it possible that instead of using the generateKey() function everytime, we use a static key for each user, say for example the username-password of the user as a key ??

Also please guide on the exception I am getting everytime I try to decrypt with the present code. As stated before, the exception I get is

"javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher. Error writing to file on Decryption"

While getting the cipher I have stated to use "PKCS5Padding", why do I get this exception ? I saw that each time the line of text is read from the file for decryption, its length is not a multiple of 8 and thus the exception occurs. What should I do to solve this error??

Resending the code :

import java.lang.*;
import javax.crypto.spec.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import java.util.*;
import java.io.*;

public class Encrypt {
Cipher _cipher;
SecretKey key;

Encrypt()
{
try
{
KeyGenerator keygen = KeyGenerator.getInstance("Blowfish");
key = keygen.generateKey();
_cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
}
catch(Exception e)
{
System.out.println("Exception Occured : "+e);
}
}

public String encrypt(String str)
{
try
{
cipher.init(Cipher.ENCRYPTMODE, key);

// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");

// Encrypt
byte[] enc = _cipher.doFinal(utf8);

// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (Exception e)
{
//System.out.println(e);
}
return null;
}

public String decrypt(String str)
{
try
{
cipher.init(Cipher.DECRYPTMODE, key);

// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);

// Decrypt
byte[] utf8 = _cipher.doFinal(dec);

// Decode using utf-8
return new String(utf8, "UTF8");
}
catch (Exception e)
{
System.out.println(e);
}
return null;
}

public static void main(String argv[]) // for execution as application
{
Encrypt encrytInstance = new Encrypt();
FileOutputStream encryptionDecryptionOutput;
FileInputStream input;
DataOutputStream out;
DataInputStream in;

/*----------------------For encrypting the text data file----------------------*/
try
{
input = new FileInputStream("myfile.txt");
in = new DataInputStream(input);
encryptionDecryptionOutput = new FileOutputStream("EncryptionOutput");
out = new DataOutputStream(encryptionDecryptionOutput);

while(in.available() != 0)
{
out.writeBytes(encrytInstance.encrypt(in.readLine()));
}
in.close();
out.close();
}
catch(Exception e)
{
System.err.println("Error writing to file on Encryption");
}

/*-------------------For decrypting the encrypted data file------------------*/
try
{
input = new FileInputStream("EncryptionOutput");
in = new DataInputStream(input);
encryptionDecryptionOutput = new FileOutputStream("DecryptionOutput.txt");
out = new DataOutputStream(encryptionDecryptionOutput);

while(in.available() != 0)
{
out.writeBytes(encrytInstance.decrypt(in.readLine()));
}
in.close();
out.close();
}
catch(Exception e)
{
System.err.println("Error writing to file on Decryption");
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2008
Added on May 12 2006
7 comments
804 views