aspose file tools*
The moose likes Java in General and the fly likes the out put is wrong in my encryption code  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "the out put is wrong in my encryption code  " Watch "the out put is wrong in my encryption code  " New topic
Author

the out put is wrong in my encryption code

hadeel marghlany
Greenhorn

Joined: Feb 21, 2013
Posts: 12

//this is the out put
pleas ente your plain text
aaa
pleas ente your key
3
the chiper text is:def


//please help me
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

How is that different from what you expect? It looks to be exactly what the code says to do.

p.s. When posting code, please follow some common indentation patterns. Your code is a mess, very hard to read, because it is not clear where one block ends and another starts. For a guide that will help make your code readable, see this style guide


Steve
hadeel marghlany
Greenhorn

Joined: Feb 21, 2013
Posts: 12
am sorry its my first time posting a code and my professers never told me this.
am telling the program

for(int i=0; i< message.length() ;i++){
for example aaa is my password
and here i=0 so 0<3

current=str.charAt(i);
so the current =0

for(pos=0;pos<25;pos++){
and posstion =0

if(current== str.charAt(pos))
and here if current witsh is 0 = possition witsh is 0 he will enter the if otherwise it will restart the loop

{chipher+=str.charAt((key+pos)%26);
lets say that i chose the key to be 3 so 3+0=3 witsh is d
and it should repeat this 3 times so ddd
so what do you mean by its doning what am asking it to do please explain more
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

hadeel marghlany wrote:am sorry its my first time posting a code and my professers never told me this.
am telling the program

for(int i=0; i< message.length() ;i++){
for example aaa is my password
and here i=0 so 0<3

current=str.charAt(i);
so the current =0

What the for loop followed by the assignment means is 'for each character in the message, get the character at the same position in the str variable.

So current does not (or at any point) equal 0. In the first iteration, current is the character at position 0 in str, which is 'a'. Then the second time through the loop, it becomes 'b' and the final time it becomes 'c' (because you increment i).

for(pos=0;pos<25;pos++){
and posstion =0

if(current== str.charAt(pos))
and here if current witsh is 0 = possition witsh is 0 he will enter the if otherwise it will restart the loop

The for loop followed by the if statement says 'navigate through the characters in the str string. We only want to work when we get the character that is the same as current. Since current is 'a' we get to 'a' in the str string, (which is position 0) and then continue. When the outer loop progresses to its second iteration, current becomes 'b' so this inner for loop skips through the str string until it finds 'b' and pos become 1. Then the third iteration of the outer loop makes current = 'c', you skip through str until you find 'c' and pos becomes 2.

{chipher+=str.charAt((key+pos)%26);
lets say that i chose the key to be 3 so 3+0=3 witsh is d

This is actually the the key + the offset in the str where current is found (as found in the pos variable). The first time through the loop, current is 'a', so position is '0' and the chipher gets the character at 3 + 0, which is 'd'. The second time through the loop current is 'b', position is 1, so the chipher gets 3+1 which is 'e'. Finally, the last time through the outer loop current is 'c', position is '2', and chipher gets 3+2 which is 'f'.

and it should repeat this 3 times so ddd
so what do you mean by its doning what am asking it to do please explain more

So it does what it says. But not what you want. For example, when I first read your code I expected "abc" to produce "def" which it does. But this is a coincidence. You are actually using the wrong String to get the current value from - you are getting current from str, not the user input. So if the user input "rm5" you would still get "def" as the result.
hadeel marghlany
Greenhorn

Joined: Feb 21, 2013
Posts: 12
i did it thankyou so mush for your help ^_^
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the out put is wrong in my encryption code