Please help me to convert this java code to csharp ,
the problem is in the hexStringToByteArray method, in java it's working well but in Csharp it's not giving me the same result ,in java it returns byte[] (signed byte) where in csharp the same method returns array[byte] (unsigned byte), i can convert it to sbyte[] to get the same result but than i cannot assign it to the
RijndaelManaged object
aesAlg.Key = Key; //byte[]
aesAlg.IV = IV; ////byte[]
where it takes only an array of byte (byte[]) what's the solution for this problem ?
java code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class WhatsAppDecrypt5 {
private static final byte[] INITIALIZATION_VECTOR = hexStringToByteArray("1e39f369e90db33aa73b442bbbb6b0b9");
private static final byte[] ENCRYPTION_KEY = hexStringToByteArray("8d4b155cc9ff81e5cbf6fa7819366a3ec621a656416cd793");
public static void main(String[] args) throws Exception {
decrypt(new File("d:\\msgstore-2014-04-03.1.db.crypt5"), new File("d:\\tt.db"), "has.cha.89@gmail.com");
}
private static void decrypt(File inputFile, File outputFile, String email) throws Exception {
String emailMD5 = md5(email);
byte[] emailMD5Bytes = hexStringToByteArray(emailMD5 + emailMD5);
byte[] decryptionKey = Arrays.copyOf(ENCRYPTION_KEY, 24);
for (int i = 0; i < 24; i++) {
decryptionKey[i] ^= emailMD5Bytes[i & 0xF];
}
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptionKey, "AES"), new IvParameterSpec(
INITIALIZATION_VECTOR));
CipherInputStream cIn = new CipherInputStream(new FileInputStream(inputFile), cipher);
FileOutputStream fOut = new FileOutputStream(outputFile);
byte[] buffer = new byte[8192];
int n;
while ((n = cIn.read(buffer)) != -1) {
fOut.write(buffer, 0, n);
}
cIn.close();
fOut.close();
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
private static String md5(String md5) throws Exception {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(md5.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
return bigInt.toString(16);
}
}
csharp code
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace AESDec
{
public class WhatsappDecrypt
{
static BinaryReader mIn;
static BinaryWriter mOut;
string emailAddress;
byte[] Key = new byte[] { 141, 75, 21, 92, 201, 255, 129, 229, 203, 246, 250, 120, 25, 54, 106, 62, 198, 33, 166, 86, 65, 108, 215, 147 };
byte[] IV = new byte[] { 0x1E, 0x39, 0xF3, 0x69, 0xE9, 0xD, 0xB3, 0x3A, 0xA7, 0x3B, 0x44, 0x2B, 0xBB, 0xB6, 0xB0, 0xB9 };
public static void Main()
{
new WhatsappDecrypt("d:\\msgstore-2014-04-03.1.db.crypt5", "d:\\db.db", "has.cha.89@gmail.com").Decrypt();
}
public WhatsappDecrypt(string inputFile, string outputFile, string emailAddress)
{
try
{
this.emailAddress = emailAddress;
mIn = new BinaryReader(new FileStream(inputFile, FileMode.Open, FileAccess.Read));
if (File.Exists(outputFile))
File.Delete(outputFile);
mOut = new BinaryWriter(new FileStream(outputFile, FileMode.CreateNew, FileAccess.Write));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void Decrypt()
{
CalculateDecryptKeyBasedOnEmailAccount();
Decrypt(mIn.ReadBytes((int)mIn.BaseStream.Length));
}
private void CalculateDecryptKeyBasedOnEmailAccount()
{
String emailMD5 = CalculateMD5Hash(emailAddress);
byte[] emailMD5Bytes = hexStringToByteArray(emailMD5 + emailMD5);
byte[] decryptionKey = Key.Take(24).ToArray();
for (int i = 0; i < 24; i++)
{
decryptionKey[i] ^= emailMD5Bytes[i & 0xF];
}
Key = decryptionKey;
}
private void Decrypt(byte[] cipherText)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (RijndaelManaged aesAlg = new RijndaelManaged ())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
mOut.Write(srDecrypt.ReadToEnd());
}
}
}
}
mOut.Flush();
mOut.Close();
}
public byte[] hexStringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
}
Please help me to convert this java code to csharp ,
the problem is in the hexStringToByteArray method, in java it's working well but in Csharp it's not giving me the same result ,in java it returns byte[] (signed byte) where in csharp the same method returns array[byte] (unsigned byte), i can convert it to sbyte[] to get the same result but than i cannot assign it to the
RijndaelManaged object
aesAlg.Key = Key; //byte[]
aesAlg.IV = IV; ////byte[]
where it takes only an array of byte (byte[]) what's the solution for this problem ?
java code
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class WhatsAppDecrypt5 {
private static final byte[] INITIALIZATION_VECTOR = hexStringToByteArray("1e39f369e90db33aa73b442bbbb6b0b9");
private static final byte[] ENCRYPTION_KEY = hexStringToByteArray("8d4b155cc9ff81e5cbf6fa7819366a3ec621a656416cd793");
public static void main(String[] args) throws Exception {
decrypt(new File("d:\\msgstore-2014-04-03.1.db.crypt5"), new File("d:\\tt.db"), "has.cha.89@gmail.com");
}
private static void decrypt(File inputFile, File outputFile, String email) throws Exception {
String emailMD5 = md5(email);
byte[] emailMD5Bytes = hexStringToByteArray(emailMD5 + emailMD5);
byte[] decryptionKey = Arrays.copyOf(ENCRYPTION_KEY, 24);
for (int i = 0; i < 24; i++) {
decryptionKey[i] ^= emailMD5Bytes[i & 0xF];
}
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptionKey, "AES"), new IvParameterSpec(
INITIALIZATION_VECTOR));
CipherInputStream cIn = new CipherInputStream(new FileInputStream(inputFile), cipher);
FileOutputStream fOut = new FileOutputStream(outputFile);
byte[] buffer = new byte[8192];
int n;
while ((n = cIn.read(buffer)) != -1) {
fOut.write(buffer, 0, n);
}
cIn.close();
fOut.close();
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
}
return data;
}
private static String md5(String md5) throws Exception {
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(md5.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1, digest);
return bigInt.toString(16);
}
}
csharp code
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace AESDec
{
public class WhatsappDecrypt
{
static BinaryReader mIn;
static BinaryWriter mOut;
string emailAddress;
byte[] Key = new byte[] { 141, 75, 21, 92, 201, 255, 129, 229, 203, 246, 250, 120, 25, 54, 106, 62, 198, 33, 166, 86, 65, 108, 215, 147 };
byte[] IV = new byte[] { 0x1E, 0x39, 0xF3, 0x69, 0xE9, 0xD, 0xB3, 0x3A, 0xA7, 0x3B, 0x44, 0x2B, 0xBB, 0xB6, 0xB0, 0xB9 };
public static void Main()
{
new WhatsappDecrypt("d:\\msgstore-2014-04-03.1.db.crypt5", "d:\\db.db", "has.cha.89@gmail.com").Decrypt();
}
public WhatsappDecrypt(string inputFile, string outputFile, string emailAddress)
{
try
{
this.emailAddress = emailAddress;
mIn = new BinaryReader(new FileStream(inputFile, FileMode.Open, FileAccess.Read));
if (File.Exists(outputFile))
File.Delete(outputFile);
mOut = new BinaryWriter(new FileStream(outputFile, FileMode.CreateNew, FileAccess.Write));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void Decrypt()
{
CalculateDecryptKeyBasedOnEmailAccount();
Decrypt(mIn.ReadBytes((int)mIn.BaseStream.Length));
}
private void CalculateDecryptKeyBasedOnEmailAccount()
{
String emailMD5 = CalculateMD5Hash(emailAddress);
byte[] emailMD5Bytes = hexStringToByteArray(emailMD5 + emailMD5);
byte[] decryptionKey = Key.Take(24).ToArray();
for (int i = 0; i < 24; i++)
{
decryptionKey[i] ^= emailMD5Bytes[i & 0xF];
}
Key = decryptionKey;
}
private void Decrypt(byte[] cipherText)
{
// Check arguments.
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
// Declare the string used to hold
// the decrypted text.
string plaintext = null;
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (RijndaelManaged aesAlg = new RijndaelManaged ())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
mOut.Write(srDecrypt.ReadToEnd());
}
}
}
}
mOut.Flush();
mOut.Close();
}
public byte[] hexStringToByteArray(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static string CalculateMD5Hash(string input)
{
// step 1, calculate MD5 hash from input
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hash = md5.ComputeHash(inputBytes);
// step 2, convert byte array to hex string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
sb.Append(hash[i].ToString("X2"));
}
return sb.ToString();
}
}
}
0 commentaires:
Enregistrer un commentaire