66 messaggi dal 08 febbraio 2007
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;
}
}
652 messaggi dal 12 maggio 2001
www.idioteca.it
Ho dato un'occhiata veloce ed ho provato a fare una conversione secca senza fare ulteriori verifiche tramite un tool online abbastanza affidabile (http://converter.telerik.com/Default.aspx) e mi sembra di notare delle differenze tra le due versioni (Vb e c#)
Questo adesempio è un caso di differenze nel for della prima funzione (ByteFromHex)

    for (lCount = 1; lCount <= sText.Length; lCount += 2) {
      btText(iKon) = Convert.ToByte(Conversion.Val("&H" + Strings.Mid(sText, lCount, 2)));
      iKon = iKon + 1;
    }
    return btText;

e questa è la tua versione
        for (int i = 0; i <= sHexText.Length - 1; i = i + 2)
        {
            bResult[iKon] = (byte)Convert.ToInt32("0x" + sHexText.Substring(i, 2), 16);
            iKon++;
        }


verifica che le due versioni siano davvero uguali
ciao
66 messaggi dal 08 febbraio 2007
In debug in arData ottengo un array di byte con valori uguale
in entrambe le versioni.

La riga dove sta il problema e questa :


byte[] decyptedBlob = transform.TransformFinalBlock(arData, 0, arData.Length);


dove in vb.net decyptedBlob e' un array di byte uguale a :
[112][105][112][112][111] = "pippo" OK

mentre in C#
[102][0][12][16][84][232][178][105][102][49][120][200][239][223][196][29]

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.
Community
Ultimi messaggi
UTENTI ONLINE
In primo piano

I più letti di oggi

Media
In evidenza
MISC