Encrypt and Decrypt object - c#-4.0

I have a class of two strings
I want to decrypt and encrypt using RC4 , I know how to do that for one string
http://tofuculture.com/blog/post/RC4-Encryption-in-C.aspx
can I apply to whole object

The easiest way to serialize into a complex format is to first serialize into a simple format (in this case string) and then perform the known transformation from the simple to complex.
So in your example you want to serialize two strings, so decide on a separation character or string that won't appear in your data and use it to separate the two strings. This allows you to create a single string, which you already know how to handle. Then when you want to deserialize you do the opposite, finding the separation and splitting the strings again into a new object.

Related

Optimising dictionary key generation/lookup in python 3

My program will contain a lot of references to a few large python dictionaries. The dictionaries use as keys fairly long strings (often +100 characters). I need to often check the presence of a key in these dictionaries. And very often it would be for the same string as it passes through the flow of the script.
Checking the presence of a key in a dictionary is O(1). However, producing a hash of a string (which is what a dictionary would do) is O(N) where N is the length of the string. Since I need to do these checks very often for the same string I was wondering if there a way to optimise this hash re-generation? I was thinking along the lines of (pseudo-code follows):
(1) receive a long string as an input
(2) create a short version of the string, e.g. by using MD5 or CRC32
(3) use the short version as a key
Does this make sense?
If yes, what sort of compression/hashing would you suggest?
To be honest, I was over-engineering the problem.
I was trying to mimic the structure of a RDBMS table so was looking for unique primary keys. These were the long strings that I was thinking of hashing. And I was looking for an efficient ways to do the hashing.
But the solution was much simpler - I found the equivalent of an auto-increment PK. In python you can do this with itertools.count(). So, instead of using the original long strings I started using itertools.count(). It is not an ideal solution but it solved the problem.

Does a string use a list to store char's?

Curious if a string data type uses a list to store its characters? And if so, what is the list implementation type?
Other than being immutable, a String has elements and index positions like a list so wondering if a String is actually a list data structure.
In most modern programming languages, a string is a wrapper around an array. In .NET, for example, the backing store is an array of 16-bit integers, each of which holds a single UTF-16 code point. Whatever the backing store, member functions provide indexing and such.
In C, there isn't even a string type. What we call strings are really just arrays of characters. There are certain rules for the format of those arrays (like a null byte, \0, terminates the string), and "string functions" that operate on those arrays as though they are strings.
There are string handling libraries that use ropes to more efficiently manipulate long strings.
Note also that not all languages specify that strings are immutable. In some languages, you can modify individual characters within the string, and do some other things without actually creating a new string object.

How do I store both text and binary files in an hsqldb database?

I would like to give two API functions to store files in a DB - one accepting a byte[] (or an InputStream) and another one accepting a String. I wonder how to implement such an API.
I see the following options:
Define two fields - one a BLOB for binary files and another one a CLOB - for text files. Then I could use PreparedStatement.setBytes (or PreparedStatement.setBinaryStream) for BLOBs and PreparedStatement.setString for CLOBs. I do not like having two fields.
Use String.getBytes() to convert the given String to byte[], thus coming back to the binary case. I do not like the need to convert a String to the byte[].
I was wondering whether there is a solution which does not require two fields and avoids the extra byte buffer.
Java String and Clob are Unicode and stored as two-byte characters. Therefore some conversion is necessary if you want to store these values in a byte[], or Blob.
Therefore the best approach is to use two separate fields, one for BLOB and the other for CLOB values.

Data Structure to use instead of hash_map

I want to make an array containing three wide character arrays such that one of them is the key.
"LPWCH,LPWCH,LPWCH" was not able to use the greater than/lesser than symbols since it thinks it is a tag
Hash_map only lets me use a pair. wKey and the element associated with it. Is there another data structure that lets me use this?
This set will be updated by different threads almost simultaneously. And thats the reason why I don't want to use a class or another struct to define the remaining two wide character arrays.
You can use LPWCH as a key and std::pair<LPWCH, LPWCH> as an element.
Using any of LP-typedefs is not good. You would only be comparing the points, and not strings.
LPWCH is nothing but a WCHAR* which can be drilled down to void*. When you compare two pointers, you are comparing where they are pointing, and not what they are pointing.
You either need to have another comparer attached to your map/hash_map, or use actual string datatype (like std::string, CString)

how to write read readable strings in scala

In scala, when you write out a string "Hello World" to a file it writes
Hello World
(note: no double quotes).
Lisp has a concept of print and write. One writes without the double quotes, the other includes them to make it easy to write out data structures and read them back later using the standard reader.
Is there anyway to do this in Scala?
With one string it is easy enough to format it - but with many deeply nested structures, it is nearly impossible.
For example, say I have
sealed trait PathSegment
case class P(x:String) extends PathSegment
case class V(x:Int) extends PathSegment
To create one does:
P("X")
or
V(0)
a list of these PathSegments prints as:
List(P(paths), P(/pets), P(get), P(responses), V(200))
I want this to print out as:
List(P("paths"), P("/pets"), P("get"), P("responses"), V(200))
In other words, I want strings (and characters), no matter where to occur in a structure to print out as "foo" or 'c'
That's what Serialization is about. Also, why JSON is popular.
Check out lift-json ( https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/ ) for writing data out that will be parsed and read by another language. JSON is pretty standard in the web services world for request/response serialization and there are JSON libraries in just about every language.
To literally write out a string including double quotes, you can also do something like this:
"""
The word "apple" is in double quotes.
"""
I find a slightly more structured format like JSON more useful, and a library like lift-json does the right thing in terms of quoting Strings and not quoting Ints, etc.
I think you are looking for something like Javascript's eval() + JSON, and Python's eval(), str() and repr(). Essentially, you want Lispy symmetric meta-circular evaluation. Meaning you can transform data into source code, and evaluating that source code with give you back the same data, right?
AFAIK, there's no equivalent of eval() in Scala. Daniel Spiewak has talked about this here before. However, if you reeeeeealy want to. I suggest the following things:
Every collection object has 3 methods that will allow you to transform its data to a string representation anyway you want. There are mkString, addString and stringPrefix. Do something clever with them (think "decompiling" your in-memory ADTs back to source-code form) and you shall arrive to step 2). Essentially, you can transform a list of integers created by List(1,2,3) back to a string "List(1,2,3)". For more basic literals like a simple string or integer, you'll need to pimp the built-in types using implicits to provide them with these toString (I'm overloading the term here) helper methods.
Now you have your string representation, you can think about how to "interpret" or "evaluate" them. You will need an eval() function that create a new instance of a parser combinator that understands Scala's literals and reassemble the data structure for you.
Implementing this actually sounds fun. Don't forget to post back here if you've successfully implementing it. :)

Resources