0% found this document useful (0 votes)
54 views6 pages

Experiment No: 5: Name: Rhugved Satardekar Roll No: 17141208

The document contains details about implementing a stream cipher and block cipher in Python. It provides theory on stream ciphers and block ciphers, describing how a stream cipher works by combining a plaintext digit with a keystream digit while a block cipher encrypts blocks of fixed size. It then shows code samples to encrypt and decrypt text using a stream cipher implemented with the pyaes library and a block cipher using the pyDes library.

Uploaded by

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

Experiment No: 5: Name: Rhugved Satardekar Roll No: 17141208

The document contains details about implementing a stream cipher and block cipher in Python. It provides theory on stream ciphers and block ciphers, describing how a stream cipher works by combining a plaintext digit with a keystream digit while a block cipher encrypts blocks of fixed size. It then shows code samples to encrypt and decrypt text using a stream cipher implemented with the pyaes library and a block cipher using the pyDes library.

Uploaded by

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

Name : Rhugved Satardekar

Roll No: 17141208

Experiment No: 5

Aim: Implement Stream Cipher & Block Cipher using any programming language.
Theory:
Stream Cipher:
A stream cipher is a symmetric key cipher where plaintext digits are combined with
a pseudorandom cipher digit stream (keystream). In a stream cipher, each plaintext digit is
encrypted one at a time with the corresponding digit of the keystream, to give a digit of
the ciphertext stream. Since encryption of each digit is dependent on the current state of the
cipher, it is also known as state cipher. In practice, a digit is typically a bit and the combining
operation is an exclusive-or (XOR).
The pseudorandom keystream is typically generated serially from a random seed value using
digital shift registers. The seed value serves as the cryptographic key for decrypting the
ciphertext stream. Stream ciphers represent a different approach to symmetric encryption
from block ciphers. Block ciphers operate on large blocks of digits with a fixed, unvarying
transformation. This distinction is not always clear-cut: in some modes of operation, a block
cipher primitive is used in such a way that it acts effectively as a stream cipher. Stream
ciphers typically execute at a higher speed than block ciphers and have lower hardware
complexity. However, stream ciphers can be susceptible to serious security problems if used
incorrectly (see stream cipher attacks); in particular, the same starting state (seed) must never
be used twice.

Types:

A stream cipher generates successive elements of the keystream based on an internal state.
This state is updated in essentially two ways: if the state changes independently of the
plaintext or ciphertext messages, the cipher is classified as a synchronous stream cipher. By
contrast, self-synchronising stream ciphers update their state based on previous ciphertext
digits.

Synchronous stream ciphers:

In a synchronous stream cipher a stream of pseudo-random digits is generated independently


of the plaintext and ciphertext messages, and then combined with the plaintext (to encrypt) or
the ciphertext (to decrypt). In the most common form, binary digits are used (bits), and the
keystream is combined with the plaintext using the exclusive or operation (XOR). This is
termed a binary additive stream cipher.

In a synchronous stream cipher, the sender and receiver must be exactly in step for decryption
to be successful. If digits are added or removed from the message during transmission,
synchronisation is lost. To restore synchronisation, various offsets can be tried systematically
to obtain the correct decryption. Another approach is to tag the ciphertext with markers at
regular points in the output.

Self-synchronizing stream ciphers:

Another approach uses several of the previous N ciphertext digits to compute the keystream.
Such schemes are known as self-synchronizing stream ciphers, asynchronous stream
ciphers or ciphertext autokey (CTAK). The idea of self-synchronization was patented in
1946, and has the advantage that the receiver will automatically synchronise with the
keystream generator after receiving N ciphertext digits, making it easier to recover if digits
are dropped or added to the message stream. Single-digit errors are limited in their effect,
affecting only up to N plaintext digits.

Block cipher:

The basic scheme of a block cipher is depicted as follows −

A block cipher takes a block of plaintext bits and generates a block of ciphertext bits,
generally of same size. The size of block is fixed in the given scheme. The choice of block
size does not directly affect to the strength of encryption scheme. The strength of cipher
depends up on the key length.
Block ciphers process blocks of fixed sizes (say 64 bits). The length of plaintexts is mostly
not a multiple of the block size. For example, a 150-bit plaintext provides two blocks of 64
bits each with third block of balance 22 bits. The last block of bits needs to be padded up
with redundant information so that the length of the final block equal to block size of the
scheme. In our example, the remaining 22 bits need to have additional 42 redundant bits
added to provide a complete block. The process of adding bits to the last block is referred to
as padding.
Too much padding makes the system inefficient. Also, padding may render the system
insecure at times, if the padding is done with same bits always.

Block Cipher Schemes:

There is a vast number of block ciphers schemes that are in use. Many of them are publically
known. Most popular and prominent block ciphers are listed below.

 Digital Encryption Standard (DES) − The popular block cipher of the 1990s. It is
now considered as a ‘broken’ block cipher, due primarily to its small key size.

 Triple DES − It is a variant scheme based on repeated DES applications. It is still a


respected block ciphers but inefficient compared to the new faster block ciphers
available.
 Advanced Encryption Standard (AES) − It is a relatively new block cipher based
on the encryption algorithm Rijndael that won the AES design competition.
 IDEA − It is a sufficiently strong block cipher with a block size of 64 and a key size
of 128 bits. A number of applications use IDEA encryption, including early versions
of Pretty Good Privacy (PGP) protocol. The use of IDEA scheme has a restricted
adoption due to patent issues.
 Twofish − This scheme of block cipher uses block size of 128 bits and a key of
variable length. It was one of the AES finalists. It is based on the earlier block cipher
Blowfish with a block size of 64 bits.
 Serpent − A block cipher with a block size of 128 bits and key lengths of 128, 192,
or 256 bits, which was also an AES competition finalist. It is a slower but has more
secure design than other block cipher.

Program:

Stream cipher implementation using python:

# import the AES library


import pyaes
# Enter plain text of any byte (stream)
i = input("Enter the plain text: ")

# A 256 bit (32 byte) key chosen


key = "abhijit#4387926131r513f124597851"

# CTR mode chosen for stream cipher


aes = pyaes.AESModeOfOperationCTR(str.encode(key))

# cipher text creation


e = aes.encrypt(i)
# or, use this directly
# e = pyaes.AESModeOfOperationCTR(str.encode(key)).encrypt(i)

print("\n The Encrypted text (in bytes): \n", e)

# decrypting cipher text


# The counter mode of operation maintains state, so decryption requires a new instance be
created
aes = pyaes.AESModeOfOperationCTR(str.encode(key))
d = aes.decrypt(e)
# or, use this directly
#d = pyaes.AESModeOfOperationCTR(str.encode(key)).decrypt(e)

print("\n The Decrypted text (in bytes): \n", d)

Output:

Block cipher implementation using python:

# import the DES library using a customized package

import pyDes

# import this library for generating random number by the system

import os
# input

i = input("Enter any string: ")

# Padding function: add ' ' until the string length is multiples of 8

def padded_text(s):

while len(s)%8 !=0 :

s += ' '

return s

p = padded_text(i)

# key should be 8 bytes long.

k_ecb = pyDes.des("DESCRYPT", pyDes.ECB, "\0\0\0\0\0\0\0\0", pad=None,


padmode=None)

# encrypted data i.e. in bytes

e_ecb = k_ecb.encrypt(str.encode(p))

print("\n The encrypted string(in bytes) - \n", e_ecb)

# extract the input text from the encrypted input using decryption

d_ecb = k_ecb.decrypt(e_ecb)

print("\n The actual input(in bytes) - \n", d_ecb)

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy