ASP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingASP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 6th, 2003, 04:52 PM
vpos vpos is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 1 vpos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Encoding strings using System.Security.Cryptography

Hello,

I have a problem with the code below. When I encrypt and decrypt the passwords on my development machine, the code below works like a charm, but when other members of my team use the same exact methods, they get different values for the same strings when encrypting them. Since the DES uses the same Key - Vector pair, this is some what confusing me. Does any one have an idea why this is happening? With synchronous encryption, given the key, this should work the same on different machines.

Please help me shed some light on this.

Thanks

VP

Public Class Crypto

'8 bytes randomly selected for both the Key and the Initialization Vector
'the IV is used to encrypt the first block of text so that any repetitive
'patterns are not apparent
Private Shared KEY_64() As Byte = {42, 16, 93, 156, 78, 4, 218, 32}
Private Shared IV_64() As Byte = {55, 103, 246, 79, 36, 99, 167, 3}

'24 byte or 192 bit key and IV for TripleDES
Private Shared KEY_192() As Byte = {42, 16, 93, 156, 78, 4, 218, 32, _
15, 167, 44, 80, 26, 250, 155, 112, _
2, 94, 11, 204, 119, 35, 184, 197}
Private Shared IV_192() As Byte = {55, 103, 246, 79, 36, 99, 167, 3, _
42, 5, 62, 83, 184, 7, 209, 13, _
145, 23, 200, 58, 173, 10, 121, 222}

'Standard DES encryption
Public Shared Function Encrypt(ByVal value As String) As String

If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = _
New DESCryptoServiceProvider()
Dim ms As MemoryStream = New MemoryStream()
Dim cs As CryptoStream = _
New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)

sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()

'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length))
End If
End Function


'Standard DES decryption
Public Shared Function Decrypt(ByVal value As String) As String
If value <> "" Then
Dim cryptoProvider As DESCryptoServiceProvider = _
New DESCryptoServiceProvider()

'convert from string to byte array
Dim buffer() As Byte = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = _
New CryptoStream(ms, cryptoProvider.CreateDecryptor(KEY_64, IV_64), _
CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)

Return sr.ReadToEnd()
End If
End Function

End Class

Reply With Quote
  #2  
Old September 22nd, 2008, 05:17 AM
samdoss samdoss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2008
Posts: 1 samdoss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 27 sec
Reputation Power: 0
Arrow Encrypt and Decrypt Password C#.Net - Samdoss

Hi,

Encrypt and Decrypt Password (Easy one)




Namespace: using System.Security.Cryptography;

form_load()
{
UnicodeEncoding bytConvertor = new UnicodeEncoding();
//Enter the string here
byte[] plainData = bytConvertor.GetBytes("samdoss");

RSACryptoServiceProvider RSAServiceProvider = new RSACryptoServiceProvider();
//Encrypt the string
byte[] enData = Encrypt(plainData, RSAServiceProvider.ExportParameters(false));

MessageBox.Show("Encrypted Output: {0}", bytConvertor.GetString(enData));
//Decrypt the string
byte[] deData = Decrypt(enData, RSAServiceProvider.ExportParameters(true));

MessageBox.Show("Decrypted Output: {0}", bytConvertor.GetString(deData));

}

//The below function encrypt the data

static private byte[] Encrypt(byte[] DataToEncrypt, RSAParameters keyInfo)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(keyInfo);
return RSA.Encrypt(DataToEncrypt, false);
}

//The below function decrypt the data

static private byte[] Decrypt(byte[] DataToDecrypt, RSAParameters keyInfo)
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
RSA.ImportParameters(keyInfo);
return RSA.Decrypt(DataToDecrypt, false);
}


Regards,

Software Developer

Samdoss. J

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingASP Development > Encoding strings using System.Security.Cryptography


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap