473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Sharing encrypted data with .Net?

I am trying to send some encrypted data from a php application to be
decoded in a .Net application. Both apps encode/decode a given string
but generate different encrypted results. Anyone have any idea? Code
to follow:

php====>
<?php

// Designate string to be encrypted
$string = "This is a test";

// Encryption/decryption key
$key = pack('H*',md5("mysecretkey"));
echo "PHP:KeySize:";
echo mcrypt_get_key_size('tripledes', 'ecb')*8;
echo "<BR>";
echo "PHP:BlockSize:";
echo mcrypt_get_block_size('tripledes', 'ecb')*8;
echo "<BR>";

// Encryption Algorithm
$cipher_alg = MCRYPT_TRIPLEDES;
#$cipher_alg = ;
#$cipher_alg = ;

// Create the initialization vector for added security.
//$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
MCRYPT_MODE_ECB), MCRYPT_RAND);

$iv = pack("H*","ec787813562c5be0");

// Output original string
echo "PHP:Original string:$string <br>";
echo "PHP:Original Key:". base64_encode($key)."<br>";
echo "PHP:Encryption Method: tripledes <br>";
echo "PHP:IV:".base64_encode($iv)."<br>";

// Encrypt $string
//$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string,
MCRYPT_MODE_CBC, $iv);
$encrypted_string = mcrypt_encrypt($cipher_alg, $key, $string,
MCRYPT_MODE_CFB, $iv);
$decrypted_string = mcrypt_decrypt($cipher_alg, $key,
$encrypted_string, MCRYPT_MODE_CFB, $iv);
echo "PHP:Encrypted string
Base64:".base64_encode($encrypted_string)."<br>";
echo "PHP:Decrypted string:$decrypted_string";
?>
<===================
PHP OUTPUTPHP:
KeySize:192
PHP:BlockSize:64
PHP:Original string:This is a test
PHP:Original Key:NNLkJVyOa+s8QvJN/X5tqQ==
PHP:Encryption Method: tripledes
PHP:IV:7Hh4E1YsW+A=
PHP:Encrypted string Base64:A+9SKuoLdZaATqa+qmTipg==
PHP:Decrypted string:This is a test

===========================>
C# (.NET)

using System;
using System.Security.Cryptography;
using System.Text;

namespace test
{
class TryDes{

public string EncryptTripleDES(string Plaintext,
string key) {
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hashMD5 = new
MD5CryptoServiceProvider();

DES.Key =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(k ey));
DES.Mode = CipherMode.CFB;
DES.IV =
Convert.FromBase64String("7Hh4E1YsW+A=");
DES.Padding=PaddingMode.Zeros;

Console.WriteLine("");
Console.WriteLine("NET:KeySize:"+DES.KeySize);

Console.WriteLine("NET:BlockSize:"+DES.BlockSize);
Console.WriteLine("NET:Original
String:"+Plaintext);
Console.WriteLine("NET:Original
Key:"+Convert.ToBase64String(DES.Key));
Console.WriteLine("NET:Encryption
Method:TripeDES");

//DES.Mode = CipherMode.ECB;
Console.WriteLine(
"NET:IV:"+Convert.ToBase64String(DES.IV));

ICryptoTransform DESEncrypt =
DES.CreateEncryptor(DES.Key,DES.IV);
byte[] Buffer =
ASCIIEncoding.ASCII.GetBytes(Plaintext);
return
Convert.ToBase64String(DESEncrypt.TransformFinalBl ock(Buffer, 0,
Buffer.Length));
}

public string DecryptTripleDES(string base64Text,
string key){
TripleDESCryptoServiceProvider DES = new
TripleDESCryptoServiceProvider();

MD5CryptoServiceProvider hashMD5 = new
MD5CryptoServiceProvider();

DES.Key =
hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(k ey));
DES.Mode = CipherMode.CFB;
DES.Padding=PaddingMode.Zeros;
DES.IV = Convert.FromBase64String("7Hh4E1YsW+A=");

ICryptoTransform DESDecrypt =
DES.CreateDecryptor(DES.Key,DES.IV);
byte[] Buffer =
Convert.FromBase64String(base64Text);
return
ASCIIEncoding.ASCII.GetString(DESDecrypt.Transform FinalBlock(Buffer,
0, Buffer.Length));
}
}

class Class1
{
static void Main(string[] args) {
TryDes md = new TryDes();
string secrekey = "mysecretkey";
string sometest = md.EncryptTripleDES("This is
a test",secrekey);
Console.WriteLine("NET:Encrypted string
Base64:"+sometest);
Console.WriteLine("NET:Decrypted
string:"+md.DecryptTripleDES(sometest,secrekey));
Console.WriteLine("");
}
}
}
<======================
..Net Output

NET:KeySize:192
NET:BlockSize:64
NET:Original String:This is a test
NET:Original Key:NNLkJVyOa+s8QvJN/X5tqQ==
NET:Encryption Method:TripeDES
NET:IV:7Hh4E1YsW+A=
NET:Encrypted string Base64:qbGgiTZp9YTGOutg8IGlqw==
NET:Decrypted string:This is a test
Nov 16 '05 #1
3 4451
Todd Gruben <tg*****@flightlock.com> wrote:
I am trying to send some encrypted data from a php application to be
decoded in a .Net application. Both apps encode/decode a given string
but generate different encrypted results. Anyone have any idea?


Well for a start, you need to know how mcrypt_encrypt encodes a string.
Pretty much every encryption scheme works with bytes rather than text -
and to get from text to bytes, you need to specify an encoding. You're
using ASCII in the .NET version - what does PHP use?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2


The string format of php is a array of characters. The
binary form of both strings in php and csharp appear to be
the same. I did a dump of the input string and its is
exactly the same

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Todd Gruben <tg*****@flightlock.com> wrote:
The string format of php is a array of characters. The
binary form of both strings in php and csharp appear to be
the same. I did a dump of the input string and its is
exactly the same


I don't think you understand. In order to encrypt data, you need to
convert from text to binary. That doesn't mean how they're stored
internally, it means converting from an array of characters to an array
of bytes, which could be in any one of many encodings. If you use a
different encoding, you will get different results.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: sffan | last post by:
I am new to database programming and was curious how others solve the problem of storing encrypted in data in db table columns and then subsequently searching for these records. The particular...
6
by: varlagas | last post by:
We disabled the antivirus software but the problem persists. Any clues? Many thanks in advance! Panagiotis Varlagas ======================================================================= ...
5
by: Nico | last post by:
My database have 20 tables and many users. I wish to store encrypted data in 3 tables and have only 3 users have access to them, walking into tables or using forms. Can someone point me a direct...
4
by: Burke Atilla | last post by:
While encrypting data with DES through CryptoStream makes encrypted data bigger than original string. if we have 8 byte key and 8 byte of data then the mode is ECB. output encrypted data is 16 bytes...
2
by: Leonardo D'Ippolito | last post by:
Hi! I have two .NET win apps that need to communicate on a TCP/IP network. 'App A' must ask 'app B' if it's allowed to do some task, and 'app B' must authorize or prohibit it. How can I do...
8
by: robert | last post by:
Hello, I want to put (incrementally) changed/new files from a big file tree "directly,compressed and password-only-encrypted" to a remote backup server incrementally via FTP,SFTP or DAV.... At...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
2
by: Bernard Dhooghe | last post by:
The information center writes: "Encryption Algorithm: The internal encryption algorithm used is RC2 block cipher with padding, the 128-bit secret key is derived from the password using a MD2...
0
by: danishce | last post by:
I want to generate 8 byte key using CBC MAC by applying encryption to whole message in vb.net.My code is: //Main form Code Imports System.Security.Cryptography Dim plainText As String ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.