You are not logged in.
Pages: 1
- Forum Index
- » .NET Programming
- » How do you decrypt the hex string from the TripleDes example?
#1 17 May 2010 8:42 am
- chrisbloom7
- New Member
- Registered: May 2010
- Posts: 3
How do you decrypt the hex string from the TripleDes example?
I have been using your example at http://sanity-free.org/131/triple_des_b … sharp.html as the basis for my own TripleDES decryption scheme between a .NET client and a PHP application. I have gotten to the point where the encryption works in both .NET and PHP, and I can decrypt in PHP, but I haven't been able to figure out how to decrypt using C#. Basically I need to know what the reverse function would be for the supplied bin2hex function so that I can get the transmitted hexadecimal string back to binary to run through the decryptor. Can anyone provide some feedback?
Thanks
Offline
#2 17 May 2010 9:54 am
- chrisbloom7
- New Member
- Registered: May 2010
- Posts: 3
Re: How do you decrypt the hex string from the TripleDes example?
Well, I think I've hacked my way out of the box. I'm not a .NET programmer, so this may be clunky, but here's what I've come up with. (Note the code is from my test script inside Snippet Compiler and I have no idea how one would make it run outside of that program. I merely needed to show proof of concept.)
Code:
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
public class MyClass
{
public static void RunSnippet()
{
// ENCRYPTING
string result = SimpleTripleDes("Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.");
WL(result);
WL("");
// DECRYPTING
result = SimpleTripleDesDecrypt("51196a80db5c51b8523220383de600fd116a947e00500d6b9101ed820d29f198c705000791c07ecc1e090213c688a4c7a421eae9c534b5eff91794ee079b15ecb862a22581c246e15333179302a7664d4be2e2384dc49dace30eba36546793be");
WL(result);
// PAUSE
RL();
}
static string SimpleTripleDes(string Data) {
byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
byte[] iv = Encoding.ASCII.GetBytes("password");
byte[] data = Encoding.ASCII.GetBytes(Data);
byte[] enc = new byte[0];
TripleDES tdes = TripleDES.Create();
tdes.IV = iv;
tdes.Key = key;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform ict = tdes.CreateEncryptor();
enc = ict.TransformFinalBlock(data, 0, data.Length);
return ByteArrayToString(enc);
}
static string SimpleTripleDesDecrypt(string Data) {
byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
byte[] iv = Encoding.ASCII.GetBytes("password");
byte[] data = StringToByteArray(Data);
byte[] enc = new byte[0];
TripleDES tdes = TripleDES.Create();
tdes.IV = iv;
tdes.Key = key;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform ict = tdes.CreateDecryptor();
enc = ict.TransformFinalBlock(data, 0, data.Length);
return Encoding.ASCII.GetString(enc);
}
public static string ByteArrayToString(byte[] ba) {
string hex = BitConverter.ToString(ba);
return hex.Replace("-","");
}
public static byte[] StringToByteArray(String hex) {
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}Offline
#3 17 May 2010 10:04 am
- chrisbloom7
- New Member
- Registered: May 2010
- Posts: 3
Re: How do you decrypt the hex string from the TripleDes example?
Here is the accompanying PHP code.
Code:
<?php
// very simple ASCII key and IV
$key = "passwordDR0wSS@P6660juht";
$iv = "password";
$cipher = mcrypt_module_open(MCRYPT_3DES, '', 'cbc', '');
// ENCRYPTING
printvar(
SimpleTripleDes('Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.'),
'Encrypted:'
);
// DECRYPTING
printvar(
SimpleTripleDesDecrypt('51196a80db5c51b8523220383de600fd116a947e00500d6b9101ed820d29f198c705000791c07ecc1e090213c688a4c7a421eae9c534b5eff91794ee079b15ecb862a22581c246e15333179302a7664d4be2e2384dc49dace30eba36546793be'),
'Decrypted:'
);
function SimpleTripleDes($buffer) {
global $key, $iv, $cipher;
printvar($buffer, 'Encrypting:');
// get the amount of bytes to pad
$extra = 8 - (strlen($buffer) % 8);
//printvar($extra, 'Padding with n zeros');
// add the zero padding
if($extra > 0) {
for($i = 0; $i < $extra; $i++) {
$buffer .= "\0";
}
}
mcrypt_generic_init($cipher, $key, $iv);
$result = bin2hex(mcrypt_generic($cipher, $buffer));
mcrypt_generic_deinit($cipher);
return $result;
}
function SimpleTripleDesDecrypt($buffer) {
global $key, $iv, $cipher;
printvar($buffer, 'Decrypting:');
mcrypt_generic_init($cipher, $key, $iv);
$result = rtrim(mdecrypt_generic($cipher, hex2bin($buffer)), "\0");
mcrypt_generic_deinit($cipher);
return $result;
}
function hex2bin($data)
{
$len = strlen($data);
return pack("H" . $len, $data);
}
// HELPER FUNCTIONS
function printvar($var, $label="") {
print "<pre style=\"border: 1px solid #999; background-color: #f7f7f7; color: #000; overflow: auto; width: auto; text-align: left; padding: 1em;\">" .
(
(
strlen(
trim($label)
)
) ? htmlentities($label)."\n===================\n" : ""
) .
htmlentities(print_r($var, TRUE)) . "</pre>";
}
?>Offline
#4 17 May 2010 4:48 pm
Re: How do you decrypt the hex string from the TripleDes example?
Looks like you've nailed it.
The only thing I'd suggest is instead of using Encoding.ASCII use Encoding.UTF8 to encode / decode the original string (in C#). That way you're able to encode / decode a much larger string set than just ascii.
Offline
Pages: 1
- Forum Index
- » .NET Programming
- » How do you decrypt the hex string from the TripleDes example?
