encryption

how to encrypt this method

Page 1 of 1

6 Replies - 944 Views - Last Post: 26 September 2009 - 10:54 AM Rate Topic: -----

#1 ALizAsOr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-September 09

encryption

Post icon  Posted 26 September 2009 - 07:41 AM


I want to encrypt this method
user enter the key word that they want
example key word=security

so the ciphertext is

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
s e c u r i t y a b d f g h j k l m n o p q v w x z

example want to encrypt "love"

so love will be "qtwr"

I do the code but still have an error


int k = 0;
	  String out = " ";
	  
			String keys = JOptionPane.showInputDialog("Enter the key word that you want");
			int key1 = Integer.parseInt(keys);
			int loop2 = keys.length();
			
			char [] arrayKey = new char[loop2];

			for (int i =0; i<arrayKey.length; i++)
			{
					arrayKey[i] = keys.charAt(substring(1, i));
			}
			
			// declare 26 new array
			int [] arrayNew = new int [26];
			
			for(int i=0; i<arrayNew.length; i++)
			{ 
					if (arrayKey[i] != "")
					{
						arrayNew[i] = arrayKey[i];
					}
					else
					{
						arrayNew[i] = characters[k];
						k =k+1;
					}
			}

			//print answer
			for (int i=0; i<arrayNew.length; i++)
			{
				  out = out + arrayNew[i];
			}
			JOptionPane.showMessageDialog(null,out);



Is This A Good Question/Topic? 0
  • +

Replies To: encryption

#2 S.R.K.   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-September 09

Re: encryption

Posted 26 September 2009 - 08:48 AM

What error are you getting?

I believe you are trying to use
Integer.parseInt(keys);
incorrectly. That method should take a string of integers and convert them to an int value. So it will error when you enter "security" because it is looking for integers.

You probably want to find a different method or handling it differently somehow.

This post has been edited by S.R.K.: 26 September 2009 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 ALizAsOr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-September 09

Re: encryption

Posted 26 September 2009 - 09:30 AM

View PostS.R.K., on 26 Sep, 2009 - 07:48 AM, said:

What error are you getting?

I believe you are trying to use
Integer.parseInt(keys);
incorrectly. That method should take a string of integers and convert them to an int value. So it will error when you enter "security" because it is looking for integers.

You probably want to find a different method or handling it differently somehow.



yes I have an error at here

arrayKey[i] = keys.substring(1, i);
and here if (arrayKey[i] != "")


Was This Post Helpful? 0
  • +
  • -

#4 S.R.K.   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-September 09

Re: encryption

Posted 26 September 2009 - 09:37 AM

So you aren't having a problem with the parseInt() method, like I believed?

Can you explain the errors you are getting and when?
Was This Post Helpful? 0
  • +
  • -

#5 ALizAsOr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 25-September 09

Re: encryption

Posted 26 September 2009 - 10:18 AM

View PostS.R.K., on 26 Sep, 2009 - 08:37 AM, said:

So you aren't having a problem with the parseInt() method, like I believed?

Can you explain the errors you are getting and when?



arrayKey[i] = keys.substring(1, i);

this error found java.lang.string
required char

if (arrayKey[i] != "")

this error incomparable types: char and java.lang.String

Was This Post Helpful? 0
  • +
  • -

#6 S.R.K.   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-September 09

Re: encryption

Posted 26 September 2009 - 10:22 AM

It looks like your types aren't matching in your comparisons. Try to follow what type you have for each variable as you go through the program.

Did you look into what parseInt does, like I mentioned? I'm surprised you aren't getting the error there, on your input, that I get. I could be reading it wrong, but it looks like you requires your security input to be an integer and you wanted to enter a string of characters and the parse them individual characters to be inserted into your encryption array, correct? The fact that you probably want an array of chars and that parses your encryption code to ints could be the problem. You may want to look into that.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: encryption

Posted 26 September 2009 - 10:54 AM

By the way

			char [] arrayKey = new char[loop2];

			for (int i =0; i<arrayKey.length; i++)
			{
					arrayKey[i] = keys.charAt(substring(1, i));
			}



can be written

char[] arrayKey = keys.toCharArray();



I would process that thing another way
First I would make an array of the whole alphabet

String alpha = "abcdefghijklmnopqrstuvwxyz";
char[] alphabet = alpha.toCharArray();

then blank into it the character in the key

// pass through the key
for(int i = 0; i < arrayKey.length; i++) {
	int idx = (int) arrayKey[i];				   // get digit into int
	idx -= (int) 'a';								   // compute offset in alphabet
	alpahbet[idx] = ' ';							 // put a ' ' when used
}



Now you can make a new array of all your key plus the remind letters
char[] code = new char[26];
int k = 0;
for(int i = 0; i < arrayKey.length; i++)
   code[k++] = arrayKey[i];
for(int i = 0; i < alphabet.length; i++) {
	if(alphabet[i] != ' ')
	  code[k++] = alphabet[i];
}



This code does not check if the same letter is entered twice in the key thow

Happy coding
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1