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
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 lettera, which is ASCII 97 or binary01100001, would become10011110. 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 both0or both1), the encrypted bit is0. If the bits are different, the encrypted bit is1. 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:
1and the 6th bit to0won’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.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!