Can you encrpt a serialized object?
Or am I trying to do something that just doesn't work that way?
I am trying to encrypt a serialized object. I can read and write the object
to a file without a problem(IF I don't encrypt it).
The encryption routine I am using works great when I am just reading in text
and writing out encrypted data/Reading in encrpted data and writing out
decrypted text.
BUT when after I encrypt the object, I can not decrypt it, I get a Bad Data
message.
From what I can see the exact same data coming out of the encryption is
going into the decryption. I am stuck.
Call to encrption:
Dim fe As New FileEncrypt
Dim myBuffer As New MemoryStream
Dim OutBuffer As New MemoryStream
Dim bf As New BinaryFormatter
bf.Serialize(myBuffer, company)
myBuffer.Position = 0
fe.EncryptFile(myBuffer, OutBuffer)
Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte
OutBuffer.Read(byteBuffer, 0, byteBuffer.Length)
dataStream.Write(byteBuffer, 0, byteBuffer.Length)
dataStream.Close()
Encrption:
Sub EncryptFile( _
ByRef inBuffer As MemoryStream, _
ByRef outBuffer As MemoryStream)
Dim cdk As New PasswordDeriveBytes( _
"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNk FAo3am992z7kgc=", Nothing)
' generate an RC2 key
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key As Byte() = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)
' setup an RC2 object to encrypt with the derived key
Dim rc2 As New RC2CryptoServiceProvider
rc2.Key = key
rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
'Read unencrypted file input into the buffer byte array.
Dim byteBuffer(CInt(inBuffer.Length - 1)) As Byte
inBuffer.Read(byteBuffer, 0, byteBuffer.Length).ToString()
' Create CryptoStream with write access to encrypt filestream using RC2
Dim cs As New CryptoStream(outBuffer, rc2.CreateEncryptor(),
CryptoStreamMode.Write)
' Write CryptoStream bytes from buffer from offset 0 to end of buffer
cs.Write(byteBuffer, 0, byteBuffer.Length)
outBuffer.Position = 0
Dim str As Byte() = outBuffer.ToArray
cs.Flush()
inBuffer.Close()
End Sub
Call to Decrption:
Dim fe As New FileEncrypt
Dim myBuffer As New MemoryStream
Dim OutBuffer As New MemoryStream
Dim bf As New BinaryFormatter
bf.Serialize(myBuffer, company)
myBuffer.Position = 0
fe.EncryptFile(myBuffer, OutBuffer)
Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte
OutBuffer.Read(byteBuffer, 0, byteBuffer.Length)
dataStream.Write(byteBuffer, 0, byteBuffer.Length)
dataStream.Close()
Deryption:
Sub DecryptFile( _
ByRef inBuffer As MemoryStream, _
ByRef outBuffer As MemoryStream)
Dim cdk As New PasswordDeriveBytes( _
"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNk FAo3am992z7kgc=", Nothing)
' generate an RC2 key
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key As Byte() = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)
' setup an RC2 object to encrypt with the derived key
Dim rc2 As New RC2CryptoServiceProvider
rc2.Key = key
rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
Dim byteBuffer(CInt(inBuffer.Length - 1)) As Byte
inBuffer.Read(byteBuffer, 0, byteBuffer.Length)
inBuffer.Position = 0
Dim strDataIn As Byte() = inBuffer.ToArray
' Create the crypto stream with read access to decrypt incoming bytes using
RC2.
Try
Dim cryptostreamDecr As New CryptoStream(inBuffer, rc2.CreateDecryptor(),
CryptoStreamMode.Read)
Dim fsBuffer As New BinaryWriter(outBuffer)
' Write out the decrypted file.
fsBuffer.Write(New StreamReader(cryptostreamDecr).ReadToEnd) <======
ex.message ="Bad Data"
outBuffer.Position = 0
fsBuffer.Flush()
Catch ex As Exception
Dim str As String
str = ex.Message
End Try
End Sub 1 3187
Yes you can.
I found the answer to my problem at :
Ivan Medvedev's blog http://blogs.gotdotnet.com/ivanmed/P...6-5973106935a4
After banging my head for five days the problem was using
CryptoStreamMode.Read in the Decryption. You can use it
(CryptoStreamMode.Read ) for decrypting file and network streams, but
apparently you need to use CryptoStreamMode.Write for Memory.
Although I am still somewhat confused and definitely dazed!
THANK YOU IVAN!!
"DazedAndConfused" <Ac********@yahoo.com> wrote in message
news:Oz**************@TK2MSFTNGP12.phx.gbl... Can you encrpt a serialized object?
Or am I trying to do something that just doesn't work that way?
I am trying to encrypt a serialized object. I can read and write the object to a file without a problem(IF I don't encrypt it).
The encryption routine I am using works great when I am just reading in text and writing out encrypted data/Reading in encrpted data and writing out decrypted text.
BUT when after I encrypt the object, I can not decrypt it, I get a Bad Data message.
From what I can see the exact same data coming out of the encryption is going into the decryption. I am stuck.
Call to encrption:
Dim fe As New FileEncrypt
Dim myBuffer As New MemoryStream
Dim OutBuffer As New MemoryStream
Dim bf As New BinaryFormatter
bf.Serialize(myBuffer, company)
myBuffer.Position = 0
fe.EncryptFile(myBuffer, OutBuffer)
Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte
OutBuffer.Read(byteBuffer, 0, byteBuffer.Length)
dataStream.Write(byteBuffer, 0, byteBuffer.Length)
dataStream.Close() Encrption:
Sub EncryptFile( _
ByRef inBuffer As MemoryStream, _
ByRef outBuffer As MemoryStream)
Dim cdk As New PasswordDeriveBytes( _
"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNk FAo3am992z7kgc=", Nothing)
' generate an RC2 key
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key As Byte() = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)
' setup an RC2 object to encrypt with the derived key
Dim rc2 As New RC2CryptoServiceProvider
rc2.Key = key
rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
'Read unencrypted file input into the buffer byte array.
Dim byteBuffer(CInt(inBuffer.Length - 1)) As Byte
inBuffer.Read(byteBuffer, 0, byteBuffer.Length).ToString()
' Create CryptoStream with write access to encrypt filestream using RC2
Dim cs As New CryptoStream(outBuffer, rc2.CreateEncryptor(), CryptoStreamMode.Write)
' Write CryptoStream bytes from buffer from offset 0 to end of buffer
cs.Write(byteBuffer, 0, byteBuffer.Length)
outBuffer.Position = 0
Dim str As Byte() = outBuffer.ToArray
cs.Flush()
inBuffer.Close()
End Sub Call to Decrption:
Dim fe As New FileEncrypt
Dim myBuffer As New MemoryStream
Dim OutBuffer As New MemoryStream
Dim bf As New BinaryFormatter
bf.Serialize(myBuffer, company)
myBuffer.Position = 0
fe.EncryptFile(myBuffer, OutBuffer)
Dim byteBuffer(CInt(OutBuffer.Length - 1)) As Byte
OutBuffer.Read(byteBuffer, 0, byteBuffer.Length)
dataStream.Write(byteBuffer, 0, byteBuffer.Length)
dataStream.Close() Deryption:
Sub DecryptFile( _
ByRef inBuffer As MemoryStream, _
ByRef outBuffer As MemoryStream)
Dim cdk As New PasswordDeriveBytes( _
"1E1705459E1B3520943FC00CF8E7CEEDA68BF5FAgtGpsCVNk FAo3am992z7kgc=", Nothing)
' generate an RC2 key
Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}
Dim key As Byte() = cdk.CryptDeriveKey("RC2", "SHA1", 128, iv)
' setup an RC2 object to encrypt with the derived key
Dim rc2 As New RC2CryptoServiceProvider
rc2.Key = key
rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}
Dim byteBuffer(CInt(inBuffer.Length - 1)) As Byte
inBuffer.Read(byteBuffer, 0, byteBuffer.Length)
inBuffer.Position = 0
Dim strDataIn As Byte() = inBuffer.ToArray
' Create the crypto stream with read access to decrypt incoming bytes using RC2.
Try
Dim cryptostreamDecr As New CryptoStream(inBuffer, rc2.CreateDecryptor(), CryptoStreamMode.Read)
Dim fsBuffer As New BinaryWriter(outBuffer)
' Write out the decrypted file.
fsBuffer.Write(New StreamReader(cryptostreamDecr).ReadToEnd) <====== ex.message ="Bad Data"
outBuffer.Position = 0
fsBuffer.Flush()
Catch ex As Exception
Dim str As String
str = ex.Message
End Try
End Sub
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: xmail123 |
last post by:
Hi,
As was pointed out whatever you return from a WebMethod needs to be
serializable to SOAP. An ArrayList is not serializable. I will be
needing to return other data types from web methods. ...
|
by: Raghavendra Tilve |
last post by:
Unable to serialize the session state. Please note that non-serializable
objects or MarshalByRef objects are not permitted when session state mode is
'StateServer' or 'SQLServer'.
Description: An...
|
by: Tommy |
last post by:
I want to encrypt the values of my cookies. I found out that I could
create a FormsAuthenticationTicket, and use the
FormsAuthentication.Encrypt method to encrypt the cookie.
However, I do not...
|
by: Harshdeep Mehta |
last post by:
Hi all,
I have written a page in which I want to preserve value of object - suppose
A - value, so I used viewstate. but that give me error.
Code :
ViewState = objClassA; // Before PostBack...
|
by: Kevin Hodgson |
last post by:
I store a users SMTP UserName and Password in a settings class, which is
serialized to an XML Settings File.
I need to encrypt the password string in some manner. Does anyone have any
ideas how...
|
by: Techno_Dex |
last post by:
I'm wanting to create a Wrapper (or Extender depending on how you look at
it) for a Serializable object that I then want to send over a webservice.
Basically I want to create a Serializable Object,...
|
by: Techno_Dex |
last post by:
Has anyone come up with a slick way to make Custom Serializable Objects to
behave like DataSets when using WebServices? What I'm looking for is some
way to force the WSDL generated code to create...
|
by: mmfranke |
last post by:
Hello.
Hi.
I'm writing a logging service that uses .NET remoting. The idea is that the service publishes an EventProcessor object that clients can access via .NET Remoting, passing it...
|
by: Morten Snedker |
last post by:
<Serializable()Public Class Dealer
What does Serializable do?
Regards /Snedker
|
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...
|
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...
|
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,...
|
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: 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...
|
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,...
|
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: 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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |