Java encryption program using arrays

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 2182 Views - Last Post: 07 March 2015 - 09:41 PM Rate Topic: -----

#1 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Java encryption program using arrays

Posted 07 March 2015 - 01:40 PM

Hey all, I'm back again for some help.

I am doing an assignment that requires the user to enter a message and type the word QUIT when finished. The message then needs to be sent to a function that encrypts the message by running through the string, and comparing the char at the specific index in the array and matching to the same index of a key array.

So basically ABC would be turned into BCD.

I am taking a Java class and a C++ class and C++ is very easy to me but for some reason Java I am just not getting at all. I know this code is pretty bad, but ANY help at all in figuring this out would be appreciated tremendously.

Can anyone help get me started or show some code examples?

Thanks again,
blue

import java.io.*;
import java.util.Scanner;


public class Encryption
{
   public static String toEncrypt(String x){  //void does not return, use public satitc string
        char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        char key[]={'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A'};
        String encrypted ="";  //holds the encrypted message
        char letterToEncrypt;
        String regText = "";
        
        //loop start to encrypt the message       
       
        for(int i=0; i<alphabet.length; i++){ 
             alphabet[i]=x.charAt(i);
             //letterToEncrypt = alphabet[i];
            encrypted = encrypted + key[i];
            System.out.println(alphabet[i] + " ");
            //System.out.println(key[i]);
            //System.out.println(encrypted);             

 
        } 
        
        return encrypted;  //returns the encrypted message to main method

}
   
   public static void main(String[] args){     
        
        String cipherText;
        String encrypted = ""; //holds the encrypted text
        String decode;
        String encode;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter your message to be encrypted. Type QUIT when finished.");
        String regText = sc.nextLine();
        toEncrypt(regText);
        System.out.println(toEncrypt(encrypted));
   }
}



Please forgive the commented statements, I'm just trying different things out.

Is This A Good Question/Topic? 0
  • +

Replies To: Java encryption program using arrays

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 01:45 PM

It's a simple transposition cipher. Characters have integer values. So 'A' + 1 == 'B'. This is nice, because you don't have to use a second array. You then have to account for a character being 'Z', as 'Z' + 1 != 'A'.
Was This Post Helpful? 1
  • +
  • -

#3 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 01:48 PM

View Postmacosxnerd101, on 07 March 2015 - 01:45 PM, said:

It's a simple transposition cipher. Characters have integer values. So 'A' + 1 == 'B'. This is nice, because you don't have to use a second array. You then have to account for a character being 'Z', as 'Z' + 1 != 'A'.


Thank you sir for your quick response however I have to use 2 arrays. Its an assignment and the instructions must be followed. I've seen a hundred different ways to encrypt and I know there are much simpler ways to do it, but he actually looks at our code and I need help figuring out how to do it this way.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 01:55 PM

Hint- if you take a letter and subtract 'A' from it, you will have the appropriate index in the key array.
Was This Post Helpful? 1
  • +
  • -

#5 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 02:00 PM

I'm not understanding what you mean by that?

We are supposed to read through the string in the encryption method, find the index in the alphabet array and compare it to the same index in the key array.

The code I have
encrypted = encrypted + key[i]
adds the value in key to my string with the encrypted message but I do not know how to get the program to actually compare the values in the array.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 02:08 PM

Quote

So basically ABC would be turned into BCD.


What is 'A' - 'A'? What index is 'B' in the second array?

Quote

We are supposed to read through the string in the encryption method, find the index in the alphabet array and compare it to the same index in the key array.

Except the alphabet array is in alphabetical order. So you don't actually have to search for the index in the alphabet array. Simple subtraction will account for this.

Quote

encrypted = encrypted + key[i]

So this isn't just transposition by 1?
Was This Post Helpful? 1
  • +
  • -

#7 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 02:31 PM

Okay would you mind giving me a simple example in code about the substraction?

What I am gathering or trying to do I guess was use the charAt(i) in the alphabet array and then compare that to key(i), I wouldn't think I'd need subtraction.

Again I'm really sorry but this is all really new to me.

Thanks so much for your time.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 02:37 PM

Quote

Okay would you mind giving me a simple example in code about the substraction?

I already did. The syntax I gave you is legal Java syntax. Please take the time to give it a try and play around with the idea.

Quote

What I am gathering or trying to do I guess was use the charAt(i) in the alphabet array

Arrays don't have a charAt() method.

Quote

then compare that to key(i), I wouldn't think I'd need subtraction.

You examine the key, then you look at the encrypted array using the algorithm I described in my previous posts.

At this point, I think it's up to you to work through things. I've given you as much help as I can without writing the thing for you.
Was This Post Helpful? 1
  • +
  • -

#9 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:09 PM

Okay I have been playing around a bit. I am getting 2 errors now when I run my code:

Encryption.java:12: error: cannot find symbol
for(int i = 0; i < charArray.length(); i ++){
^
symbol: method length()
location: variable charArray of type char[]
Encryption.java:13: error: incompatible types: possible lossy conversion from int to char
charArray[i] = charArray[i] + 1;
^

I am not sure what is going on here, any ideas?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:28 PM

In arrays length is a field, not a method. Remove the parentheses and you should be fine.

Quote

charArray[i] = charArray[i] + 1;

Try casting it back to a char.
Was This Post Helpful? 1
  • +
  • -

#11 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:46 PM

That fixed the length issue.

So to cast it back to a char would I be using
strcat(charArray);
?

I am trying different things with the code but nothing still not compiling.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:49 PM

That's not a Java method.

This is a cast:
char c = (char)('A' + 1); //c == 'B'


Was This Post Helpful? 1
  • +
  • -

#13 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:51 PM

So are you saying I would replace
charArray[i] = charArray[i] + 1;
and use the code you just provided? I'm not sure where I would do that at and what to replace if any. Man thank you so much for your willingness to help me on this, really means a lot!
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12189
  • View blog
  • Posts: 45,251
  • Joined: 27-December 08

Re: Java encryption program using arrays

Posted 07 March 2015 - 05:54 PM

Quote

So are you saying I would replace

No. Your code isn't even close to what I was describing.

By the way- your code was way different than what you were describing as the algorithm. You need to make sure you understand what is required here. Given a plaintext String (input) and a key, what is the algorithm to produce the ciphertext? I think either you need to check your understanding or communicate it to us better, as there is a gap somewhere.
Was This Post Helpful? 1
  • +
  • -

#15 still2blue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 02-February 15

Re: Java encryption program using arrays

Posted 07 March 2015 - 06:02 PM

This is all I have changed:

import java.io.*;
import java.util.Scanner;


public class Encryption
{
   public static String toEncrypt(String x){  //void does not return, use public satitc string
        //char alphabet[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        //char key[]={'B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A'};
        String message ="";  //holds the encrypted message
        char[] charArray = message.toCharArray();        
        for(int i = 0; i < charArray.length; i ++){
         charArray[i] = charArray[i] + 1;
         }
        

                
        
        return message;  //returns the encrypted message to main method

}
   
   public static void main(String[] args){     
        
        String cipherText;
        String encrypted = ""; //holds the encrypted text
        String decode;
        String encode;
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter your message to be encrypted. Type QUIT when finished.");
        String message = sc.nextLine();
        toEncrypt(message);
        //System.out.println(toEncrypt(encrypted));
        
        
        //System.out.println(encrypt);
   }
}



I am only getting the one error now about loss of conversion int and char.

I have been reading threads and articles good part of the day. Java really has me dumbfounded and I know I am getting frustrated. Is the updated code getting closer to what I need? I am thinking that the loop goes through the string with the message to be encrypted, then assigns it the new letter (i.e B would be C). Then I think I would set a new string, say encrypted, to encrypted = encrypted + charArray[i] then run back through the loop until no more characters are present?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2