473,386 Members | 1,741 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,386 software developers and data experts.

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.GetBytes(StringToEncrypt);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateEncryptor(tdeskey, tdesIV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream,
cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;

byte[] result = new byte[encryptedStream.Length - 1];
encryptedStream.Read(result, 0, (int)(encryptedStream.Length - 1));
cryptStream.Close();

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.GetBytes(StringToDecrypt);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateDecryptor(tdeskey, tdesIV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream,
cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, (inputInBytes.Length));
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;

byte[] result = new byte[decryptedStream.Length - 1];
decryptedStream.Read(result, 0, (int)(decryptedStream.Length - 1));
cryptStream.Close();

DecryptedString = result.ToString();

return DecryptedString;
}//end Decrypt

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

cryptStream.FlushFinalBlock() in the decrypt function is what fails.

Any ideas would be greatly welcomed.

jdn
ki******@earthlink.net

Nov 17 '05 #1
5 1573
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******@earthlink.net> wrote in message
news:uA*************@TK2MSFTNGP11.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.GetBytes(StringToEncrypt);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateEncryptor(tdeskey, tdesIV);
MemoryStream encryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(encryptedStream,
cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, inputInBytes.Length);
cryptStream.FlushFinalBlock();
encryptedStream.Position = 0;

byte[] result = new byte[encryptedStream.Length - 1];
encryptedStream.Read(result, 0, (int)(encryptedStream.Length - 1));
cryptStream.Close();

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.GetBytes(StringToDecrypt);
TripleDESCryptoServiceProvider tdesProvider = new
TripleDESCryptoServiceProvider();
ICryptoTransform cryptoTransform =
tdesProvider.CreateDecryptor(tdeskey, tdesIV);
MemoryStream decryptedStream = new MemoryStream();
CryptoStream cryptStream = new CryptoStream(decryptedStream,
cryptoTransform, CryptoStreamMode.Write);

cryptStream.Write(inputInBytes, 0, (inputInBytes.Length));
cryptStream.FlushFinalBlock();
decryptedStream.Position = 0;

byte[] result = new byte[decryptedStream.Length - 1];
decryptedStream.Read(result, 0, (int)(decryptedStream.Length - 1));
cryptStream.Close();

DecryptedString = result.ToString();

return DecryptedString;
}//end Decrypt

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

cryptStream.FlushFinalBlock() in the decrypt function is what fails.

Any ideas would be greatly welcomed.

jdn
ki******@earthlink.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******@earthlink.net> wrote in message news:uT**************@tk2msftngp13.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******@earthlink.net> wrote in message news:uT**************@tk2msftngp13.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
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) {...
7
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.