Ho 2 funzioni in apparenza apparentemente identiche, una in VB.NET e una in C#
partendo dalla stessa stringa "cryptata" e la stessa chiave mi danno
risultati diversi...
Quella in VB.NET mi restituisce la stringa decriptata corretta,
quella in c# della sporcizia
Vorrei utilizzre nel mio progetto sono sorgente C#, ma non riesco a capire dove sta' l'errore !!!
Chi mi aiuta ?
// TEST delle 2 versioni
protected void Page_Load(object sender, EventArgs e)
{
string sText = "37591888F82AE4E0";
string sVbText = VbCryptoApi.DecryptHex("ROSSI", sText);
string sCsText = CsCryptoApi.DecryptHex("ROSSI", sText);
}
******** file VbCryptoApi.vb
Imports Microsoft.VisualBasic
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class VbCryptoApi
Public Shared Function ByteFromHex(ByVal sText As String) As Byte()
Dim lCount As Long
Dim btText() As Byte
Dim iKon As Integer
ReDim btText((sText.Length * 0.5) - 1)
iKon = 0
For lCount = 1 To sText.Length Step 2
btText(iKon) = CByte(Val("&H" & Mid(sText, lCount, 2)))
iKon = iKon + 1
Next
Return btText
End Function
Public Shared Function DecryptHex(ByVal sPwd As String, ByVal sHexText As String) As String
Dim arData() As Byte
arData = ByteFromHex(sHexText)
Dim cspParams As CspParameters = New CspParameters(1, "Microsoft Base Cryptographic Provider v1.0")
Dim deriveBytes As PasswordDeriveBytes = New PasswordDeriveBytes(sPwd, Nothing, "SHA-1", 1, cspParams)
Dim rgbIV(7) As Byte
Dim key() As Byte = deriveBytes.CryptDeriveKey("RC2", "SHA1", 0, rgbIV)
Dim provider As RC2CryptoServiceProvider = New RC2CryptoServiceProvider
'
provider.Key = key
provider.IV = rgbIV
'
Dim transform As ICryptoTransform = provider.CreateDecryptor()
Dim decyptedBlob() As Byte = transform.TransformFinalBlock(arData, 0, arData.Length)
Dim encryptedString As String = Encoding.ASCII.GetString(decyptedBlob)
Return encryptedString
End Function
End Class
******** file CsCryptoApi.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text;
/// <summary>
/// Summary description for CsCryptoApi
/// </summary>
public class CsCryptoApi
{
private static byte[] BytesFromHex(string sHexText)
{
byte[] bResult = new byte[(int)(sHexText.Length * 0.5)];
int iKon = 0;
for (int i = 0; i <= sHexText.Length - 1; i = i + 2)
{
bResult[iKon] = (byte)Convert.ToInt32("0x" + sHexText.Substring(i, 2), 16);
iKon++;
}
return bResult;
}
public static string DecryptHex(string sPwd, string sHexText)
{
string sReturn = "";
byte[] arData = BytesFromHex(sHexText);
// creates the CspParameters object and sets the key container name used to store the RSA key pair
CspParameters cspParams = new CspParameters(1, "Microsoft Base Cryptographic Provider v1.0");
PasswordDeriveBytes deriveBytes = new PasswordDeriveBytes(sPwd, null, "SHA-1", 1, cspParams);
// generate an RC2 key
byte[] rgbIV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] key = deriveBytes.CryptDeriveKey("RC2", "SHA1", 0, rgbIV);
// setup an RC2 object to encrypt with the derived key
RC2CryptoServiceProvider provider = new RC2CryptoServiceProvider();
provider.Key = key;
provider.IV = rgbIV; // new byte[] { 21, 22, 23, 24, 25, 26, 27, 28 };
using (ICryptoTransform transform = provider.CreateEncryptor())
{
byte[] decyptedBlob = transform.TransformFinalBlock(arData, 0, arData.Length);
sReturn = Encoding.ASCII.GetString(decyptedBlob);
}
return sReturn;
}
}