Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1463

VB6 - SillyStream Encrypted Text I/O

$
0
0
While hand-rolled encryption is never a good idea, sometimes you don't need a high-security solution. SillyStream is a text I/O class that you can set a few parameters on and then do low-volme encrypted text file I/O. It would need optimization if you really need to work on large files, but it was really intended for smaller items such as settings files.


Usage

Pretty simple, just add SillyStream.cls to your Project. Then create instances as you need them (eaach only handles one file at a a time). Call Init() to set the parameters, then one of the open methods (input, output, append), and use the read and write methods, and finally the close method. Then you can open a new file either with or without setting new parameters first.


Parameters

There are two mask values (one a Byte, another a Long) and two dimensional parameters. These have funny names in the class and in the demo Project. See the comments for an explanation.


Cipher Used Here

This is a combination cipher, based on two very very simple ciphers.

The main feature here is a transposition cipher using the two dimensions provided to create a buffer block as a matrix. When writing or reading the file, the data is an inversion of this matrix.

On top of that we have simple (really simple) substitution cipher. All of the data characters a XORed with a one Byte mask. With some effort you could use a longer "password" mask but it complicates things (especially "open append" operations).

The final block is padded with pseudorandom values.

Finally, to make this work even with a padded final block, the actual data length is stored in the first 4 bytes of the file as a Long value. This is XORed with the Long mask you provide to help obscure it a bit. Adding these 4 bytes has another advantage: It makes it all little tougher to crack because simply trying to factor the file size will be a bit of a red herring even if someone guesses it uses a rectangular transposition cipher.

Dumb but cheap, and perhaps good enough.


Demo

The demo Project lets you open a new files, type some stuff, write is as characters or as a line 9adding newline), etc, and then close. Then you can read it back and see it displayed, using both reading by byte count or the slower reading by line.

Even though slower, I expect most people to just read by line most of the time. Writing by line is probably going to be more useful then by text length as well.

Sample inputs:
Code:

This is a test.
This is only a test.
How now, brown cow?
Mellow yellow, such a silly fellow!
Test test test test test test test test test test testing!

Hex dump:
Code:

0000        e5 54 12 f0 0f 2f 35 13  35 2c 33 37 28 2f 2f 3e  .T.../5.5,37(//>
0010        51 7c 07 8c 15 33 75 37  34 7b 7b 7b 34 2f 3e 7b  Q|...3u74{{{4/>{
0020        28 04 83 14 93 22 32 56  22 2c 38 22 3a 2c 7b 28  (...."2V",8":,{(
0030        2f 2f 0b 90 1b a0 29 28  51 7b 7b 34 3e 7b 7a 2f  //....)(Q{{4>{z/
0040        2f 3e 7b 18 97 28 a7 36  7b 0f 3a 35 2c 37 28 56  />{..(.6{.:5,7(V
0050        3e 7b 28 2f 1f a4 2f b4  3d 32 33 7b 34 64 37 32  >{(/../.=23{4d72
0060        51 28 2f 2f 3e 2c ab 3c  bb 4a 28 32 2f 2c 56 34  Q(//>,.<.J(2/,V4
0070        37 0f 2f 3e 7b 28 33 b8  43 c8 51 7b 28 3e 77 51  7./>{(3.C.Q{(>wQ
0080        2c 37 3e 7b 28 2f 2f 40  bf 50 cf 5e 3a 7b 28 7b  ,7>{(//@.P.^:{({
0090        16 77 22 28 2f 2f 3e 32  47 cc 57 dc 65 7b 32 2f  .w"(//>2G.W.e{2/
00a0        39 3e 7b 7b 2f 3e 7b 28  35 54 d3 64 e3 72 2f 28  9>{{/>{(5T.d.r/(
00b0        75 29 37 28 3d 7b 28 2f  2f 3c 5b e0 6b f0 79 3e  u)7(={(//<[.k.y>
00c0        7b 56 34 37 2e 3e 2f 2f  3e 7b 7a 68 e7 78 f7 86  {V47.>//>{zh.x..
00d0        28 34 51 2c 34 38 37 3e  7b 28 2f 56 6f f4 7f 0e  (4Q,487>{(/Vo...
00e0        8d                                                .

Hex dump of a test with the data XORing omitted. Normally you wouldn't do this but it shows that the transposition and padding can be effective all alone:
Code:

0000        e5 54 12 f0 54 74 6e 48  6e 77 68 6c 73 74 74 65  .T..TtnHnwhlstte
0010        0a af 30 b5 3c 68 2e 6c  6f 20 20 20 6f 74 65 20  ..0.<h.lo  ote
0020        73 37 b6 3d bc 49 69 0d  79 77 63 79 61 77 20 73  s7.=.Ii.ywcyaw s
0030        74 74 3e c3 44 c9 50 73  0a 20 20 6f 65 20 21 74  tt>.D.Ps.  oe !t
0040        74 65 20 4b ca 51 d0 5d  20 54 61 6e 77 6c 73 0d  te K.Q.] Tanwls.
0050        65 20 73 74 52 d7 58 dd  64 69 68 20 6f 3f 6c 69  e stR.X.dih o?li
0060        0a 73 74 74 65 5f de 65  e4 71 73 69 74 77 0d 6f  .stte_.e.qsitw.o
0070        6c 54 74 65 20 73 66 eb  6c f1 78 20 73 65 2c 0a  lTte sf.l.x se,.
0080        77 6c 65 20 73 74 74 73  f2 79 f8 85 61 20 73 20  wle stts.y..a s
0090        4d 2c 79 73 74 74 65 69  7a ff 80 0d 8c 20 69 74  M,ystteiz.... it
00a0        62 65 20 20 74 65 20 73  6e 87 08 8d 14 99 74 73  be  te sn.....ts
00b0        2e 72 6c 73 66 20 73 74  74 67 8e 15 94 21 a0 65  .rlsf sttg...!.e
00c0        20 0d 6f 6c 75 65 74 74  65 20 21 9b 1c a1 28 ad    .oluette !...(.
00d0        73 6f 0a 77 6f 63 6c 65  20 73 74 0d a2 29 a8 35  so.wocle st..).5
00e0        b4                                                .


Bugs

I havent found any, but if you do be sure to mention them.
Attached Files

Viewing all articles
Browse latest Browse all 1463

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>