0% found this document useful (0 votes)
10 views3 pages

Lab6 F

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Lab6 F

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT-06

Date :
AIM : Write a program to Implement Symmetric key cryptography.

APPARATUS : Scilab

THEORY :
Symmetric Key Cryptography
1. Definition: Symmetric key cryptography is a type of encryption where the same key is used for both
encryption and decryption of data. It is one of the two main types of cryptography, the other being
asymmetric cryptography. In symmetric cryptography, both the sender and the receiver must have
access to the same secret key, which must be kept private.

2. How It Works:
• Encryption: The plaintext message is transformed into ciphertext using the secret key and a
chosen encryption algorithm.

• Decryption: The ciphertext is transformed back into plaintext using the same secret key and
the corresponding decryption algorithm.

XOR Cipher Operation

1.Encryption:

• Convert plaintext to its binary or ASCII representation.


• XOR each bit or byte of the plaintext with the corresponding bit or byte of the key. If the key
is shorter than the plaintext, it is repeated as necessary.
• The result is the ciphertext.

2.Decryption:

• XOR the ciphertext with the same key used for encryption. Due to the self-inverse property of
XOR, this operation restores the original plaintext.

3. Algorithm Steps
1.Convert Plaintext and Key:

• Convert the plaintext and key into their binary or ASCII values.

2.Apply XOR Operation:

• XOR each byte (or bit) of the plaintext with the corresponding byte (or bit) of the key. If the
key is shorter, wrap around and repeat the key as necessary.

3.Output Ciphertext:
• Convert the result back to characters or binary as needed.

4.Decrypt Ciphertext:

• Apply the same XOR operation with the same key to the ciphertext to retrieve the original
plaintext.

CODE :
// Function for XOR encryption/decryption function

result=xor_cipher(data, key)

data = ascii(data);

key = ascii(key);

// Ensure key length is not longer than data

key_length = length(key);

data_length = length(data);

if key_length < 1 then

error("Key length must be greater than

0."); end

result = zeros(data_length, 1);

for i = 1:data_length

result(i) = bitxor(data(i), key(modulo(i-1, key_length) + 1));

end

result = char(result');

encrypted_message = xor_cipher(message, key); //Message to be encrypted

decrypted_message = xor_cipher(encrypted_message, key);

// Display results in a structured format


disp("Original Message: " + message)

disp("Key: " + key)

disp("--------------------------------------------------")

disp("Encrypted Message: " + encrypted_message)

disp("Encrypted ASCII: " +string(ascii(encrypted_message)))

disp("--------------------------------------------------")
disp("Decrypted Message: " + decrypted_message)

disp("Decrypted ASCII: " +string(ascii(decrypted_message)))

OUTPUT :

CONCLUSIONS :
It offers efficient and fast encryption but requires secure key exchange between the parties. Its
simplicity makes it suitable for many applications, though it lacks the scalability of public-key
systems. For secure communication, careful management and protection of the shared key are
essential.

You might also like