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
kingcrim@dotnet.itags.org.earthlink.netChange 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" <kingcrim@.earthlink.net> wrote in message
news:uAMyKjEdDHA.904@.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
> kingcrim@.earthlink.net
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
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
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" <kingcrim@.earthlink.net> wrote in message news:uTuNAWYdDHA.2932@.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
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" <kingcrim@.earthlink.net> wrote in message news:uTuNAWYdDHA.2932@.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.
Showing posts with label encryption. Show all posts
Showing posts with label encryption. Show all posts
Wednesday, March 28, 2012
TripleDES Encryption issue
Labels:
asp,
donesomething,
encryption,
framework,
net,
obviously,
point,
stupid,
tripledes
TripleDES with Hexadecimal Key
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
TripleDESCryptoServicerProvider? I realize that what he is giving me
is hexadecimal, but how do I convert it a .Key that I can use with
the .NET framework. The only way that I can see to set the .Key
property is: Dim objDES.Key as Byte() = {48, 3, 17, 131, 202, ....and
so on}
How do I either convert the .Key that the vendor has given me to the
VB.NET language above - or is there another to set the .Key property
that I am overlooking?
Any help anyone could give would be hugely appreciated - thanks!It 0E329232EA6D0D73 is hexadecimal notation of 14, 50, ... in decimal
where did 48, 3, 17, 131, 202, came from?
Use calc.exe to convert it to decimal and write correct values
Dim objDES.Key as Byte() = {14, 50, }
Or you can use
Int32.Parse("0E", NumberStyles.HexNumber )
Int32.Parse("32", NumberStyles.HexNumber )
...
George
"Celia" <c.oblinger@.gmail.com> wrote in message
news:0dcd82f2-3eaa-480d-81cf-749e6158fbd1@.s8g2000prg.googlegroups.com...
>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
> TripleDESCryptoServicerProvider? I realize that what he is giving me
> is hexadecimal, but how do I convert it a .Key that I can use with
> the .NET framework. The only way that I can see to set the .Key
> property is: Dim objDES.Key as Byte() = {48, 3, 17, 131, 202, ....and
> so on}
> How do I either convert the .Key that the vendor has given me to the
> VB.NET language above - or is there another to set the .Key property
> that I am overlooking?
> Any help anyone could give would be hugely appreciated - thanks!
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
TripleDESCryptoServicerProvider? I realize that what he is giving me
is hexadecimal, but how do I convert it a .Key that I can use with
the .NET framework. The only way that I can see to set the .Key
property is: Dim objDES.Key as Byte() = {48, 3, 17, 131, 202, ....and
so on}
How do I either convert the .Key that the vendor has given me to the
VB.NET language above - or is there another to set the .Key property
that I am overlooking?
Any help anyone could give would be hugely appreciated - thanks!It 0E329232EA6D0D73 is hexadecimal notation of 14, 50, ... in decimal
where did 48, 3, 17, 131, 202, came from?
Use calc.exe to convert it to decimal and write correct values
Dim objDES.Key as Byte() = {14, 50, }
Or you can use
Int32.Parse("0E", NumberStyles.HexNumber )
Int32.Parse("32", NumberStyles.HexNumber )
...
George
"Celia" <c.oblinger@.gmail.com> wrote in message
news:0dcd82f2-3eaa-480d-81cf-749e6158fbd1@.s8g2000prg.googlegroups.com...
>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
> TripleDESCryptoServicerProvider? I realize that what he is giving me
> is hexadecimal, but how do I convert it a .Key that I can use with
> the .NET framework. The only way that I can see to set the .Key
> property is: Dim objDES.Key as Byte() = {48, 3, 17, 131, 202, ....and
> so on}
> How do I either convert the .Key that the vendor has given me to the
> VB.NET language above - or is there another to set the .Key property
> that I am overlooking?
> Any help anyone could give would be hugely appreciated - thanks!
Subscribe to:
Posts (Atom)