P2Pprogrammer 2 programmer

Home > Tips > Encryption and Decryption > Symmetric IV Encryption

Symmetric Encryption

Secret Key Encryption, Stream Cipher, Initialize Vector, Symmetric IV Encryption Decryption Source Code , Download .NET Encryption Source Code

What is Symmetric or Secret Key Encryption?
In this type of encryption both the sender and recipient posses the same key, to encrypt and decrypt the data, for successful operation both parties must agree upon a shared secret key. If there are "n" correspondent one have to keep track of n-different secret keys. if the same key is used by encrypt more than one then key holder can easily decrypt all data. Symmetric encryption schemes are also subject to authenticity problems. because, encrypter and decrypter have same secret key identity of encrypter or decrypter cannot be determine, both can encrypt or decrypt the data. There is mainly two type of symmetric or secret key encryption, Stream Ciphers and Block Ciphers

Stream Ciphers
The simplest stream cipher is called the Vernam Cipher. A bit from a key is added, with the carry discarded, to a bit from the plain text to get one bit of cipher text. XOR encryption is the example of stream cipher, it is turn out to be easy to crack. it is weak when repeating the same keys, but it can be very effective when the key stream varies constantly. The most well known stream cipher is RC4.

Block Ciphers
Block ciphers are designed to take data blocks of particular size, encrypt them with a key in particular size, and get a block of cipher text of a particular size, Block ciphers encrypt data in fixed size blocks. To encrypt a stream of data, the data must be broken into block size pieces and each block encrypted individually. When decrypted in the right order the encrypted blocks get the original plain text.

In geometrical pattern encoding we are using block ciphers, today's practical block ciphers all generate a cipher text block same size as the palin text block. Data Encryption Standard (DES) may be the best block cipher. Block ciphers are analyzed and tested for their ability to encrypt the data blocks of their given block size. A statistical analysis of cipher text generated by the block cipher algorithm should find that individual data bits as well as pattern of bits appear completely random.

Data block size and secret key size in block ciphers

Encryption Algorithm Data Block (Bits) Secret Key (Bits)
Data Encryption Standard (DES) 64 56
International Data encryption Algorithm (IDEA) 64 128
Modular Multiplication Block (MMB) 128 128

Patterns in the cipher text become a problem when we apply same secret keys to stream of data. if we encrypt the same block twice with same key, it returns same cipher text for both occurrence, for a experienced cracker easily identified the visual pattern of cipher text and exploit the cipher text with some reverse calculation. This makes the messages more vulnerable to attack. if we encrypt a stream block by block, patterns in the plaintext will produce statistically significant patterns in the cipher text. these pattern can give a code breaker the entry point for an attack.

Initialize Vector (IV) 
Cipher block chaining in the plain text by systematically combining each plaintext block with a cipher text before actually encrypting it, the two blocks combined bit by bit using XOR operation, instead of directly encrypting plaintext data, the block cipher encrypts the plain text data after it is combined with random looking cipher text, the process started with a block of random bits called the initialization vector (IV). two same plain text will never look same in cipher text as the IV is different for both the case.

In following Example we use symmetric key for both encryption and decryption, however if we encrypt same text repeating time it creates different cipher text every time, we use Initialization vector for selecting the bit pattern for each time, we pass the same bit pattern to cipher text block to decrypt it to plain text.

Function for encryption

private Function StrEncrypt(ByVal EnStr As String) As String

        Dim Key As String

        Key = "Abt$9>3ZyX 21~)**1_0d%1xOp0#?s!14k-L7`3s9cxPo1ilIj=-0DnmOpas#$%5854/*?>00021atanu???"


        Dim p1 As Integer


        p1 = (Rnd() * 8) + 1

        Dim p2 As Integer

        p2 = Len(EnStr)

        Dim RandSeed As Integer

        RandSeed = p1

        Dim i As Integer

        Dim s1 As String = ""

        Dim ft As String

        ft = ""

        For i = 1 To 50

            s1 = s1 & Chr(Asc(Rnd() * 255))



        ft = Chr(p1) & Chr(p2)

        Dim iXor As Integer

        For i = 1 To Len(EnStr)

            iXor = Asc(Mid(KeyPair, i + p1, 1)) Xor Asc(Mid(EnStr, i, 1))

            ft = ft & Chr(iXor)


        For i = Len(ft) To 50

            ft = ft & Chr(Rnd() * 255)


        StrEncrypt = ft

End Function


Function for decryption

Private Function StrDecrypt(ByVal EnStr As String) As String

        Dim Key As String

        Key = "Abt$9>3ZyX 21~)**1_0d%1xOp0#?s!14k-L7`3s9cxPo1ilIj=-0DnmOpas#$%5854/*?>00021atanu???"


        Dim EText As String

        Dim Rt As String

        Dim p1, i As Integer

        EText = EnStr

        Dim Pbit As Integer

        Pbit = Asc(Mid(EText, 1, 1))

        p1 = Asc(Mid(EText, 1, 1))

        Dim PLen As Integer

        PLen = Asc(Mid(EText, 2, 1))

        Rt = ""


        For i = 3 To PLen + 2

            Rt = Rt & Chr(Asc(Mid(EText, i, 1)) Xor Asc(Mid(Key, i - 2 + p1, 1)))


        StrDecrypt = Rt

End Function

For example if we use plain text "ATANU MAITY" and encrypt it with StrEncrypt function it will create cipher text different for every time.

  CASE 1.
  StrEncrypt("ATANU MAITY")
  Cipher Text " xjr,xmsx*p_qc€ý÷ßZÊödñ¸H`xü5¯†9³ŒzŪ¢¿ ,ë¾À"

  CASE 2.
  StrEncrypt("ATANU MAITY")
  Cipher Text ":i#ubK\{jizC,,g¾°8;TÑl›¬ÚŠjm1v"ë3a9>|ô+•`P=/"

  CASE 3.
  Cipher Text "366bu9\VŒª…Vßΰ’—ÐJŒ¨‹Ÿ˜ [Js¥P߂嚢8`7Ÿ Ýh¥"

if you carefully analysis with Case 1 and Case 2, you can not find any pattern match between two cipher text although pain text and secret key are same. and if you analysis case no 1 with case no 2 then you can find the length of cipher text dose not vary with the length of plain text.

For completing listing of the code and sample Visual Basic and C# .NET project for Symmetric IV Encryption can be found in Sample Code section.

Home > Tips > Encryption and Decryption > Symmetric IV Encryption