Get Mystery Box with random crypto!

May the peace, blessings, and mercy of God be upon you, The XO | Learn Programming تعلم البرمجة

May the peace, blessings, and mercy of God be upon you,
The XOR encryption and decryption process is simple and effective in some cases. It relies on performing an XOR operation (or inverted combination of bits) between the original message and the secret key to generate the encrypted message, and vice versa for decryption. The process can be explained in detail as follows:

1- When encrypting, the main steps are to perform an "XOR" operation between each letter in the original message and a letter of the secret key. For example, if the original message is 'Hello' and the secret key is '1234', the first step of the process will XOR between 'H' and '1' (i.e., between the letter and the key letter) and so on until all characters are XORed in the text.

2- After completing the XOR operation, the encrypted message is generated which is the same length as the original message and contains the result returned from the XOR operation.

3- When decrypting, the same secret key that was used in the encryption process is used to XOR again between the encrypted message and the secret key.

4- The calculated result of the XOR is used to regenerate the original message.

Although the XOR process is simple and effective in some cases, it is not secure enough for sophisticated computer cryptography, which results from the use of more secure and accurate encryption algorithms to protect sensitive data.

Encryption and decryption using XOR depends on performing an XOR operation between the original (plain) text and a secret key to encrypt it and if the process is reversed by performing an XOR operation between the ciphertext and the secret key it will be decrypted.

To apply this idea to the text "hello world" we will use the secret key "123".

To encrypt the message, we will apply the XOR operation to the key and the body as follows:

h^1 = i
e^2 = c
l^3 = o
l^1 = k
o ^ 2 = m

w^1 = v
o ^ 2 = m
r^3 = u
l^1 = k
d^2 = f

So the encrypted message will be: "icokm vmukf"

To decrypt the message, we'll perform an XOR operation between the secret key and the encrypted message:

i^1 = h
c^2 = e
o^3 = l
k^1 = l
m^2 = o

v^1 = w
m^2 = o
u^3 = r
k^1 = l
f^2 = d

So we will get the original (pronounced): "hello world".

Here's the Python code for XOR encoding and decoding:

# Encrypt using XOR
def xor_encrypt(msg, key):
encrypted = ""
for i in range(len(msg)):
encrypted += chr(ord(msg[i]) ^ ord(key[i % len(key)]))
return encrypted

# Decrypt using XOR
def xor_decrypt(encrypted, key):
decrypted=""
for i in range(len(encrypted)):
decrypted += chr(ord(encrypted[i]) ^ ord(key[i % len(key)]))
return decrypted

# Example of encoding and decoding
msg = "hello world"
key = "123"

encrypted = xor_encrypt(msg, key)
print("encrypted text: ", encrypted)

decrypted = xor_decrypt(encrypted, key)
print("original text: ", decrypted)

# Output:
# Ciphertext: icokm vmukf
# Original text: hello world

@SuDevelopers | Subscribe