• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help figuring out what a couple methods mean  RSS feed

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically I got help with this assignment from a friend but I don't understand what the encrypt and decrypt methods are doing. I'm pretty sure the decrypt method is wrong because it just encrypts the method another way when I need it to decrypt the encrypted password so it shows the password that was entered. I don't want you to do it for me, just help me understand what the Encrypt method is doing so I possibly can fix the decrypt method.


 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
passwordArray[decrypt[i + 1]] = password.charAt(decrypt[i]);

if i change this line to

passwordArray[decrypt[i]] = password.charAt(decrypt[i]);

it will decrypt my password showing the password entered.

From the example my professor gave us though, I can't it to encrypt the same way (my letters are in different orders than his are)
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me it looks like the encrypt() and decrypt() methods are just shuffling the characters in the password. Or at least, it looks like that's what they are supposed to do. However they would work a lot better if the decrypt[] array was the reverse of the encrypt[] array. (I have to say that giving the methods and the arrays the same names is sort of confusing, it makes it hard to talk about the code.)

I also don't understand what the point is of not encrypting when a password is less than 10 characters long, or at least: I can see what the point is from the program's point of view, because the code is going to crash in that case. But if the password is longer than 10 characters, then the part beyond 10 is going to be left alone. It's also strange that decrypting DOES apply to passwords which are 10 characters long but encrypting doesn't.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's just a simple program for my intro to java class. The minimum is 10 meaning the password has to be at least ten characters long. Sorry can't reply cos I'm in another class. Hopefully I can get some help in a couple hours when I get home.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:The minimum is 10 meaning the password has to be at least ten characters long.


That seems contrary to the way that the isValidPassword() method works -- it returns false if the password is at least 10 characters long and true if it's less than 10 characters long.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did my isValidLength that way because for my driver I have:



So if they enter a password that is less than 10 characters it will prompt them to re-enter the password. I guess I could have done while (!Encryption.isValidLength(pass)).
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it'll help, here are my assignment instructions.


http://technology.heartland.edu/faculty/todds/csci130/assignments/A6_Encryption_Array/csci130assign6.htm
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:I did my isValidLength that way because for my driver I have:
So if they enter a password that is less than 10 characters it will prompt them to re-enter the password. I guess I could have done while (!Encryption.isValidLength(pass)).




Now if I look at that code and try to read it as English, it does the complete OPPOSITE to what I expect.


Yes, the code does what you WANT it to, but it doesn't READ that way.
How code reads is as important (or more important in some views) than if it works or not.

I would expect to see


In otherwords.
Don't make your method illogical to suit your driving program.
Change your driving program to make sense, and your library will as well.

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you mean:



and then my driver would look like this:




I'm still having trouble with the characters not being put in the right order.

private static final int [] encrypt = {2, 9, 3, 4, 6, 8, 1, 0}; char 0 should go into 2, 2 into 9, 9 into 3, etc. I'm not sure how to make it work this way.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is it doing right now?

If you type in "colossal squid" from the example, what does it give you?
Does it even look close to ""o csosslalquid" ?

I would also suggest you put a through "System.out.println" calls in your code to print out the values in progress so you can see what is happening in your program.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it prints out o lsosslalquid


i dont know how its getting the extra l

excuse my ignorance, but put system.out.printn() where?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest in your inner loop of your encrypt method so that you can see the value of your passwordArray after every swap.

So that you can see a progression something like:

colossal squid
colossal lquid
colsssal lquid
colsosal lquid
colsossl lquid
colsosslalquid
c lsosslalquid
o lsosslalquid
o csosslalquid

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heh, no matter where I put it it just makes a big space before it encrypts it.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I at least on the right track with my code? I re-wrote the whole Encryption class as something different but I cant get it to work either. I'm at a loss. I've been working on this for over a week.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it went without saying, but you do need to actually GIVE it something to print out... like the current state of the variables.
For example, I added a line like this:



That will print out
- what operation just took place
- what the encrypted password looks like right now.

2-->9 : colossal lquid
9-->3 : colsssal lquid
3-->4 : colsosal lquid
...


 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I've been having a very rough time in the class since midterm.

What printed out was:

2 --> 9 : colossal lquid
9 -->3 : colsssal lquid
3 --> 4 : colsosal lquid
4 --> 6 : colsossl lquid
6 --> 8 : colsosslalquid
8 --> 1 : c lsosslalquid
1 --> 0 : o lsosslalquid

I still dont get where the extra l is coming from.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What "extra l" is that?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from the very first permutation it adds an l. There are only two "l's" in colossal squid.

my encrypt method would be perfect if the first l was a c. the l gets added and after the last permutation the c gets dropped.

I still can't figure out how to get it to decrypt properly either. I can make it print out the original password but not through the decryption process.

It's really discouraging because programming really interests me. I just can't seem to get the hang of it.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Johnny Vazquez wrote:from the very first permutation it adds an l. There are only two "l's" in colossal squid.


It doesn't "add" an l. It takes the "l" from position 2 (the third position) and puts it in position 9 (the tenth position), replacing the "s" which was there before. But the "s" gets added back in the next step... notice how the number of s's goes back to three again?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is my code wrong then?

I need the end result to be: o csosslalquid

and why is it still in position 2 if its supposed to be moved?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and why is it still in position 2 if its supposed to be moved?

Well obviously you need to read the assignment instructions again, and take a close look at what the program is doing.
Do the steps printed out via the println logging make sense? Are they correct?
Did any of those listed steps involve position 2?
Is it supposed to have moved?
What is supposed to have moved it?


Each character in the password is moved to a new index according to the permutation. Since 2 is immediately followed by 9 in the permutation, the character at index 2 of the password must be reassigned to index 9. Similarly, the character in index 9 must be reassigned to index 3 and so on. The last reassignment completes the cycle by placing the character originally at index 0 into index 2.


Which step/replacement is missing from your encrypt method?
If you did it yourself manually how would you do it?
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
and why is it still in position 2 if its supposed to be moved?

Well obviously you need to read the assignment instructions again, and take a close look at what the program is doing.
Do the steps printed out via the println logging make sense? Are they correct?
Did any of those listed steps involve position 2?
Is it supposed to have moved?
What is supposed to have moved it?


Each character in the password is moved to a new index according to the permutation. Since 2 is immediately followed by 9 in the permutation, the character at index 2 of the password must be reassigned to index 9. Similarly, the character in index 9 must be reassigned to index 3 and so on. The last reassignment completes the cycle by placing the character originally at index 0 into index 2.


Which step/replacement is missing from your encrypt method?
If you did it yourself manually how would you do it?


I'm not sure. This is why I'm lost. My friend couldnt explain anything to me (making me feel like he copied off someone else). I want to learn but I'm failing at it.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, my advice at this point is to stop.

Step away from the computer.
Go get yourself a pen and paper.

Work through the steps of encrypting a word with the pen and paper until you can come up with the answer you need.

Until you yourself understand the how the encryption is supposed to work, you will never be able to teach the computer.
It may help to use a word OTHER than the "colossal squid" example.

For instance "0123456789" (results in 1809354762)
Or maybe "abcdefghij"

Use a word which doesn't have any double letters. That might make it easier.

 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:Ok, my advice at this point is to stop.

Step away from the computer.
Go get yourself a pen and paper.

Work through the steps of encrypting a word with the pen and paper until you can come up with the answer you need.

Until you yourself understand the how the encryption is supposed to work, you will never be able to teach the computer.
It may help to use a word OTHER than the "colossal squid" example.

For instance "0123456789" (results in 1809354762)
Or maybe "abcdefghij"

Use a word which doesn't have any double letters. That might make it easier.



I should've came here earlier for advice. I've been trying to fix this for a week or so on my own. Unfortunately its due in 27 minutes. So i'll have to take the points deducted.
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since its turned in, can you please show me the correct code?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code isn't far off from the requirements. But critically you missed this:

The last reassignment completes the cycle by placing the character originally at index 0 into index 2.


But I'd still agree with Stefan's suggestion to do the process with paper and pencil. Then you'll get a better appreciation of the permutation business (and what goes wrong if the permutation isn't complete).
 
Jeremy Wages
Ranch Hand
Posts: 141
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just don't get how to make it do the last permutation of 0 --》2
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your loop covers replacing all characters EXCEPT the first one in your encryption list.
So you need one more execution of
to cover the values you haven't yet swapped.


 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it was me I'd just add one more entry in the permutation array to cover the last step.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!