encrypt decrypt file program using caeser cipher

the package should open a file, encrypt it, and write out the encrypted data to another file. Then the package should be able to open an encrypted file, decrypt it, and write out the decrypted
data to a new file. if anyone could help me with this. give me an outline of what i need and i will try to fill it in to come up with a good program. thanks.
rayissAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rayissAuthor Commented:
i found this code but it only encrypt a message i need to encrypt a file like i mentioned above. can you help me add to this code to make it work.
package ceasercipher;

import javax.swing.*;

public class Cipher{


private String inputString;

private int intOffset;

public Cipher() {

inputString = null;

intOffset = 0;

}

public static void main(String [] args) {

Cipher c2 = new Cipher();

String tmp = c2.encrypt();

System.out.println( tmp ); //System.out.println( decrypt( tmp ) );

System.exit(0);

}

public String encrypt() {

inputString = ((String)JOptionPane.showInputDialog
                ("Enter the message you wish to encrypt") ).toLowerCase().trim();

intOffset = Integer.parseInt(JOptionPane.showInputDialog("Enter Key") );

                String ALPHABET  = "abcdefghijklmnopqrstuvwxyz";


StringBuffer sb = new StringBuffer();

int holder = inputString.length();

for(int i = 0; i < holder; i++)

{

String tmp = ""+inputString.charAt(i);

int offset = ALPHABET.indexOf(tmp);

offset += intOffset;

if( offset > 25 )

{

int newOffset = 0;

newOffset = offset % 25;

sb.append( ALPHABET.charAt(newOffset) );

}

else

{

sb.append( ALPHABET.charAt(offset) );

}

//

}//forloop

return sb.toString();

}


}//class

Open in new window

0
rayissAuthor Commented:
can anyone help please.
0
cmalakarCommented:
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cmalakarCommented:
0
rayissAuthor Commented:
first thank you for replying. Now can u help me put together one using caeser cipher to encrypt and decrypt a file like i mentioned above. Thanks again
0
cmalakarCommented:
Please follow the example I provided.. and update your code..
If you face any problem in updating it..  Let me know.. Ready to help.
0
rayissAuthor Commented:
Ok im runnin to work are you going to be available after 6
0
cmalakarCommented:
I am in India.. available @ my "early morning times".. and evening times
0
rayissAuthor Commented:
Ok well i have until wed.i will work on it aNd post to you. Now what should i mainly update
0
cmalakarCommented:
Use the attached encrypt and decrypt methods.. in you method.

public String encrypt(String input){    
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec);
 
    byte[] encryptedText = m_encrypter.doFinal(input.getBytes());

   return new String(encryptedText);
}

public String decrypt(String encryptedText){
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();

    // for CBC; must be 8 bytes
    byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 };

    AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector);
    Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding");
    m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec);

    byte[] decryptedText = m_decrypter.doFinal(encryptedText.getBytes());

    return new String(decryptedText);
}

Open in new window

0
rayissAuthor Commented:
Ok what about opening a file Encrypting it and write it to another file.im just not a programmer but i have to make this program work..
0
cmalakarCommented:
Find various examples of read and write to files here.

http://www.exampledepot.com/taxonomy/term/164
0
cmalakarCommented:
aikimark,

Apologies.. didn't catch that..

What can be done ?
0
cmalakarCommented:
sure
0
rayissAuthor Commented:
Well anyway the code he gave i can not use i need to start from scratch and build my code code istead of grabing code from the internet. can u tell me please all i need to start writing this program and u can tell me what im doing wrong as im going.thanks again
0
rayissAuthor Commented:
well i have this code now it's doing something but i don't know if it's doing what i'm asking for. is this code encrypting right on caeser cipher way? and for decrypting i can use the negative of key used for decrypting. now when it asks me to input the file it works but the output file it says that thye system cannot find the specified file. what should i do? please help.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class Encryptor {
	public Encryptor(int aKey)
	{
		key = aKey;
	}
	public void encryptFile(String inFile, String outFile)
	throws IOException
	{
		InputStream in = null; 
		OutputStream out = null;
		
		try 
		{
			in = new FileInputStream(inFile);
			out = new FileOutputStream(outFile);
			encryptStream(in, out);
		}
		finally
		{
			if (in != null) in.close();
			if (out != null) out.close();
		}
	}
	private void encryptStream(InputStream in, OutputStream out) 
		throws IOException
		{
		boolean done = false;
		while (!done)
		{
			int next = in.read();
			if (next == -1) done = true;
			else 
			{
				byte b = (byte) next;
				byte c = encrypt(b);
				out.write(c);
				
			}
		}
		
	}
	private byte encrypt(byte b) {
		return (byte) (b + key);
	}
	private int key;
}
	****************
import java.io.IOException;
import java.util.Scanner;


public class EncryptorTester{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		try
		{
			System.out.print("Input file: ");
			String inFile = in.next();
			System.out.print("Output file: ");
			String outFile = in.next();
			System.out.print("Encryption key: ");
			int key = in.nextInt();
			Encryptor crypt = new Encryptor(key);
			crypt.encryptFile(inFile, outFile);
		}

		catch (IOException exception)
		{
			System.out.println("Error processing file: "
					+ exception);
		}
	}
}

Open in new window

0
cmalakarCommented:
>>it says that thye system cannot find the specified file. what should i do

What is the input you are giving for Input file  & output file?
Does input file exists in your machine ?
0
rayissAuthor Commented:
im giving the file name. i created a file and i put it in the src folder.
0
cmalakarCommented:
try to give complete file path, instead of just file name.
0
rayissAuthor Commented:
ok where i do that? and is the encryption like we are to use a Caesar cipher lwith random sequence and character insertion. will my code do this.
0
cmalakarCommented:
>> im giving the file name

When your program asks for "Input file:" give the complete path to the file as "C:\temp\input.txt" assuming windows.

Your encryption looks good.. Please proceed, if you face any problem while decryption, then let me know.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rayissAuthor Commented:
what about the output should that be another new file in the same location.
0
cmalakarCommented:
>>what about the output should that be another new file in the same location

Yes.. because you want the output to write it to a new file.
Even though the output file doesnot exists, the FileOutputStream will create one for you.
0
rayissAuthor Commented:
im glad your back. well when it's asking me for the input im entering C:\temp\security\decrypt\bin\rayiss.txt
because that's where the file is but for the output what should i enter i tried not to enter anything but it won't let me and when i entered the same thing as the input and 2 for encryption key it said that system cannot find the file rayiss. what do you think im doing wrong.
0
rayissAuthor Commented:
i got it i got it i wasnt supposed to use rayiss as a file name.thank you though. now can you tell me what should i do for decryption i mean do i need a new method or what.
0
cmalakarCommented:
Yes.. go ahead and write a new method for decryption.
0
rayissAuthor Commented:
But i tried to use the negative of the encryption key nd  it worked
0
cmalakarCommented:
please post the code you have written for decryption.. I will check
0
rayissAuthor Commented:
i didnt write one i just used the negative of the encryption key nd it decryptwd the text back to plaintext
0
cmalakarCommented:
You cannot directly use the ecnryptedValue - key..

You should use the attached logic.


public static void main(String args[]){
		byte value  = 120;
		
		byte key = 10;
		byte encryptedValue = (byte) (value + key);
		System.out.println("Original Value: " + value);
		System.out.println("Encrypted Value: " + Byte.toString(encryptedValue));
		if(encryptedValue < 0){
			System.out.println("Decrypted Value: " + (256 + encryptedValue - key));
		}else{
			System.out.println("Decrypted Value: " + (encryptedValue - key));
		}
	}

Open in new window

0
rayissAuthor Commented:
so i should use this main instead of the main i have in my previous code?
0
cmalakarCommented:
no.. this is the sample code.. I shown.. how a byte encrypted using a key is decrypted back to its original value

Try to run the above  example.. with different values.. you will come to know the decryption logic
0
rayissAuthor Commented:
so will something like this work or do i have to add more to it to work.
private byte decrypt(byte b){
	return(byte)(b-key);
	}

Open in new window

0
cmalakarCommented:
not exactly..

probably it shoud look like..

private byte decrypt(byte b){
        if(b < 0){
                        return (byte)(256 + b- key);
                }else{
                        return (byte)(b- key);
                }
        }

Open in new window

0
rayissAuthor Commented:
So how im supposed to fix that.thanks
0
cmalakarCommented:
The above code shows.. how your decrypt function should be..

w
0
rayissAuthor Commented:
good
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.