Quick Search for:  in language:    
Encryption,Decryption,String
   Code/Articles » |  Newest/Best » |  Community » |  Jobs » |  Other » |  Goto » | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
.Net Stats

 Code: 157,764. lines
 Jobs: 470. postings

 How to support the site

 
Sponsored by:

 
You are in:
 
Login





Latest Code Ticker for .Net.
On The Fly Image Resizing, And Saveing
By Chris Hood on 1/27


Click here to see a screenshot of this code!Zip/View Project
By Mähr Stefan on 1/27

(Screen Shot)

Quick Audit
By Thomas Michael McGeown on 1/27


Click here to see a screenshot of this code!Kill Timer
By Adam( ) on 1/26

(Screen Shot)

Fibonacci Function
By Paddy Pasqualmie on 1/25


ID3v1.1 Reader
By Lewis Moten on 1/25


Click here to see a screenshot of this code!Lotto Picker
By Mick Doherty on 1/24

(Screen Shot)

Load a PopUnder using InternetExplore r
By Norberto Olazabal on 1/23


Stack
By Amit Malhotra on 1/23


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

clsTripleDES.vb

Print
Email
 

Submitted on: 9/15/2003 11:12:08 AM
By: Iain Kusel 
Level: Intermediate
User Rating: By 3 Users
Compatibility:VB.NET

Users have accessed this code 3040 times.
 
 
     Encryption / Decryption of String
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!

    //**************************************
    //     
    // for :clsTripleDES.vb
    //**************************************
    //     
    Based on Wrox article http://www.devarticles.com/art/1/249/6.
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

    //**************************************
    //     
    // Name: clsTripleDES.vb
    // Description:Encryption / Decryption o
    //     f String
    // By: Iain Kusel
    //
    // Assumes:Triple DES algorithm used in 
    //     combination with conversion to base64 st
    //     ring within Encrypted / Decrypted proper
    //     ties for easy storage in e.g. database. 
    //     Uses MemoryStream as opposed to FileStre
    //     am. Key() should be stored in seperate b
    //     inary.
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=1564&lngWId;=10    //for details.    //**************************************
    //     
    
    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Security.Cryptography
    Imports System.Xml
    Public Class clsTES
    Private _CreditCardNumber As String
    Private _EncCreditCardNumber As String
    Public Property CreditCardNumber() As String
    Get
    Return _CreditCardNumber
    End Get
    Set(ByVal Value As String)
    'exit if no value 
    If Value = String.Empty Then Exit Property
    _CreditCardNumber = Value
    'encrypt class ref. set 
    Dim objTES As New clsTES()
    'encrypt ccard string 
    Dim baEnc As Byte() = objTES.Encrypt(_CreditCardNumber)
    'get base64 string from byte array
    Dim s64 As String = System.Convert.ToBase64String(baEnc)
    'set class string enc. 
    _EncCreditCardNumber = s64
    End Set
    End Property
    Public Property EncryptedCreditCardNumber() As String
    Get
    Return _EncCreditCardNumber
    End Get
    Set(ByVal Value As String)
    'exit if no value 
    If Value = String.Empty Then Exit Property
    If _CreditCardNumber = String.Empty Then Exit Property
    _EncCreditCardNumber = Value
    'decrypt class ref. set 
    Dim objTES As New clsTES()
    'get byte array from base64 string i.e. from db stored as base64
    Dim baDec As Byte() = System.Convert.FromBase64String(_CreditCardNumber)
    'decrypt ccard string 
    Dim sDec As String = objTES.Decrypt(baDec)
    'set string decrypted
    _CreditCardNumber = sDec
    End Set
    End Property
    'keys 
    Private key() As Byte = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}
    Private iv() As Byte = {65, 110, 68, 26, 69, 178, 200, 219}
    Public Function Encrypt(ByVal plainText As String) As Byte()
    ' Declare a UTF8Encoding object so we may use the GetByte 
    ' method to transform the plainText into a Byte array. 
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim inputInBytes() As Byte = utf8encoder.GetBytes(plainText)
    ' Create a new TripleDES service provider 
    Dim tdesProvider As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
    ' The ICryptTransform interface uses the TripleDES 
    ' crypt provider along with encryption key and init vector 
    ' information 
    Dim cryptoTransform As ICryptoTransform = tdesProvider.CreateEncryptor(Me.key, Me.iv)
    ' All cryptographic functions need a stream to output the 
    ' encrypted information. Here we declare a memory stream 
    ' for this purpose. 
    Dim encryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(encryptedStream, _
    cryptoTransform, CryptoStreamMode.Write)
    ' Write the encrypted information to the stream. Flush the information 
    ' when done to ensure everything is out of the buffer. 
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    encryptedStream.Position = 0
    ' Read the stream back into a Byte array and return it to the calling 
    ' method. 
    Dim result(encryptedStream.Length - 1) As Byte
    encryptedStream.Read(result, 0, encryptedStream.Length)
    cryptStream.Close()
    'test section
    'Convert to / fro the memory stream to a base64 string
    'compare decText to result
    Dim encText As String = Convert.ToBase64String(encryptedStream.ToArray())
    Dim decText As Byte() = Convert.FromBase64String(encText)
    Dim sDec As String = Decrypt(decText)
    Return result
    End Function
    Public Function Decrypt(ByVal inputInBytes() As Byte) As String
    ' UTFEncoding is used to transform the decrypted Byte Array 
    ' information back into a string. 
    Dim utf8encoder As UTF8Encoding = New UTF8Encoding()
    Dim tdesProvider As TripleDESCryptoServiceProvider = New _
    TripleDESCryptoServiceProvider()
    ' As before we must provide the encryption/decryption key along with 
    ' the init vector. 
    Dim cryptoTransform As ICryptoTransform = _
    tdesProvider.CreateDecryptor(Me.key, Me.iv)
    ' Provide a memory stream to decrypt information into 
    Dim decryptedStream As MemoryStream = New MemoryStream()
    Dim cryptStream As CryptoStream = New CryptoStream(decryptedStream, _
    cryptoTransform, CryptoStreamMode.Write)
    cryptStream.Write(inputInBytes, 0, inputInBytes.Length)
    cryptStream.FlushFinalBlock()
    decryptedStream.Position = 0
    ' Read the memory stream and convert it back into a string 
    Dim result(decryptedStream.Length - 1) As Byte
    decryptedStream.Read(result, 0, decryptedStream.Length)
    cryptStream.Close()
    Dim myutf As UTF8Encoding = New UTF8Encoding()
    Return myutf.GetString(result)
    End Function
    End Class

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
9/15/2003 5:42:46 PM:Danny J
I am sorry but if you are going to 
start converting other peoples code I 
suggest you use C# as your programming 
language danny j
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
9/17/2003 8:47:36 AM:Christina McEntire
Thanks -- this will be helpful!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/14/2003 4:47:10 AM:Niknak!!
"Danny J", what???  You are talking 
nonsence, if the code has been 
converted what language was it in 
before?  And what is wrong with VB?  I 
think *you* need to learn how to read 
both!!!  I'll give 5 just for your 
comment!
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/14/2003 7:23:35 AM:
I'm sorry to see so many posting really 
bad security-code here at 
planetsourcecode.. Just to point out 
the most obvious: never hardcode the 
key in your code!
You'll learn 
everything you need to know about how 
to use crypto in .NET by reading one or 
two articles on MSDN.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
11/14/2003 7:27:26 AM:Iain Kusel
In the Assumption comment for the 
code:
Key() should be stored in 
seperate  binary.
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | .Net Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.