65.9K
CodeProject is changing. Read more.
Home

Implementing Caesar Cipher in VB.NET

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.53/5 (12 votes)

Aug 17, 2006

Ms-PL
viewsIcon

82358

downloadIcon

2495

This article implements Caesar Cipher (shift by 3) in VB.NET.

Sample Image - Caesar.jpg

Introduction

This article describes implementing the Caesar Cipher (shift by 3) algorithm in VB.NET 2005. It’s a very simple piece of code (was difficult for me as I belong to the C# family). This was one of my practice applications to get my hands on VB.NET.

Background

Caesar Cipher is a rotation based encryption algorithm that replaces each character in the input string with the third (or nth) next character. For example, “ABC” when encrypted, will become “DEF”. Decryption is similar, simply rotate in the backward direction.

Using the code

The solution has a class CaesarCipher which you can use in your application for encrypting and decrypting stuff. This solution applies not just to readable English characters but to any Unicode character (making it specific to English would have been a little tricky).

The source code of the CaesarCipher class is given below:

Friend Class CaeserCipher

    ''' <summary>
    ''' The number of places to shift for encrypting or decrypting
    ''' </summary>
    ''' <remarks></remarks>
    Private _shiftCount As Integer

    ''' <summary>
    ''' Gets or sets the number of places to shift while
    ''' encrypting or decrypting
    ''' </summary>
    ''' <value>The number of places to shift while
    '''        encrypting or decrypting</value>
    ''' <returns>The number of places to shift while
    '''          encrypting or decrypting</returns>
    ''' <remarks></remarks>
    Public Property ShiftCount() As Integer
        Get
            Return _shiftCount
        End Get
        Set(ByVal value As Integer)
            _shiftCount = value
        End Set
    End Property

    ''' <summary>
    ''' Default constructor. Creates a new CaesarCipher
    ''' object with ShiftCount = 3
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        Me.New(3)
    End Sub

    ''' <summary>
    ''' Creates a new CaesarCipher object with specified ShiftCount
    ''' </summary>
    ''' The number of places to shift
    ''' while encrypting or decrypting
    ''' <remarks></remarks>
    Public Sub New(ByVal shiftCount As Integer)
        Me.ShiftCount = shiftCount
    End Sub

    ''' <summary>
    ''' Encrypts a given string
    ''' </summary>
    ''' The plainText string to encrypt
    ''' <returns>Encrypted ciphertext</returns>
    ''' <remarks></remarks>
    Public Function Encipher(ByVal plainText As String) As String
        Dim cipherText As String = String.Empty
        Dim cipherInChars(plainText.Length) As Char
        For i As Integer = 0 To plainText.Length - 1
            cipherInChars(i) = _
               Convert.ToChar((Convert.ToInt32(
               Convert.ToChar(plainText(i))) + Me.ShiftCount))
        Next
        cipherText = New String(cipherInChars)
        Return cipherText
    End Function

    ''' <summary>
    ''' Decrypts given cipherText
    ''' </summary>
    ''' The cipherText to decrypt
    ''' <returns>Decrypted Plaintext</returns>
    ''' <remarks></remarks>
    Public Function Decipher(ByVal cipherText As String) As String
        Dim plainText As String = String.Empty
        Dim cipherInChars(cipherText.Length) As Char
        For i As Integer = 0 To cipherText.Length - 1
            cipherInChars(i) = _
               Convert.ToChar((Convert.ToInt32(
               Convert.ToChar(cipherText(i))) - Me.ShiftCount))
        Next
        plainText = New String(cipherInChars)
        Return plainText
    End Function

End Class

Enhancements

No exception handling has been done, and the code is not unbreakable. So please modify it if you plan to use it in your application..