473,837 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can you encrypt a serializable object?

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(my Buffer, company)

myBuffer.Positi on = 0

fe.EncryptFile( myBuffer, OutBuffer)

Dim byteBuffer(CInt (OutBuffer.Leng th - 1)) As Byte

OutBuffer.Read( byteBuffer, 0, byteBuffer.Leng th)

dataStream.Writ e(byteBuffer, 0, byteBuffer.Leng th)

dataStream.Clos e()

Encrption:

Sub EncryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

Dim cdk As New PasswordDeriveB ytes( _

"1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=", Nothing)

' generate an RC2 key

Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServic eProvider

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.Lengt h - 1)) As Byte

inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th).ToString()

' Create CryptoStream with write access to encrypt filestream using RC2

Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
CryptoStreamMod e.Write)

' Write CryptoStream bytes from buffer from offset 0 to end of buffer

cs.Write(byteBu ffer, 0, byteBuffer.Leng th)

outBuffer.Posit ion = 0

Dim str As Byte() = outBuffer.ToArr ay

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(my Buffer, company)

myBuffer.Positi on = 0

fe.EncryptFile( myBuffer, OutBuffer)

Dim byteBuffer(CInt (OutBuffer.Leng th - 1)) As Byte

OutBuffer.Read( byteBuffer, 0, byteBuffer.Leng th)

dataStream.Writ e(byteBuffer, 0, byteBuffer.Leng th)

dataStream.Clos e()

Deryption:

Sub DecryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

Dim cdk As New PasswordDeriveB ytes( _

"1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=", Nothing)

' generate an RC2 key

Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServic eProvider

rc2.Key = key

rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}

Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte

inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)

inBuffer.Positi on = 0

Dim strDataIn As Byte() = inBuffer.ToArra y

' Create the crypto stream with read access to decrypt incoming bytes using
RC2.

Try

Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
CryptoStreamMod e.Read)

Dim fsBuffer As New BinaryWriter(ou tBuffer)

' Write out the decrypted file.

fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd) <======
ex.message ="Bad Data"

outBuffer.Posit ion = 0

fsBuffer.Flush( )

Catch ex As Exception

Dim str As String

str = ex.Message

End Try

End Sub
Nov 21 '05 #1
1 3225
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
CryptoStreamMod e.Read in the Decryption. You can use it
(CryptoStreamMo de.Read ) for decrypting file and network streams, but
apparently you need to use CryptoStreamMod e.Write for Memory.

Although I am still somewhat confused and definitely dazed!

THANK YOU IVAN!!

"DazedAndConfus ed" <Ac********@yah oo.com> wrote in message
news:Oz******** ******@TK2MSFTN GP12.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(my Buffer, company)

myBuffer.Positi on = 0

fe.EncryptFile( myBuffer, OutBuffer)

Dim byteBuffer(CInt (OutBuffer.Leng th - 1)) As Byte

OutBuffer.Read( byteBuffer, 0, byteBuffer.Leng th)

dataStream.Writ e(byteBuffer, 0, byteBuffer.Leng th)

dataStream.Clos e()

Encrption:

Sub EncryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

Dim cdk As New PasswordDeriveB ytes( _

"1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
Nothing)

' generate an RC2 key

Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServic eProvider

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.Lengt h - 1)) As Byte

inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th).ToString()

' Create CryptoStream with write access to encrypt filestream using RC2

Dim cs As New CryptoStream(ou tBuffer, rc2.CreateEncry ptor(),
CryptoStreamMod e.Write)

' Write CryptoStream bytes from buffer from offset 0 to end of buffer

cs.Write(byteBu ffer, 0, byteBuffer.Leng th)

outBuffer.Posit ion = 0

Dim str As Byte() = outBuffer.ToArr ay

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(my Buffer, company)

myBuffer.Positi on = 0

fe.EncryptFile( myBuffer, OutBuffer)

Dim byteBuffer(CInt (OutBuffer.Leng th - 1)) As Byte

OutBuffer.Read( byteBuffer, 0, byteBuffer.Leng th)

dataStream.Writ e(byteBuffer, 0, byteBuffer.Leng th)

dataStream.Clos e()

Deryption:

Sub DecryptFile( _

ByRef inBuffer As MemoryStream, _

ByRef outBuffer As MemoryStream)

Dim cdk As New PasswordDeriveB ytes( _

"1E1705459E1B35 20943FC00CF8E7C EEDA68BF5FAgtGp sCVNkFAo3am992z 7kgc=",
Nothing)

' generate an RC2 key

Dim iv() As Byte = {0, 0, 0, 0, 0, 0, 0, 0}

Dim key As Byte() = cdk.CryptDerive Key("RC2", "SHA1", 128, iv)

' setup an RC2 object to encrypt with the derived key

Dim rc2 As New RC2CryptoServic eProvider

rc2.Key = key

rc2.IV = New Byte() {21, 22, 23, 24, 25, 26, 27, 28}

Dim byteBuffer(CInt (inBuffer.Lengt h - 1)) As Byte

inBuffer.Read(b yteBuffer, 0, byteBuffer.Leng th)

inBuffer.Positi on = 0

Dim strDataIn As Byte() = inBuffer.ToArra y

' Create the crypto stream with read access to decrypt incoming bytes
using RC2.

Try

Dim cryptostreamDec r As New CryptoStream(in Buffer, rc2.CreateDecry ptor(),
CryptoStreamMod e.Read)

Dim fsBuffer As New BinaryWriter(ou tBuffer)

' Write out the decrypted file.

fsBuffer.Write( New StreamReader(cr yptostreamDecr) .ReadToEnd) <======
ex.message ="Bad Data"

outBuffer.Posit ion = 0

fsBuffer.Flush( )

Catch ex As Exception

Dim str As String

str = ex.Message

End Try

End Sub

Nov 21 '05 #2

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

Similar topics

8
6322
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. Is there a document, or can some one list those types that are not serializable and the syntax for converting them? Thanks
3
11477
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 unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: Unable to...
1
2291
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 want to use FormsAuthentication feature. I already have custom code to redirect the use back to the login page if they haven't signed on. My question is can I use the FormsAuthentication.Encrypt method to encrypt my cookies without using the...
1
2681
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 objClassA = (ClassA) ViewState // After PostBack Any Body can help me out?
1
2052
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 to do that and still store it in the XML File? I was thinking of putting the Encryption/Decryption code in the Get/Set methods of the SMTPPassword Property. Sample Code:
3
1824
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, which has some properties on it, but I want to make one of the Properties a generic type that I can assign one of multiple Serializable object. I've look at creating it using an XmlNode and an object but so far both appear to have issues. I've...
8
1825
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 a reference to my custom object's namespace instead of creating it's own copy of the object in it's own WS namespace. Either that, or some easy way to move all of the properties from the Custom Serializable Object into the WSDL generated object...
0
2323
mmfranke
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 DiagnosticEvent objects for processing. I've altered the main program for the service slightly so that I can run it more easily as a "regular" application during debugging. I have an assembly that contains the "diagnostics" classes that do the work, the...
2
1606
by: Morten Snedker | last post by:
<Serializable()Public Class Dealer What does Serializable do? Regards /Snedker
0
10563
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
10621
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,...
1
7804
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
6998
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
5668
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
5845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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 we have to send another system
2
4038
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3123
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.