473,419 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Encrypt/Decrypt

bferguson94
Design a program that allows the user to encrypt or decrypt a file.

This means you will need to ask the user the direction to shift (left or right) and the number of places to shift (should they choose to encrypt a file). The number of places a file can be shifted is anywhere from 0 to 2 billion. You may assume the input from the user will be in this range (the user will NOT enter a negative number). If the user chooses to encrypt, present them with a menu that finds out which direction to shift for encryption (left or right). When encrypting a file, before you write the encrypted data to an output file, you'll need to get the name of the file to encrypt and what to call the encrypted file from the user. The first thing in the file you build that is encrypted is the direction of the shift ('left' or 'right' -- use these words), followed by a space, followed by the number of spaces shifted. This information will be used by the decrypt portion of this program. Finally, you can proceed to write the encrypted data to the file.

To decrypt a file, get the name of the file to decrypt from the user and the name the user wants to call the decrypted file. Use the direction and places found on the first line in the encrypted file to determine how to decrypt. You may assume the first line of the file will contain the direction of shift followed by the number of places shifted. You may also assume the file to decrypt exists. Be sure NOT to include the shift direction and number of spaces in the decrypted file!

NOTE: once a file has been encrypted or decrypted, be sure and close that file!

You will/may need a total of three (3) files for this project: the original input file, the file you created from encrypting, and the file created from decrypting. Depending on what the user chooses to do, you may not ever create an encrypted and decrypted file. Incidentally, the decrypted file should end up looking the same as the original file...

The only output to the monitor in this program is: (1) the menu requesting input to encrypt, decrypt, or quit (2) request for the name of an input file, name to call encrypted file, and the direction of shift and number of places if encrypt is chosen (3) request for name of the encrypted file and what to call the decrypted file if decrypt is chosen (4) error messages resulting from a file not opening.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;//file, fileNotFoundException, printStream
  2. import java.util.Scanner;
  3. public class FileEncrypter
  4. {
  5.     public static void main(String [] args) throws FileNotFoundException
  6.     {
  7.  
  8.         int ShiftValue;
  9.         Scanner inputFile;
  10.         PrintStream outputFile; 
  11.         Scanner keyboard = new Scanner(System.in);
  12.  
  13.         inputFile = openInputFile(keyboard);
  14.         outputFile = openOutputFile(keyboard);
  15.         ShiftValue = getShiftValue(keyboard);
  16.  
  17.         getShiftValue(keyboard);
  18.  
  19.         EncryptFile (inputFile, outputFile, ShiftValue);
  20.  
  21.         inputFile.close();
  22.         outputFile.close();
  23.  
  24.     }//end of main
  25.  
  26.     public static int getShiftValue(Scanner keyboard)
  27.     {
  28.         String directionShift = keyboard.next();
  29.         int positionShift = keyboard.nextInt();
  30.         int ShiftValue = 0;
  31.  
  32.         System.out.print("Enter the direction you would like to shift and the number of positions: ");
  33. //    directionShift = keyboard.next();
  34. //    positionShift = keyboard.nextInt();
  35.         while(ShiftValue == positionShift)
  36.         {
  37.             if(directionShift.equalsIgnoreCase("left"))
  38.             {
  39.                 ShiftValue = positionShift * 1;
  40.             }
  41.             else if(directionShift.equalsIgnoreCase("right"))
  42.             {
  43.                  ShiftValue = positionShift * (-1);
  44.             }
  45.         }//end while
  46.         return ShiftValue;
  47.     }
  48.  
  49.     public static Scanner openInputFile(Scanner keyboard) throws FileNotFoundException
  50.     {
  51.         Scanner inputFile;
  52.         String fileName;
  53.  
  54.         System.out.print("Enter name of input file: ");
  55.  
  56.         fileName = keyboard.nextLine();
  57.  
  58.         inputFile = new Scanner(new File(fileName));
  59.         return inputFile;
  60.  
  61.     }//end method InputFile
  62.  
  63.     public static void EncryptFile(Scanner inputFile, PrintStream outputFile, int ShiftValue)
  64.     {
  65.         String line;
  66.         ShiftValue = ShiftValue%26;
  67.  
  68.         while (inputFile.hasNextLine())
  69.         {
  70.             line = inputFile.nextLine();
  71.             for (int i = 0; i < line.length(); i++)
  72.             {
  73.                 char ch = line.charAt(i);
  74.                 ch = ch + ShiftValue;
  75.                 if (ch > 'Z' || ch > 'z')
  76.                 {
  77.                     ch = ch -26;
  78.                     System.out.print(ch);
  79.                     outputFile.print(ch);
  80.                 }
  81.                 else if (ch < 'A' || ch < 'a')
  82.                 {
  83.                     ch = ch + 26;
  84.                     System.out.print(ch);
  85.                     outputFile.print(ch);
  86.                 }
  87.  
  88.             }//end for loop
  89.             System.out.println();//finished processing a line
  90.             outputFile.println();
  91.  
  92.         }//end while
  93.  
  94.     }//end EncryptFile
  95.  
  96.     public static PrintStream openOutputFile(Scanner keyboard) throws FileNotFoundException
  97.     {
  98.         String fileName;
  99.         PrintStream outFile;
  100.         System.out.print("Enter name of output file: ");
  101.         fileName = keyboard.nextLine();
  102.         outFile = new PrintStream(fileName);
  103.  
  104.         return outFile;
  105.  
  106.     }//end of OutputFile
  107.  
  108. }//end of class 
  109.  
May 19 '10 #1
2 4106
p.s. I have no room for much modification, if you have any suggestions please let me know.. I would really appreciate it. I'm kind of stuck and I'm really just looking for advice, not a whole makeover of my code.
May 19 '10 #2
jkmyoung
2,057 Expert 2GB
I don't think this while condition is right.
while(ShiftValue == positionShift)

Moreover, if a user enters 0, you will be stuck in an infinite loop.
Guessing: you want to loop until the user enters something valid?
Try using this pattern instead:
Expand|Select|Wrap|Line Numbers
  1. ask for input.
  2. loop{
  3.   get input
  4.   if input is left,  set value and break
  5.   if input is right, set value and break
  6.   display error message, requesting proper input.
  7. }
May 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Spikinsson | last post by:
I'm looking for a good decrypt/encrypt function, all I want is a function in this form: char* encrypt(char* normal) { ... return encrypted; } and
1
by: Benoît | last post by:
Hi, I have generated two keys : "C:>openssl req -nodes -new -x509 -keyout ben.key -out ben.crt -days 3650" I try to encrypt/decrypt a string like "JOHN" with these asymetrics keys. With the...
8
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly...
0
by: Aaron | last post by:
Is the native Encrypt/Decrypt functionality with .NET PGP compatible?
4
by: Hrvoje Voda | last post by:
Does anyone knows a good example of how to encrypt/decrypt a string? Hrcko
3
by: Sushant Bhatia | last post by:
Hi All, I am trying to do encryption/decryption with RSA. For encryption, I use the public key. For decryption, I use the private key. The idea is to take a 80 byte array and encrypt it and then...
1
by: Microsoft | last post by:
Hi I'm not able to find a newsgroup for classic asp so I'm writing here hoping someone can help me So, I have to encrypt and also decrypt some user passwords I know md5 algoritm is a one way...
0
by: Kelvin | last post by:
Dear all, I don't know how to write a asp/vb/c# program to decrypt PHP encrypted code. Here is attched my code in PHP $key = "ab"; $random_number = kelvin;...
7
by: Jean Christophe Avard | last post by:
Hi! I am designing an application wich comes with image file. These images are copyrighted and they have to be accessible only from within the application. At first, I tought I was going to store...
4
by: Max Vit | last post by:
Here is my problem: I have an application built in Access that outputs sensitive data to a text file. I would like to encrypt this data *whilst* the file is being outputted. The encryption I was...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.