473,789 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TripleDES class - Suggestions Requested.

i have a class i found/wrote (meaning i found it originally and
tinkered with it to make it mine) and i would like some suggestions on
how to make it better. i am looking for things i need to add,
weaknesses, whatnot. Thanks in advance.

-----------------------------------------------------------------------------------------------------------------

Imports System.Security .Cryptography
Imports System.Security
Imports System.Text
Imports System.IO

Public Class encoder

Private m_des As New TripleDESCrypto ServiceProvider
Private m_utf8 As New UTF8Encoding
Private m_key() As Byte
Private m_iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}

Public Function Encrypt(ByVal text As String, ByVal strPassword As
String) As String
m_key =
System.Text.Enc oding.UTF8.GetB ytes(strPasswor d.PadRight(24, "&"))
Dim input() As Byte = m_utf8.GetBytes (text)
Dim output() As Byte = Transform(input ,
m_des.CreateEnc ryptor(m_key, m_iv))
Return Convert.ToBase6 4String(output)
End Function

Public Function Decrypt(ByVal text As String, ByVal strPassword As
String) As String
m_key =
System.Text.Enc oding.UTF8.GetB ytes(strPasswor d.PadRight(24, "&"))
Dim input() As Byte = Convert.FromBas e64String(text)
Dim output() As Byte = Transform(input ,
m_des.CreateDec ryptor(m_key, m_iv))
Return m_utf8.GetStrin g(output)
End Function

Private Function Transform(ByVal input() As Byte, ByVal
CryptoTransform As ICryptoTransfor m) As Byte()
Dim memStream As MemoryStream = New MemoryStream
Dim cryptStream As CryptoStream = New CryptoStream(me mStream,
CryptoTransform , CryptoStreamMod e.Write)
cryptStream.Wri te(input, 0, input.Length)
cryptStream.Flu shFinalBlock()
memStream.Posit ion = 0
Dim result(CType(me mStream.Length - 1, System.Int32)) As Byte
memStream.Read( result, 0, CType(result.Le ngth, System.Int32))
memStream.Close ()
cryptStream.Clo se()
Return result
End Function

End Class

-----------------------------------------------------------------------------------------------------------------

Feb 16 '06 #1
1 1310
Make it more general purpose. For example:

1) Create a constructor that can set the key data up
2) Don't hard your Encrypt and Decrypt to only work for your specific need
today. Make these types of functions general.
3) If you want to add an Encrypt/Decrypt Password function make them
seperate functions or possibly a derived class.

BTW. You do know there is an easier way to encrypt passwords? Have you
ever tried using SHA256 instead of 3DES. If you use SHA you don't need to
keep a key around somewhere.

"d4******@gmail .com" wrote:
i have a class i found/wrote (meaning i found it originally and
tinkered with it to make it mine) and i would like some suggestions on
how to make it better. i am looking for things i need to add,
weaknesses, whatnot. Thanks in advance.

-----------------------------------------------------------------------------------------------------------------

Imports System.Security .Cryptography
Imports System.Security
Imports System.Text
Imports System.IO

Public Class encoder

Private m_des As New TripleDESCrypto ServiceProvider
Private m_utf8 As New UTF8Encoding
Private m_key() As Byte
Private m_iv() As Byte = {8, 7, 6, 5, 4, 3, 2, 1}

Public Function Encrypt(ByVal text As String, ByVal strPassword As
String) As String
m_key =
System.Text.Enc oding.UTF8.GetB ytes(strPasswor d.PadRight(24, "&"))
Dim input() As Byte = m_utf8.GetBytes (text)
Dim output() As Byte = Transform(input ,
m_des.CreateEnc ryptor(m_key, m_iv))
Return Convert.ToBase6 4String(output)
End Function

Public Function Decrypt(ByVal text As String, ByVal strPassword As
String) As String
m_key =
System.Text.Enc oding.UTF8.GetB ytes(strPasswor d.PadRight(24, "&"))
Dim input() As Byte = Convert.FromBas e64String(text)
Dim output() As Byte = Transform(input ,
m_des.CreateDec ryptor(m_key, m_iv))
Return m_utf8.GetStrin g(output)
End Function

Private Function Transform(ByVal input() As Byte, ByVal
CryptoTransform As ICryptoTransfor m) As Byte()
Dim memStream As MemoryStream = New MemoryStream
Dim cryptStream As CryptoStream = New CryptoStream(me mStream,
CryptoTransform , CryptoStreamMod e.Write)
cryptStream.Wri te(input, 0, input.Length)
cryptStream.Flu shFinalBlock()
memStream.Posit ion = 0
Dim result(CType(me mStream.Length - 1, System.Int32)) As Byte
memStream.Read( result, 0, CType(result.Le ngth, System.Int32))
memStream.Close ()
cryptStream.Clo se()
Return result
End Function

End Class

-----------------------------------------------------------------------------------------------------------------

Feb 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3090
by: Jonas | last post by:
I have the following perl program witch i use to encrypt a password file with. In perl 5.6 this program works like a charm but when trying it on the RED HAT EL 3 platform (taroon) is doesnt decrypt the encrypted string right. Program use Crypt::TripleDES; sub generate() {
8
13543
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly decrypt several of the first few characters. Here's the code: class TMyCipher {
3
2762
by: Chua Wen Ching | last post by:
Hi there, I had applied this security permissions in my class library based on fxcop standards. Before namespace: using System.Runtime.InteropServices; using System.Security.Permissions;
7
17880
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes); byte key = Encoding.ASCII.GetBytes("0123456789012345");
5
1833
by: ozie | last post by:
Hi , I am new to ASP.NET. I was reading about Page class in one of the ASP.NET books and am confused with the way the Page class is actually implemented.What is didnt understand in when is this Page class created ? Is it when the ASPX file is requested for the first time ? Is this executable Page object saved somewhere in the server hard disk space or is it available only in the memory ? I would really like to know more about how all...
2
30546
by: GRB | last post by:
A client wants me to decrypt a cookie. They encrypted the data in java using TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm. Ive tried to decrypt in vb.net and get the above error. Below is the class I use and the code I use to call it. Not sure wher I'm going wrong here. The TripleDES service provider only uses a 192 bit 24 character length key. Any help would be greatly appreciated.
12
2349
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to be able to access the vector through a string which is either x, y or z. So that I can use: ---xyzvec.hh-- #ifndef XYZVEC_HH #define XYZVEC_HH
1
2097
by: Celia | last post by:
I am working with a web service which requires enryption of some XML fields. It will be symmetric encryption - TripleDES. The vendor has given me the Key that we will share. With the exception that there are 48 characters in the actual Key that he gave me, here is an example of what he gave me: 0E329232EA6D0D73 (The vendor has told me to use all zeros for the IV.) How can I use this with the .Key property of...
0
2174
by: qfmomen | last post by:
I have encrypted password in db added by java application using tripleDES. I need to build same class but it is giving me different result. This class i have used: JavaProject: Making TripleDES Simple in Visual Basic .NET. Free source code and programming help Any help? Qurban
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.