Click here to Skip to main content
6,595,854 members and growing! (19,522 online)
Email Password   helpLost your password?
General Programming » Cryptography & Security » Encryption     Intermediate License: The Microsoft Public License (Ms-PL)

Implementing Caesar Cipher in VB.NET

By Deobrat Singh

This article implements Caesar Cipher (shift by 3) in VB.NET.
VB, Windows, .NET 2.0VS2005, Dev
Posted:16 Aug 2006
Updated:2 Nov 2006
Views:22,592
Bookmarked:13 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 2.73 Rating: 2.53 out of 5
4 votes, 33.3%
1
3 votes, 25.0%
2
1 vote, 8.3%
3

4
4 votes, 33.3%
5

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..

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Deobrat Singh


Member
I am a developer (okay... that's very obvious!). I started programming at the age of 17 when I entered my engineering graduation back in the year 2001. I started off with C, C++ on UNIX. But soon I realized that if I need to make money out of my skills, I need to add some visual element to my applications (and that I need to switch to MS technologies!). Someone told me about VB. I started learning it but couldn’t get thru (I was religiously in love with curly braces; I never felt VB is a language). Then I began with VC++ 6.0 and I fell in love with it. Since then, I have been working on various MS technologies (C++, C#, VB.NET, SQL Server 2000/2005 etc.) I graduated in 2005 and currently, I am working with Microsoft in Hyderabad, India.
That’s all I can write as of now. If you want to know more, drop me a mail, I will try and respond Smile
Occupation: Software Developer
Company: Microsoft India
Location: India India

Other popular Cryptography & Security articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 1 of 1 (Total in Forum: 1) (Refresh)FirstPrevNext
GeneralCIS Security Pinmemberkeith franklin12:54 30 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 Nov 2006
Editor: Smitha Vijayan
Copyright 2006 by Deobrat Singh
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project