Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Cryptography
3.
Stream Cipher
3.1.
Synchronous Stream Cipher
3.2.
Self-Synchronous Stream Cipher
3.3.
Keystream
3.4.
Example
3.5.
Advantages of Stream Cipher
4.
Frequently Asked Questions
4.1.
What are the different types of stream cipher?
4.2.
Does stream cipher encrypts a block of data at a time?
4.3.
Why do we use XOR operation in stream cipher?
4.4.
What type of cryptography techniques is applied to the steam cipher?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Cipher, blow the stream

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will discuss one of the cryptographic methods or techniques known as the stream cipher. We will discuss some crucial factors in the stream cipher and implement a mathematical example to demonstrate the stream cipher logic.

Cipher, blow the stream

But before that, let's discuss the term cryptography in brief.

Cryptography

Cryptography is one of the subjects in computer science that focus on secure communication between the sender and receiver and also discusses the methods we can use to secure digital data like digital signature uploaded on the internet.

With cryptography, we do not need to worry about the confidentiality and integrity of the data during transmission.

There are mainly three types of cryptography techniques.

  • Public key or symmetric key cryptography
  • Secret key or asymmetric key cryptography
  • Hash functions
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Stream Cipher

Before discussing stream cipher, you need to know a few terms we will frequently use during the explanation.

  • Plaintext: The normal text we will convert into a non-readable format.
     
  • Ciphertext: The text we will generate after converting the plain text by applying cryptography techniques.
     
  • Key: A key is a string of characters or bits that helps us convert plain text into cipher text and is only known to the sender and receiver.
     
  • Encryption: Encryption converts plain text into cipher text with the help of a secret key.
     
  • Decryption: Decryption converts cipher text into normal or plain text with the help of a secret key.
     

Now let's discuss the stream cipher. 
A stream cipher is a symmetric cryptography technique that converts plain text into cipher text with the help of a keystream. A keystream is a randomly generated string or bits that will help us to convert the plain text into ciphertext. In a stream cipher, the encryption will be applied on a single bit at a time, meaning if the sender sends a 6-bit message, then each of the bits has to go through the encryption.

0

1

1

0

1

1

 

There are two types of stream cipher.

  1. Synchronous stream cipher
  2. Self-Synchronous stream cipher

Synchronous Stream Cipher

In a synchronous stream cipher, the plaintext will be combined or encrypted with the independently generated pseudorandom keystream. 
 

synchronous

Self-Synchronous Stream Cipher

In the self-synchronous stream cipher, the keystream will be generated along with some bits of the cipher text. The reason for doing this is to avoid any tampering with the message stream or ciphertext. If the message stream has been tampered with by someone, the receiver can detect it by comparing the cipher text and keystream. 

Self-Synchronous stream cipher

Keystream

Keystream or secret key is the most crucial part of the stream cipher. A keystream you will use must be confidential except for the sender and receiver. In a stream cipher, mostly the keystream is the same as the size of the plaintext, and it is beneficial because it is not easy for an attacker to generate the keystream.
 

But what if your plain text is large, like 200KB or more than that?

Well, in this case, you can choose a key with some fixed amount, for example, 30 KB, and the rest you can generate with the help of a Keystream generator but remember the keystream generator has to be completely random and will not follow a fixed pattern for the generation.

Example

In the stream cipher,  the XOR operation will be applied on the plain text and keystream to generate the cipher text.  When we use an XOR operation between two bits, it follows some rules.

XOR Operation

Bit 1

Bit 2

Resultant Bit

0

0

0

0

1

1

1

0

1

1

1

0

If you apply XOR on two separate bits and they are the same, then the resultant bit will be 0, and if they are 0 and 1 or 1 and 0, then resulting bit will be 1.
 

Now let's apply the XOR operation on plaintext in the binary format.

 

stream cipher example

As you can see in the image, we have used the XOR operation on the plaintext and keystream and generated a random ciphertext.
 

Let's see a more particle example of the stream cipher.

XOR calculator
  • In the above image, we have used the XOR calculator, an online website. As Input 1, we have given some text; as INPUT 2, we have given a completely random key in hexadecimal format.
     
  • As a result, we have generated a cipher text in hexadecimal format. Keep in mind that INPUT 2 is randomly generated, and changing it will affect the output.
     
  • Now, let's decrypt the cipher text.
XOR calculator
  • As you can see, we got the same plaintext we entered after applying the XOR on ciphertext and Keystream. I hope now you have a more precise understanding of stream cipher.

Advantages of Stream Cipher

  • The benefit of a stream cipher is that it typically operates faster and with much less code than block ciphers because it does not require block ciphers as a building block.
     
  • It encrypts one bit at a time.
     
  • More 1s and 0s can be added to a keystream to increase its efficiency and make cryptanalysis more challenging.
     
  • It can be a great cipher when to want to send a stream of data to a destination.


Check out this problem - XOR Queries On Tree

Must Read, Monte Carlo Simulation

Frequently Asked Questions

What are the different types of stream cipher?

There are two types of stream cipher: Synchronous and Asynchronous stream cipher.

Does stream cipher encrypts a block of data at a time?

Stream cipher encrypted one bit at a time.

Why do we use XOR operation in stream cipher?

XOR uses less computational power to encrypt or decrypt the message.

What type of cryptography techniques is applied to the steam cipher?

Stream cipher follows symmetric key encryption means the sender and receiver both have a mutual secret key.

Conclusion

In this blog, we have discussed the Stream cipher and different cryptography terms. We have also discussed an example to demonstrate the working of a stream cipher, and in the end, we have mentioned the advantages of stream cipher.

To learn more about cryptography, check out the following articles.

To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available to you. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Previous article
Cipher-Permutate It, Keeping Plaintext
Next article
Analysis in Cryptography Cryptanalysis
Live masterclass