473,789 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TripleDES

1 New Member
Hi,

I tried encrypting and decrypting a string using the following Java code and key. And was successful.

Then, I used the following C# to encrypt and decrypt a string using the following C# code and key. And was successful.

But when i tried to encrypted using java and decrypted using C#, but it was not successful.

Can someone please help? THanks in advanced.


[JAVA code]
private static String algorithm = "DESede";
private static Key key = null;
private static Cipher cipher = null;

private static void setUp() throws Exception {
key = new SecretKeySpec(" 123456789012345 678901234".getB ytes(), algorithm);
cipher = Cipher.getInsta nce(algorithm);
}

public static void main(String[] args) throws Exception {
setUp();
byte[] encryptionBytes = null;
encryptionBytes = encrypt("This is a testing");
System.out.prin tln("Recovered: " + decrypt(encrypt ionBytes));
}

private static byte[] encrypt(String input) throws InvalidKeyExcep tion,
BadPaddingExcep tion, IllegalBlockSiz eException {
cipher.init(Cip her.ENCRYPT_MOD E, key);
byte[] inputBytes = input.getBytes( );
return cipher.doFinal( inputBytes);
}

private static String decrypt(byte[] encryptionBytes ) throws
InvalidKeyExcep tion, BadPaddingExcep tion, IllegalBlockSiz eException {
cipher.init(Cip her.DECRYPT_MOD E, key);
byte[] recoveredBytes = cipher.doFinal( encryptionBytes );
String recovered = new String(recovere dBytes);
return recovered;
}

[C# Code]
static void Main()
{
try
{
TripleDESCrypto ServiceProvider tDESalg = new
TripleDESCrypto ServiceProvider ();

// Create a string to encrypt.
string sData = "This is a testing";

// Encrypt the string to an in-memory buffer.
byte[] key = Encoding.UTF8.G etBytes("123456 789012345678901 234");
byte[] Data = EncryptTextToMe mory(sData, key, key);

string Final = DecryptTextFrom Memory(Data, key, key);
// Display the decrypted string to the console.
Console.WriteLi ne(Final);
}
catch (Exception e)
{
Console.WriteLi ne(e.Message);
}
}

public static byte[] EncryptTextToMe mory(string Data, byte[] Key, byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mS tream,
new TripleDESCrypto ServiceProvider ().CreateEncryp tor(Key, IV),
CryptoStreamMod e.Write);

// Convert the passed string to a byte array.
byte[] toEncrypt = Encoding.UTF8.G etBytes(Data);

// Write the byte array to the crypto stream and flush it.
cStream.Write(t oEncrypt, 0, toEncrypt.Lengt h);
cStream.FlushFi nalBlock();

// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray ();

// Close the streams.
cStream.Close() ;
mStream.Close() ;

// Return the encrypted buffer.
return ret;
}
catch (CryptographicE xception e)
{
Console.WriteLi ne("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}

public static string DecryptTextFrom Memory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Da ta);

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(ms Decrypt,
new TripleDESCrypto ServiceProvider ().CreateDecryp tor(Key, IV),
CryptoStreamMod e.Read);

// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];

// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);

//Convert the buffer into a string and return it.
return new ASCIIEncoding() .GetString(from Encrypt);
}
catch (CryptographicE xception e)
{
Console.WriteLi ne("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
Oct 24 '06 #1
0 3298

Sign in to post your reply or Sign up for a free account.

Similar topics

5
2852
by: c duden | last post by:
I am attempting to encrypt some text and be able to decrypt it at a later time. I have two methods to do this: public static Byte EncryptText(string textToEncrypt, string encryptionHash) { Byte bytearrayinput = StringAndByteManipulation.ConvertStringToByteArray(textToEncrypt); //DES instance System.Security.Cryptography.TripleDESCryptoServiceProvider des = new
0
3090
by: Jonas | last post by:
I have the following perl program witch i use to encrypt a password file with. In perl 5.6 this program works like a charm but when trying it on the RED HAT EL 3 platform (taroon) is doesnt decrypt the encrypted string right. Program use Crypt::TripleDES; sub generate() {
8
13543
by: wkodie | last post by:
I'm having trouble encrypting/decrypting a simple string using the System.Security.Cryptography.TripleDESCryptoServiceProvider, etc... The encryption works, but the decryption does not properly decrypt several of the first few characters. Here's the code: class TMyCipher {
7
17880
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm able to encrypt and then decrypt data okay as in the following code: // encrypt the data // Encryptor enc = new Encryptor(EncryptionAlgorithm.TripleDes); byte key = Encoding.ASCII.GetBytes("0123456789012345");
2
30546
by: GRB | last post by:
A client wants me to decrypt a cookie. They encrypted the data in java using TripleDES. The key provided is a 168 bit 44 character key, DESede alogorithm. Ive tried to decrypt in vb.net and get the above error. Below is the class I use and the code I use to call it. Not sure wher I'm going wrong here. The TripleDES service provider only uses a 192 bit 24 character length key. Any help would be greatly appreciated.
1
2097
by: Celia | last post by:
I am working with a web service which requires enryption of some XML fields. It will be symmetric encryption - TripleDES. The vendor has given me the Key that we will share. With the exception that there are 48 characters in the actual Key that he gave me, here is an example of what he gave me: 0E329232EA6D0D73 (The vendor has told me to use all zeros for the IV.) How can I use this with the .Key property of...
0
2174
by: qfmomen | last post by:
I have encrypted password in db added by java application using tripleDES. I need to build same class but it is giving me different result. This class i have used: JavaProject: Making TripleDES Simple in Visual Basic .NET. Free source code and programming help Any help? Qurban
0
9665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6768
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.