PDA

View Full Version : Please help with decryption/encryption java program


phloxian
10-13-2005, 10:08 PM
First off, this is a homework assignment. I'm not asking for code though, as I've got about 95% of it written already. I'm just wondering if there's a better way I can do this, because what I have doesn't work 100% of the time.

We are to ask the user whether they wish to encrypt/decrypt, ask them for a key, and a string (no spaces). We then return the encoded string.

Our program only changes letters, not numbers or symbols, and must stay true to the case. I tried doing modulo arithmetic, since that's what our professor is having us do right now. Here is part of what I'm using in my methods:

char chChangeL = (char)((str.charAt(i) + key - 'A') %26 + 'A'); //there's an 'a' there instead of 'A' whenever it's dealing with lower case

This seems to work great for a positive key, in which we shift it Right. But when I try it out with a negative number that takes it out of the alphabet (i.e. 'e' with a -5 for a key), it starts giving me symbols. Should I be multiplying by a -1 somewhere to make it positive, or changing a sign in my formula?

Everything else works great, as far as input and output. It's just something with my formula that's not right. I won't post my whole program since the deadline to turn it in hasn't passed yet.

It's really quite annoying that I'm so close, yet so far. Any help would be appreciated.

phloxian
10-14-2005, 04:55 PM
Since no one was able to help, I guess it was because of the lack of information (?). Sorry if I wasn't clear enough, it's just that the assignment isn't due until today, and I didn't want to give too much away. But it's okay, I solved the problem with a bit of tweaking. If anybody wants to see the code, I'll post it after this weekend. Thanks to whoever was at least thinking about helping out.