Click here to Skip to main content
15,918,007 members
Home / Discussions / C#
   

C#

 
AnswerRe: Precompiled or runtime eval? Pin
Guffa2-Oct-06 8:49
Guffa2-Oct-06 8:49 
GeneralRe: Precompiled or runtime eval? Pin
spin vector2-Oct-06 10:27
spin vector2-Oct-06 10:27 
Questionicodecompiler Pin
waheed awan2-Oct-06 7:00
waheed awan2-Oct-06 7:00 
AnswerRe: icodecompiler [modified] Pin
samtam2-Oct-06 22:21
samtam2-Oct-06 22:21 
QuestionP2P messaging and file sharing Pin
asamay2-Oct-06 6:53
asamay2-Oct-06 6:53 
AnswerRe: P2P messaging and file sharing Pin
User 66582-Oct-06 7:03
User 66582-Oct-06 7:03 
GeneralRe: P2P messaging and file sharing Pin
asamay2-Oct-06 7:36
asamay2-Oct-06 7:36 
QuestionBad Data during 3DES ECB Decryption Pin
naleh332-Oct-06 6:43
naleh332-Oct-06 6:43 
I am trying to encrypt and decrypt using the functions below. Encryption is successful but decryption will return Bad Data. The Utils.byteToHexStr is a function to convert a hexadecimal string into a byte array where by each two characters of the hexadecimal string are combined to create one byte. (eg. 0x31 0x32 0x33 0x34 (ASCII 1234) -> 0x12 0x34). This is the requirement of the inputdata for encryption and decryption.

crypto.DesEncrypt("1234567812345678", "123456789012345678901234567890123456789012345678", "0000000000000000");

- > the code above got 0x2D 0xC8 0x88 0xCA 0x7D 0x62 0x17 0xE7 as result. However, to restore the original value using the code below, the program complain "Bad Data". I have tried all sorts of ways but still to no avail. Please provide me some advices.

crypto.DesDecrypt("2DC888CA7D6217E7", "123456789012345678901234567890123456789012345678", "0000000000000000");




public static byte[] DesEncrypt(string stringToEncrypt, string szKey, string szIV)
{
byte[] data = Utils.byteToHexStr(stringToEncrypt);
byte[] m_bDESKey = Utils.byteToHexStr(szKey);
byte[] m_bDESIV = Utils.byteToHexStr(szIV);

TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
MemoryStream ms = new MemoryStream(4096);
CryptoStream encStream = new CryptoStream(ms,des.CreateEncryptor(m_bDESKey, m_bDESIV),CryptoStreamMode.Write);

encStream.Write(data,0,data.Length);
//encStream.FlushFinalBlock();

//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
encStream.Close();
return bResult;
}

public static string DesDecrypt(string stringToDeCrypt, string szKey, string szIV)
{
byte[] data = Utils.byteToHexStr(stringToDeCrypt);
byte[] m_bDESKey = Utils.byteToHexStr(szKey);
byte[] m_bDESIV = Utils.byteToHexStr(szIV);


TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
MemoryStream ms = new MemoryStream(4096);
CryptoStream encStream = new CryptoStream(ms,des.CreateDecryptor(m_bDESKey, m_bDESIV),CryptoStreamMode.Read);
ms.Write(data,0,data.Length);

ms.Position = 0;
string strResult = new StreamReader(encStream).ReadToEnd();
encStream.Close();
return strResult;

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static byte[] byteToHexStr(string szHexString)
{
int discarded;
byte[] byteArray = HexEncoding.GetBytes(szHexString, out discarded);
if (discarded > 0)
{
return null;
}
return byteArray;
}

public static byte[] GetBytes(string hexString, out int discarded)
{
discarded = 0;
string newString = "";
char c;
// remove all none A-F, 0-9, characters
for (int i=0; i<hexstring.length; i++)
="" {
="" c="hexString[i];
" if="" (ishexdigit(c))
="" newstring="" +="c;
" else
="" discarded++;
="" }
="" odd="" number="" of="" characters,="" discard="" last="" character
="" (newstring.length="" %="" 2="" !="0)
" newstring.length-1);
="" }

="" int="" bytelength="newString.Length" 2;
="" byte[]="" bytes="new" byte[bytelength];
="" string="" hex;
="" j="0;
" for="" (int="" i="0;" i<bytes.length;="" hex="new" string(new="" char[]="" {newstring[j],="" newstring[j+1]});
="" bytes[i]="HexToByte(hex);
" return="" bytes;
="" public="" static="" tostring(byte[]="" bytes)
="" hexstring="" ;
="" hexstring;
="" <summary="">
/// Determines if given string is in proper hexadecimal string format
///
/// <param name="hexString" />
/// <returns>
public static bool InHexFormat(string hexString)
{
bool hexFormat = true;

foreach (char digit in hexString)
{
if (!IsHexDigit(digit))
{
hexFormat = false;
break;
}
}
return hexFormat;
}

///
/// Returns true is c is a hexadecimal digit (A-F, a-f, 0-9)
///

/// <param name="c" />Character to test
/// <returns>true if hex digit, false if not
public static bool IsHexDigit(Char c)
{
int numChar;
int numA = Convert.ToInt32('A');
int num1 = Convert.ToInt32('0');
c = Char.ToUpper(c);
numChar = Convert.ToInt32(c);
if (numChar >= numA && numChar < (numA + 6))
return true;
if (numChar >= num1 && numChar < (num1 + 10))
return true;
return false;
}
///
/// Converts 1 or 2 character string into equivalant byte value
///

/// <param name="hex" />1 or 2 character string
/// <returns>byte
private static byte HexToByte(string hex)
{
if (hex.Length > 2 || hex.Length <= 0)
throw new ArgumentException("hex must be 1 or 2 characters in length");
byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return newByte;
}
Questiontransfer area access Pin
gus_br2-Oct-06 6:06
gus_br2-Oct-06 6:06 
AnswerRe: transfer area access Pin
LongRange.Shooter3-Oct-06 7:48
LongRange.Shooter3-Oct-06 7:48 
QuestionNDoc: dead or alive? Pin
spin vector2-Oct-06 5:45
spin vector2-Oct-06 5:45 
AnswerRe: NDoc: dead or alive? Pin
Judah Gabriel Himango2-Oct-06 5:52
sponsorJudah Gabriel Himango2-Oct-06 5:52 
GeneralRe: NDoc: dead or alive? Pin
Jon Sagara2-Oct-06 6:17
Jon Sagara2-Oct-06 6:17 
GeneralRe: NDoc: dead or alive? [modified] Pin
spin vector2-Oct-06 7:08
spin vector2-Oct-06 7:08 
GeneralRe: NDoc: dead or alive? Pin
Stefan Troschuetz2-Oct-06 7:24
Stefan Troschuetz2-Oct-06 7:24 
GeneralRe: NDoc: dead or alive? Pin
spin vector2-Oct-06 7:25
spin vector2-Oct-06 7:25 
QuestionFull screen form - disabling the Task Bar Pin
srev2-Oct-06 5:45
srev2-Oct-06 5:45 
AnswerRe: Full screen form - disabling the Task Bar Pin
Judah Gabriel Himango2-Oct-06 5:53
sponsorJudah Gabriel Himango2-Oct-06 5:53 
GeneralRe: Full screen form - disabling the Task Bar Pin
srev2-Oct-06 5:59
srev2-Oct-06 5:59 
GeneralRe: Full screen form - disabling the Task Bar Pin
eggie52-Oct-06 8:50
eggie52-Oct-06 8:50 
QuestionRetrieving Stored Proecedure return columns Pin
Jamie Nordmeyer2-Oct-06 5:34
Jamie Nordmeyer2-Oct-06 5:34 
AnswerRe: Retrieving Stored Proecedure return columns Pin
Judah Gabriel Himango2-Oct-06 6:05
sponsorJudah Gabriel Himango2-Oct-06 6:05 
GeneralRe: Retrieving Stored Proecedure return columns Pin
Jamie Nordmeyer2-Oct-06 7:06
Jamie Nordmeyer2-Oct-06 7:06 
QuestionOnFocus Method Issues Pin
Sam Heller2-Oct-06 5:19
Sam Heller2-Oct-06 5:19 
QuestionAPI hooking Pin
bayfouinc2-Oct-06 5:12
bayfouinc2-Oct-06 5:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.