|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > Encoding strings using System.Security.Cryptography |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|