2

If anyone here knows the meaning of these algorithm, please help me to understand, because I don't want to simply copy without knowing how it works.

Here's the code:

1:

    public static String encryptKey(String key){
    int ch = 0;

    StringBuilder encryptedKey = new StringBuilder();

    for(int i = 0; i < key.length(); i++ ){

        ch = key.charAt(i);

        ch = ~ch;

        encryptedKey.append(ch);
    }

    return encryptedKey.toString();
    }

2:

    public String encrypt(String message, String key){

    StringBuilder encryptedMessage = new StringBuilder();
    char ch;
    int j = 0;

    for(int i = 0; i < message.length(); i++, j++ ){

        if(j >= key.length()){
            j = 0;
        }

        ch = message.charAt(i);

        ch = (char) (key.charAt(j) ^ ch);

        encryptedMessage.append(ch);

    }

    return encryptedMessage.toString();
    }

Thank's, once again

7
  • 4
    Look into the ~ and ^ operators, trace through the code a few times, and it should be pretty easy to figure out. Might be kind of hard to search for symbols, so ~ flips all of the bits in a variable and ^ is exclusive or. docs.oracle.com/javase/tutorial/java/nutsandbolts/… Commented May 15, 2012 at 2:10
  • 3
    Beware home-grown encryption methods. The person who is prohibiting the use of encryption libraries is not well-versed in encryption. Commented May 15, 2012 at 2:10
  • 3
    @tom_yes_tom: Or it is class assignment and not an actual production cryptosystem. As long as people are warned not to do it when it matters, I see no problems with it. Maybe it's even the point, in a crypto class: let's first implement a cryptosystem, then show how easy it is to crack and why the homemade cryptosystems are never ever used. Commented May 15, 2012 at 2:11
  • Is this homework? If so, it is advantageous to tag it as such. Commented May 15, 2012 at 2:16
  • @AndrewThompson: Eh... opinions are apparently split on this, lately... meta.stackexchange.com/questions/123758/… Commented May 15, 2012 at 2:18

1 Answer 1

9

These are two simple and standard (and completely insecure, but this is a learning experience) ways to alter data in a reversible way.

The first one uses ch = ~ch; to toggle all of the bits in the character. So, for example, the letter a, which is ASCII 97 or binary 01100001, would become 10011110. This is reversible because you can apply exactly the same operation to the encrypted data to retrieve the original message.

The second is slightly more secure and uses the ^ (exclulsive-or) operator to alter the bits in each character based on a key: each bit in the message character is compared to the corresponding bit in the key character. If the bits are the same (either both 0 or both 1), the encrypted bit is 0. If the bits are different, the encrypted bit is 1. This is reversible because you can apply the same ^ operation again, using the same key, to retrieve the original message.

As a side note, all modern ciphers (such as DES3 and AES) are based on the exclusive-or operation, combined with an algorithm that "mixes" the bits in a specified way, so it's much harder to guess the key. The ciphertext can be decrypted by running the algorithm in reverse, or by taking computational short cuts that are mathematically equivalent.

Warning, and rant: This is clearly homework, and you've already admitted that both you and your friend are cheating by copying someone else's answer. As bad as that may be, it's even worse if you can't explain it, especially because I promise you that every other student who has ever taken the course and copied the answer will have produced these same 2 solutions. What's stupidly worse is that encryption is dead simple: it all comes down to flipping bits.

So here's the essence of cryptography:

  • Take each character in the message.
  • Modify the character somehow, according to some rule you invent. The important thing is that you must be able to reverse this rule, so for example, always setting the 3rd bit to 1 and the 6th bit to 0 won't work, because you'll no longer know what they originally were. But swapping the 3rd and 6th bits would be fine; you'd just swap them again to decrypt.
  • Append the modified character to your output as the encrypted text.
  • Repeat until you run out of characters.

To decrypt, follow the same algorithm, but reverse whatever rule you used to modify each character.

Uibu't bmm uifsf jt up ju. Hppe mvdl!

Sign up to request clarification or add additional context in comments.

7 Comments

well... because i'm network student... crytography does not really suited for my course.. and I don't know why my campus gave my course such subject.. well.. it's good.. but too much stress, for me. so, i seek help from outside :)) but, thx for the advice :D
@chiaki_kei Networking and encryption are often very closely linked unless you mean networking at only a physical level. And either way, plagiarizing homework is never a good idea. If I were to get caught copy/pasting code at my university, especially code I could not explain, I would at the minimum receive a 0 in the class.
well im not plagiarizing the whole set of program, the usage method of the algorithms is my original arrangement.
"well im not plagiarizing the whole set of program,.." I do not believe that academic conduct guidelines excuse a 'little bit of plagiarism'. Correct me If I'm wrong.
@AndrewThompson you're right, very right, but i'm not coming here for moral n academic things lecture, OK?.. i came here to UNDERSTAND code better.. PS: i don't really like academic way of life, because it does not care about the student life, and don't give a sh*t about student's health, my friend involved in a traffic accident because he was too tired doing assignments which submitted in the same day. The lecturers said other assignment other than their assignment is not their concern
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.