|
Thanks very much, This is a BIG help to encrypt and decrypt passwords for our Project
|
|
|
|
|
|
Very well and Easy algorythm!
Thank you!
|
|
|
|
|
hi,
i'm doing a comparison of performance between ECC and RSA ,
i used ECDsaCng class for ECC,i used the SignData(),verifyData() methods.
i used RSACryptoServiceProvider class for RSA,using signData(),VerifyData() methods also.
but the signing of data is including the compute of hash,and i want to do the comparsion
between the tow algorithms as encryption methods.
so the signing and verifying of data times between the tow algorithms will not be accurate.
and i noticed there are Encrypt(),Decrypt() methods in RSACryptoServiceProvider class
but no such methods in ECDsa class,
so the question is :how can i use ECC as encryption method,like to encrypt a message by the
public key and decrypt it by the private or encrypt it bey private and decrypt by public?
another question is :how can i use RSAcng class because i didn't find it in
System.Security.Cryptography namespace,and i am using VS 2008.
here is some info about this class in this link.
http://clrsecurity.codeplex.com/wikipage?title=Security.Cryptography.RSACng
|
|
|
|
|
|
Hi there,
I need a c++ version of the decryption so far my attempts have failed this is my code:
BYTE* Base64Decode2(LPCTSTR lpData, DWORD dwSize, DWORD *dwBufSize)
{
DWORD dwResult = 0;
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult,NULL,NULL))
{
BYTE * decodedStr;
decodedStr = new BYTE;
LPTSTR lpszBase64Decoded = new TCHAR[dwResult+(sizeof(TCHAR) * 2)];
memset(decodedStr,0,dwResult);
if(CryptStringToBinary(lpData, dwSize, CRYPT_STRING_BASE64,decodedStr, &dwResult,NULL,NULL))
{
*dwBufSize = dwResult;
return decodedStr;
}
}
return NULL;
}
LPTSTR Base64Encode2(BYTE* lpData, DWORD dwSize, BOOL bStripCRLF, DWORD *dwbufSize)
{
DWORD dwResult = 0;
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, NULL, &dwResult))
{
LPTSTR lpszBase64 = new TCHAR[dwResult];
if(CryptBinaryToString(lpData, dwSize, CRYPT_STRING_BASE64, lpszBase64, &dwResult))
{
TCHAR pByteLF = *(LPWORD)(lpszBase64 + dwResult -1);
TCHAR pByteCR = *(LPWORD)(lpszBase64 + dwResult -2);
if(pByteCR == 0x0D && pByteLF == 0x0A)
{
*(LPWORD)(lpszBase64 + dwResult -2) = 0;
}
*dwbufSize = dwResult;
return lpszBase64;
}
}
return NULL;
}
DWORD TripleDESdecrypt2(TCHAR *cyphertext,DWORD ctlen,TCHAR *passwd,DWORD pwlen,BYTE *plaintext,DWORD *ptlen)
{
HCRYPTPROV hProv;
HCRYPTHASH hHash;
HCRYPTKEY hKey;
DWORD dwSizeNeeded =0;
BYTE hashData;
BLOBHEADER stBlob;
stBlob.bType = PLAINTEXTKEYBLOB;
stBlob.aiKeyAlg = CALG_3DES;
stBlob.reserved = 0;
stBlob.bVersion = 0x02;
BOOL result = CryptAcquireContext(&hProv,NULL,MS_STRONG_PROV,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
result = CryptHashData(hHash,(BYTE *)passwd,pwlen,0);
result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
BYTE PadMode = CRYPT_MODE_ECB;
result = CryptSetKeyParam(hKey, PKCS5_PADDING, &PadMode, 0);
if(*ptlen >= ctlen)
{
memcpy(plaintext,cyphertext,*ptlen);
result = CryptDecrypt(hKey,hHash,1,0,plaintext,&ctlen);
*ptlen=ctlen;
}
else
{
dwSizeNeeded = ctlen * sizeof(TCHAR);
}
CryptDestroyKey(hKey);
CryptDestroyHash(hHash);
CryptReleaseContext(hProv,0);
return dwSizeNeeded;
}
int main()
{
BYTE* decodedStr;
HCRYPTPROV hCryptProv;
HCRYPTHASH hHash = 0;
HCRYPTKEY hKey = 0;
TCHAR szKey[] = _T("h3bmull3r");
TCHAR szUnencrypted[MAX_PATH * sizeof(TCHAR)] = _T("n7M41mbWvrA=");
TCHAR * pszEncrypted = NULL;
TCHAR * pszBase64 = NULL;
TCHAR * pszBase64Decoded = NULL;
TCHAR * psz3DESDecoded = NULL;
DWORD dwSizeNeeded;
DWORD BufSize;
DWORD dwUnEncryptedSize;
TCHAR * KeyBase64 = NULL;
decodedStr = Base64Decode2(szUnencrypted,_tcslen(szUnencrypted) * sizeof(TCHAR), &BufSize);
TCHAR* NewDecoded = reinterpret_cast<TCHAR*>(decodedStr);
dwUnEncryptedSize = NULL;
TCHAR *decoded = NewDecoded;
dwSizeNeeded = TripleDESdecrypt2(decoded,BufSize,szKey,_tcslen(szKey),decodedStr,&dwUnEncryptedSize);
psz3DESDecoded = new TCHAR[dwSizeNeeded];
TripleDESdecrypt2(decoded,BufSize,szKey,_tcslen(szKey),decodedStr,&dwSizeNeeded);
pszBase64 = Base64Encode2(decodedStr,BufSize,FALSE, &BufSize);
delete [] pszBase64;
delete [] pszBase64Decoded;
delete [] pszEncrypted;
return 0;
}
And by the way i have slightly modified how I encrypt by c#:
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
HashAlgorithm XHash = HashAlgorithm.Create("MD5");
keyArray = XHash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
XHash.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
Please help me
Many thanks,
Jayjay
|
|
|
|
|
Hi
i use the following code for encryption of password
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
Byte[] hashedByte;
UTF8Encoding encoder = new UTF8Encoding();
hashedByte = md5hasher.ComputeHash(encoder.GetBytes(TextBox6.Text));
SqlParameter parmPwd = new SqlParameter("@parmPwd", SqlDbType.Binary, 16);
parmPwd.Value = hashedByte;
cmd.Parameters.Add(parmPwd);
con.Open();
cmd.ExecuteNonQuery();
con.Close()
Please Help me to decrypt it
mail me battan20@gmail.com
|
|
|
|
|
Hi
Were u able to decrypt the code via MD5CryptoServiceProvider?
i also have the same issue..
help!!
|
|
|
|
|
Hi
Sorry, i havn't solved that
Because my task was changed
For which purpose you need it
|
|
|
|
|
mD5 is one way hash, it's not meant to be decrypted
|
|
|
|
|
I don't know MD5 but Why you encode something if you will not be able to decode later??
I put the autor code in my application and it is working....
look:
Create a new class in your application named CryptDecrypt
Delete all code inside the class and put this code (C#):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
namespace System
{
class CryptDecrypt
{
public string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
public string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader =
new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey",typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}
obs: if you want to use a fixed KEY, simple change this line in both functions:
//string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
string key = "123456789012345678901234567890123456789012345678901234567890";
Now, if you need to crypt or decrypt something you could use code like this:
<br />
...<br />
CryptDecrypt CD = new CryptDecrypt();<br />
string Password = CD.Decrypt(Properties.Settings.Default.BD_PWD,true)<br />
CD = null;<br />
...<br />
†GnR† Slash
"Can one man truly make a diference?"
modified on Thursday, January 7, 2010 1:20 PM
|
|
|
|
|
hi i am also working on the same
but it is showing the error 1) "the name properties does not exists in the current context"
no 2 is that when is trying to use "using System.Linq;" it says missing assembly reference
"Error 1 The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?)"
canu please solve it
|
|
|
|
|
humm... I think you don't need Linq in my sample, just delete the line: using System.Linq;
BTW: when you reference Linq, you need to add a reference to System.Xml.Linq (use menu Project, Add Reference and search for Linq)
|
|
|
|
|
You may also be interested in looking at the following, related Code Project articles:
Generic SymmetricAlgorithm Helper[^]
This is a generic helper class that exposes simplified Encrypt and Decrypt functionality for strings, byte arrays and streams for any SymmetricAlgorithm derivative (DES, RC2, Rijndael, TripleDES, etc.).
Making TripleDES Simple in VB.NET and C#[^]
This is a simple wrapper class that provides an easy interface for encrypting and decrypting byte arrays and strings using the 3DES algorithm.
|
|
|
|
|
I'm getting an error in .NET 2.0 when encrypting and NOT using hashing.
The error I get is: "Specified key is not a valid size for this algorithm."
The error occurs when calling: "tdes.Key = keyArray;".
Do you have any idea what could be wrong here?
Am I using an invalid key or is it a bug? The key I use is: "MyVerySecretKey".
|
|
|
|
|
I'm novince to cryptography, and was dealing with encryption/decryption algorithms, I tried a few different implementations for RSA, DES, MD5 algorithms with .net and kept receiving bad data, invalid key length errors. Your sample is great, I used both "use hashing" and "no-hash" options, when I use no hash, the key size now matters again and I receive that good old friendly "invalid key length" error again... Now, I have a better grasp on what I was doing wrong. Thanks a lot...
|
|
|
|
|
Hi,
I used the above mentioned code in VS.net 2003/.net 1.1 framework, and it gives me error -- >
An unhandled exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll
Ny help on this would be highly appreciated!!!
Thanks
alex
|
|
|
|
|