Search code examples
javaencryptioncryptographytimestampblowfish

How can I add a time stamp to the Blowfish cryptographic algorithm in Java?


I'm running Java in Eclipse, fyi.

I have plaintext which is encrypted using Blowfish and decrypted on another end. I want to add a time stamp, such that the encrypted text is different each time for the same plaintext.

How can I add the a time stamp to the blowfish algorithm in Java, such that I can decrypt it on another end?

Thank you.

Here is my encryption code:

import BlowfishJ.*;


public class EncryptBlowFishTest {
/**
 * @param args
 */
public static void main(String[] args) {

    long CBCIV = 0x0x765904567324590L;

    String pwd = "1234567890";
    int pwdLength = password.length();

    // generate key
            byte[] testkey = new byte[5];


            for (int i = 0; i < testkey.length; i++)
                testkey[i] = (byte) (i + 1);


    BlowfishCBC blowfishcbc = new BlowfishCBC(testkey, 0, testkey.length, CBCIV);

    byte[] tempBuffer = pwd.getBytes();

    // align to the next 8 byte border
            byte[] msgBuffer;
            int n = pwdLength & 7;

            if (n != 0) {
                msgBuffer = new byte[(pwdLength & (~7)) + 8];
                System.arraycopy(tempBuffer, 0, msgBuffer, 0, pwdLength);

                for (int i = pwdLength; i < msgBuffer.length; i++)
                    msgBuffer[i] = 0;
            }
            else {
                msgBuffer = new byte[pwdLength];
                System.arraycopy(tempBuffer, 0, msgBuffer, 0, pwdLength);
            }

            byte[] showCBCIV = new byte[BlowfishCBC.BLOCKSIZE];
            blowfishcbc.getCBCIV(showCBCIV, 0);

            blowfishcbc.encrypt(msgBuffer, 0, msgBuffer, 0, msgBuffer.length);

            String encryptedPwd = BinConverter.bytesToBinHex(msgBuffer);

            System.out.println(encryptedPwd);

    }
}

Solution

  • Use a random IV, instead. Just generate a random sequence of bytes of the appropriate length and use it as your IV. Send the IV unencrypted, among with your encrypted message.

    Using a random IV is a standard practice (PKCS#5).