473,753 Members | 7,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TripleDES Encryption issue

jdn
I'm new to using this part of the framework, so I'm hoping I've done
something obviously stupid, which someone will be able to point out in
an obvious manner.

Most of the samples I've seen involved encrypting and decrypting to and
from a file, but that's not what I want. I want to be able to insert a
string into an encryption function which outputs that encrypted string,
which could then be sent into a decryption function and spit out the
original string.

As you might expect, ultimately, I want to be able to put this string
into a database table.

Anyways, I set up a simple test page where I enter a string in a
textbox, and then run it through an encrypt, then decrypt, function, and
print out to some labels to make sure it is doing it correctly.

The encrypt part seems to work (at least the function doesn't fail), but
the decrypt function fails, with an error saying that the length of the
encrypted string is invalid. Here are the functions:

*************** *************** *************** *************** **

public static string Encrypt(string StringToEncrypt ){
string EncryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToE ncrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateEncryptor(t deskey, tdesIV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(en cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);

cryptStream.Wri te(inputInBytes , 0, inputInBytes.Le ngth);
cryptStream.Flu shFinalBlock();
encryptedStream .Position = 0;

byte[] result = new byte[encryptedStream .Length - 1];
encryptedStream .Read(result, 0, (int)(encrypted Stream.Length - 1));
cryptStream.Clo se();

UTF8Encoding myutf = new UTF8Encoding();
EncryptedString = myutf.GetString (result);
return EncryptedString ;
}//end Encrypt

public static string Decrypt(string StringToDecrypt ){
string DecryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToD ecrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateDecryptor(t deskey, tdesIV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(de cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);

cryptStream.Wri te(inputInBytes , 0, (inputInBytes.L ength));
cryptStream.Flu shFinalBlock();
decryptedStream .Position = 0;

byte[] result = new byte[decryptedStream .Length - 1];
decryptedStream .Read(result, 0, (int)(decrypted Stream.Length - 1));
cryptStream.Clo se();

DecryptedString = result.ToString ();

return DecryptedString ;
}//end Decrypt

*************** *************** *************** *************** ************

cryptStream.Flu shFinalBlock() in the decrypt function is what fails.

Any ideas would be greatly welcomed.

jdn
ki******@earthl ink.net

Nov 17 '05 #1
5 1586
Change the stream type. The encryption samples use a file stream, but any
type of stream can be used to encrypt/decrypt. I have seen plenty of samples
with strings, as well as files, but you are correct that many of the samples
are file based.

Hope this helps.

--
Gregory A. Beamer
MPV; MCP: +I, SE, SD, DBA

*************** *************** *************** *************** **********
Think outside the box!
*************** *************** *************** *************** **********
"jdn" <ki******@earth link.net> wrote in message
news:uA******** *****@TK2MSFTNG P11.phx.gbl...
I'm new to using this part of the framework, so I'm hoping I've done
something obviously stupid, which someone will be able to point out in
an obvious manner.

Most of the samples I've seen involved encrypting and decrypting to and
from a file, but that's not what I want. I want to be able to insert a
string into an encryption function which outputs that encrypted string,
which could then be sent into a decryption function and spit out the
original string.

As you might expect, ultimately, I want to be able to put this string
into a database table.

Anyways, I set up a simple test page where I enter a string in a
textbox, and then run it through an encrypt, then decrypt, function, and
print out to some labels to make sure it is doing it correctly.

The encrypt part seems to work (at least the function doesn't fail), but
the decrypt function fails, with an error saying that the length of the
encrypted string is invalid. Here are the functions:

*************** *************** *************** *************** **

public static string Encrypt(string StringToEncrypt ){
string EncryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToE ncrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateEncryptor(t deskey, tdesIV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(en cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);

cryptStream.Wri te(inputInBytes , 0, inputInBytes.Le ngth);
cryptStream.Flu shFinalBlock();
encryptedStream .Position = 0;

byte[] result = new byte[encryptedStream .Length - 1];
encryptedStream .Read(result, 0, (int)(encrypted Stream.Length - 1));
cryptStream.Clo se();

UTF8Encoding myutf = new UTF8Encoding();
EncryptedString = myutf.GetString (result);
return EncryptedString ;
}//end Encrypt

public static string Decrypt(string StringToDecrypt ){
string DecryptedString ;
UTF8Encoding utf8encoder = new UTF8Encoding();
byte[] inputInBytes = utf8encoder.Get Bytes(StringToD ecrypt);
TripleDESCrypto ServiceProvider tdesProvider = new
TripleDESCrypto ServiceProvider ();
ICryptoTransfor m cryptoTransform =
tdesProvider.Cr eateDecryptor(t deskey, tdesIV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(de cryptedStream,
cryptoTransform , CryptoStreamMod e.Write);

cryptStream.Wri te(inputInBytes , 0, (inputInBytes.L ength));
cryptStream.Flu shFinalBlock();
decryptedStream .Position = 0;

byte[] result = new byte[decryptedStream .Length - 1];
decryptedStream .Read(result, 0, (int)(decrypted Stream.Length - 1));
cryptStream.Clo se();

DecryptedString = result.ToString ();

return DecryptedString ;
}//end Decrypt

*************** *************** *************** *************** ************

cryptStream.Flu shFinalBlock() in the decrypt function is what fails.

Any ideas would be greatly welcomed.

jdn
ki******@earthl ink.net

Nov 17 '05 #2
jdn
Cowboy (Gregory A Beamer) wrote:
Change the stream type. The encryption samples use a file stream, but any
type of stream can be used to encrypt/decrypt. I have seen plenty of samples
with strings, as well as files, but you are correct that many of the samples
are file based.

Hope this helps.


Change the stream type to what?

Thanks.

jdn

Nov 17 '05 #3
jdn
Cowboy (Gregory A Beamer) wrote:
Change the stream type. The encryption samples use a file stream, but any
type of stream can be used to encrypt/decrypt. I have seen plenty of samples
with strings, as well as files, but you are correct that many of the samples
are file based.

Hope this helps.

Why would changing the stream type fix the issue? It seems to have to
do with how the decrypt function is written or maybe with how the
encrypt function writes the string.

jdn

Nov 17 '05 #4
There are several kinds of streams, IO streams, memory streams, XML streams.. etc.. so if you are dealing with a filestream - then you will only really be able to read/write from a file.. if you want to read/write to a variable, you likely need a MemoryStream or some equivalent..
"jdn" <ki******@earth link.net> wrote in message news:uT******** ******@tk2msftn gp13.phx.gbl...
Cowboy (Gregory A Beamer) wrote:
Change the stream type. The encryption samples use a file stream, but any
type of stream can be used to encrypt/decrypt. I have seen plenty of samples
with strings, as well as files, but you are correct that many of the samples
are file based.

Hope this helps.

Why would changing the stream type fix the issue? It seems to have to
do with how the decrypt function is written or maybe with how the
encrypt function writes the string.

jdn

Nov 17 '05 #5
jdn
Drebin wrote:
There are several kinds of streams, IO streams, memory streams, XML streams.. etc.. so if you are dealing with a filestream - then you will only really be able to read/write from a file.. if you want to read/write to a variable, you likely need a MemoryStream or some equivalent..
"jdn" <ki******@earth link.net> wrote in message news:uT******** ******@tk2msftn gp13.phx.gbl...
Cowboy (Gregory A Beamer) wrote:

Change the stream type. The encryption samples use a file stream, but any
type of stream can be used to encrypt/decrypt. I have seen plenty of samples
with strings, as well as files, but you are correct that many of the samples
are file based.

Hope this helps.


Why would changing the stream type fix the issue? It seems to have to
do with how the decrypt function is written or maybe with how the
encrypt function writes the string.

jdn


No, I know that. Since I am trying to Encrypt and Decrypt using
MemoryStream and CryptoStream both places, I don't see how changing that
will solve the issue. Though maybe I'm missing something.

Nov 17 '05 #6

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

Similar topics

5
2851
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
7
17879
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");
4
2621
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
9072
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
9653
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...
0
9451
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9421
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
8328
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...
0
4771
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...
0
4942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
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.