473,799 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Crypto Question

I have been playing around with encrypting passwords using a class found in a
MS KB (see farther down). It seems to work great so long as the original
password is comprised of characters on the keyboard. However, if the
password is mixed with characters in both ASCII code set 0-127 and 128-255, I
run into a problem. Users can set a password using keyboard characters and
by holding ALT and typing in the decimal value for the non-keyboard
characters. The password is encrypted, but when decrypted, it doesn't match
the original. I'm not sure if the problem is in the encrypting or decrypting
or both.

I would GREATLY appreciate it if someone could review the code below and
discover my problem....

Thanks,
Mark
=============== =============== =============== =======
Imports System.Security .Cryptography
Public Class Crypto

' TAKEN FROM MS KB Q317535

Public Shared Function EncryptTripleDE S(ByVal sIn As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()

' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the encryptor.
Dim DESEncrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateEncry ptor()
' Get a byte array of the string.
Dim Buffer As Byte() = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn)
' Transform and return the string.
Return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(Buffer ,
0, Buffer.Length))
End Function

Public Shared Function DecryptTripleDE S(ByVal sOut As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()
' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the decryptor.
Dim DESDecrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateDecry ptor()
Dim Buffer As Byte() = Convert.FromBas e64String(sOut)
' Transform and return the string.
Return
System.Text.ASC IIEncoding.ASCI I.GetString(DES Decrypt.Transfo rmFinalBlock(Bu ffer, 0, Buffer.Length))
End Function

Private Shared Function ScrambleKey(ByV al v_strKey As String) As String

Dim sbKey As New System.Text.Str ingBuilder
Dim intPtr As Integer
For intPtr = 1 To v_strKey.Length
Dim intIn As Integer = v_strKey.Length - intPtr + 1
sbKey.Append(Mi d(v_strKey, intIn, 1))
Next

Dim strKey As String = sbKey.ToString

Return sbKey.ToString

End Function

End Class
Apr 13 '06 #1
2 1923

Mark wrote:
I have been playing around with encrypting passwords using a class found in a
MS KB (see farther down). It seems to work great so long as the original
password is comprised of characters on the keyboard. However, if the
password is mixed with characters in both ASCII code set 0-127 and 128-255, I
run into a problem. Users can set a password using keyboard characters and
by holding ALT and typing in the decimal value for the non-keyboard
characters. The password is encrypted, but when decrypted, it doesn't match
the original. I'm not sure if the problem is in the encrypting or decrypting
or both.

I would GREATLY appreciate it if someone could review the code below and
discover my problem....

Thanks,
Mark
=============== =============== =============== =======
Imports System.Security .Cryptography
Public Class Crypto

' TAKEN FROM MS KB Q317535

Public Shared Function EncryptTripleDE S(ByVal sIn As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()

' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the encryptor.
Dim DESEncrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateEncry ptor()
' Get a byte array of the string.
Dim Buffer As Byte() = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn)
' Transform and return the string.
Return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(Buffer ,
0, Buffer.Length))
End Function

Public Shared Function DecryptTripleDE S(ByVal sOut As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()
' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the decryptor.
Dim DESDecrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateDecry ptor()
Dim Buffer As Byte() = Convert.FromBas e64String(sOut)
' Transform and return the string.
Return
System.Text.ASC IIEncoding.ASCI I.GetString(DES Decrypt.Transfo rmFinalBlock(Bu ffer, 0, Buffer.Length))
End Function

Private Shared Function ScrambleKey(ByV al v_strKey As String) As String

Dim sbKey As New System.Text.Str ingBuilder
Dim intPtr As Integer
For intPtr = 1 To v_strKey.Length
Dim intIn As Integer = v_strKey.Length - intPtr + 1
sbKey.Append(Mi d(v_strKey, intIn, 1))
Next

Dim strKey As String = sbKey.ToString

Return sbKey.ToString

End Function

End Class


Mark... Well you are using the Encoding.ASCII class. That is going to
limit you to the 0-127 range, since that is what ASCII is defined as.
This is only a guess, but try using the Encoding.Defaul t instead. That
should use the default code page - which will probably do what you
want.

--
Tom Shelton [MVP]

Apr 13 '06 #2
Thanks! I'll give that a shot.

Mark

"Tom Shelton" wrote:

Mark wrote:
I have been playing around with encrypting passwords using a class found in a
MS KB (see farther down). It seems to work great so long as the original
password is comprised of characters on the keyboard. However, if the
password is mixed with characters in both ASCII code set 0-127 and 128-255, I
run into a problem. Users can set a password using keyboard characters and
by holding ALT and typing in the decimal value for the non-keyboard
characters. The password is encrypted, but when decrypted, it doesn't match
the original. I'm not sure if the problem is in the encrypting or decrypting
or both.

I would GREATLY appreciate it if someone could review the code below and
discover my problem....

Thanks,
Mark
=============== =============== =============== =======
Imports System.Security .Cryptography
Public Class Crypto

' TAKEN FROM MS KB Q317535

Public Shared Function EncryptTripleDE S(ByVal sIn As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()

' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the encryptor.
Dim DESEncrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateEncry ptor()
' Get a byte array of the string.
Dim Buffer As Byte() = System.Text.ASC IIEncoding.ASCI I.GetBytes(sIn)
' Transform and return the string.
Return Convert.ToBase6 4String(DESEncr ypt.TransformFi nalBlock(Buffer ,
0, Buffer.Length))
End Function

Public Shared Function DecryptTripleDE S(ByVal sOut As String, ByVal sKey
As String) As String
Dim DES As New
System.Security .Cryptography.T ripleDESCryptoS erviceProvider( )
Dim hashMD5 As New
System.Security .Cryptography.M D5CryptoService Provider()
' scramble the key
sKey = ScrambleKey(sKe y)
' Compute the MD5 hash.
DES.Key =
hashMD5.Compute Hash(System.Tex t.ASCIIEncoding .ASCII.GetBytes (sKey))
' Set the cipher mode.
DES.Mode = System.Security .Cryptography.C ipherMode.ECB
' Create the decryptor.
Dim DESDecrypt As System.Security .Cryptography.I CryptoTransform =
DES.CreateDecry ptor()
Dim Buffer As Byte() = Convert.FromBas e64String(sOut)
' Transform and return the string.
Return
System.Text.ASC IIEncoding.ASCI I.GetString(DES Decrypt.Transfo rmFinalBlock(Bu ffer, 0, Buffer.Length))
End Function

Private Shared Function ScrambleKey(ByV al v_strKey As String) As String

Dim sbKey As New System.Text.Str ingBuilder
Dim intPtr As Integer
For intPtr = 1 To v_strKey.Length
Dim intIn As Integer = v_strKey.Length - intPtr + 1
sbKey.Append(Mi d(v_strKey, intIn, 1))
Next

Dim strKey As String = sbKey.ToString

Return sbKey.ToString

End Function

End Class


Mark... Well you are using the Encoding.ASCII class. That is going to
limit you to the 0-127 range, since that is what ASCII is defined as.
This is only a guess, but try using the Encoding.Defaul t instead. That
should use the default code page - which will probably do what you
want.

--
Tom Shelton [MVP]

Apr 13 '06 #3

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

Similar topics

0
1191
by: Simon Roses Femerling | last post by:
Hey all :) I'm looking for a good crypto toolkit (that should be multiplatform, work on python 2.3, etc..) What I have found looks old and too complex (installing 3 party libs, etc.) I just want to encrypt files using a secure encryption. I guess a pure python toolkit is out of the question! Any comments ?
1
1890
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2 from the original lenght in order to get it to work as there seems to be 2 extra 0 bytes at the end. Functions included Stu
2
3793
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
6
6585
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at using this library and to familiarise myself writing small tests with each of the ciphers. When I hit Crypto.Cipher.ARC4 I've
13
2001
by: Andy Chau | last post by:
I try to use RSA to implement the following scheme but wasn't sucessful. Sever encrypt a message using a public key, the client decrpyt the message using a private key. I don't want the client to be able to encrypt a message. However, using the Crypto API I need to pass in both the private and public key pairs in order to decrypt the message. When the client has both private and public key, it can just use the public
0
2134
by: Slug | last post by:
Hello all, I've been trying to get a public key solution working but have been having a few problems. For starters there is a lot of contradictory information out there, MSDN is not much help, and a lot of the sample code available I have found are buggy so don't provide much insight. Unfortunately for me every developer I know either gives me a completely blank look when I try to talk crypto, or they have some wildly inaccurate and...
5
3761
by: vermarajeev | last post by:
Hi guys, I want to encrypt/decrypt a file with AES in CTR mode using crypto++ library. To encrypt a file using AES in CTR mode the solution is something like this int CRYPTOPP_API main(int argc, char *argv) { std::string command, executableName, macFilename;
2
2505
by: vermarajeev | last post by:
Hi guys, I have written code to encrypt and decrypt files using perl script. Please help me to port below code to crypto++ library. //ENCRYPTION my $cipher = Crypt::CBC->new( -cipher => "Crypt::Rijndael", -key => $key, -header => 'salt', );
12
2264
by: Fett | last post by:
I need a crypto package that works on windows with python 2.5. Can anyone suggest one for me? I have been searching for a couple days for a good cryptography package to use for public/private key encryption, at this point I would settle for symmetric even. Every encryption package I have found for python was either operating system specific (read *nix only): http://www.freenet.org.nz/ezPyCrypto/
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
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
6803
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
5459
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
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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
2935
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.