Saturday, July 30, 2011

RSA encryption algorithm example C# with keys up to 2048 bits (with multiple threads)

At the end of december 2010 I started to do some research on the RSA implementation with big encryption/decryption keys. There were many reasons why I wanted to do this:

  1. At university, in 2009, I had the task to implement the RSA algorithm. This was not so hard, but a little challenging for the one that never did any kind of encryption. So I implemented this algorithm, and encrypted every byte using it. The enc/dec key had several bits, no more. This was enough for the practice lessons, the single problem was that the key was very small, and I wanted to know how to encrypt with bigger keys. Anyway, I didn't do anything in this direction al all.
  2. In 2010, when I was doing my master degree at another university, there were some requests from my fellow students at the original University to provide them my RSA project as an example, so for them to be easier to understand how to implement it. The harder part was that we were supposed to calculate the approximate time of encryption and the decryption at runtime, like in the case when Windows explorer calculates how many minutes you have to wait until all the files are copied. It was then that I thought to create an RSA example, more close to that what is used in practice, and to provide it for all that want to understand how it works. I thought about encrypting with bigger keys (because currently the safe RSA key is considered to be 2048 bits), and to encrypt the file on multi-core machines.
  3. The third reason was to better understand all the operations on bits, bytes etc. in C#. Believe me, it was worth the effort :D.
So, I started in december 2010, worked on the project for some months, but when I started my professional training, I stopped working on it. My time was completely allocated to it, and even now it is allocated for other things and the project remains unfinished. I thought that maybe it's worth posting it as it is, because anyway I did some good job (I think), and I learned a lot in creating this app. Maybe others will want to see what I did, and to try to understand themselves how to do what I did. I don't claim that I did something extraordinary, but at the time when I started, there wasn't any project with such features in C#, at least I didn't found, and anyway, I wanted to do all this myself (just 4 fun). 

I shall describe right now my app., what it does and what it doesn't do.

The main window:

The settings window:

So, what my program does can be deduced from the images above. Now the problems:

  1. A key with the length of 2048 bits cannot be always generated, I don't know the reason right now, some test must be done. I wasn't able to generate a key bigger than 2048 bits, even though the BigInteger class used in this application (from codeproject) can generate such a key.
  2. Even if a big key is used to encrypt the data, it's a very simple method used here. I encrypt every byte, and as you may guess, the encrypted file can be easily cracked, if we know that some 2048 bits in the file represent a byte (and in my encrypted file every line is an encrypted byte :) ), and if the encrypted text was in English, then we can find, using statistical methods and knowing the probability of English characters in an average text, what the text is.
  3. I tried to correct the problem stated in point 2, by encrypting not every byte, but a block of bytes. In such case, it will be more difficult to crack the file. But here I ran in some problems. Firstly, if we choose a block of 3 bytes to be encrypted together, then the simple equality must be satisfied, to assure that we will be able to decrypt the file: file.length % blockSize == 0; . Well, this wasn't so difficult to achieve, I simply added some zeros at the end of the file, and I remove them after decryption. But, a bigger problem here arises at decryption. For example, if we had to encrypt together the bytes: 1 20 255, then our string to encrypt will be 120255. OK, we can encrypt it, but how we will be able to find what were the numbers that we encrypted at decryption, i.e. 1 has the length 1, 20 has 2 and 255 has 3. Even if we decrypt our string, we will not know what the encrypted bytes were. In such a case, we must add some redundancy, or prior to encryption, to transform, using a dictionary in which any byte will be represented by a number with a fixed length, for example: 1 to be 1001, 20 to be 1020, 255 to be 1255. In such case, we will be able to find the original bytes. Anyway, this is just an idea :)
  4. I think you noticed that all the labels are in brackets. This is because I wanted to add language support for my app, but I haven't done it yet.

So, this is it. Because I cannot allocate time right now to do some research how in practice RSA works (and it's not easy to find such information) and to develop my app. (maybe in future, just for fun), I posted my source code on this blog.

Any suggestions and help are welcome!

I hope this project will be useful for those who want to understand a little more on RSA, but because I never worked on official encryption projects, I cannot provide more information on RSA encryption in practice.

Here's the link to the project's GitHub repository:


Monday, June 6, 2011

Soft body manipulation using an attached rigid body with Phantom Omni Arm

I uploaded a new video that demonstrates the manipulation of a soft body using an attached rigid body and with the Phantom Omni Arm.
I attached the rigid body to the soft one with a linear joint. The goal is to insert the soft body in the tube that is present in the scene. I detect the insertion testing the Aabb boxes of the two objects.

- The manipulation is not intuitive, is like manipulating a copter, and to stabilize the moved object, you must move the Phantom Arm in the opposite direction. This is because I apply a force to the moved object, and Bullet calculates his inertia after that. No prediction or something to make the manipulation more intuitive is used. Tested on some users, they stated that is difficult. The problem is in the fact that I do all the rendering and simulation in the same thread. I also tried to do the simulation in another Win32 thread, but there appear problems on synchronization, that are not so easy to correct. Also, I did some research on the possibility to manipulate a soft object directly, and the only solution was to loop through all the soft body clusters, and to calculate, based on their velocities etc. the resultant force, and apply it to the cluster. In general, you will obtain at the end the possibility to manipulate directly the body. The manipulation, however, is not easy, and for those who tested what I done was more difficult to insert the soft body in the tube than in the case of an attached rigid body :)
- I must be very accurate with the forces that I apply to the moved objects, because if not, they may explode. So that's the main reason why no enhancements were done to assist the user in the manipulation. Also because I had no allocated time for that (to think and implement).

Here's the link to the video:

Thursday, April 28, 2011

Soft body manipulation with Phantom arm

From the last demo that I presented, I was working on the integration of the soft bodies in my application. I found it extremely difficult to achieve, due to the fact that there is no documentation on world configuration, bodies configuration and so on.

In the end, I managed to integrate soft bodies, trying this and that, using a so called technique of "try and see".

Here is what I discovered during these days of using Bullet. There is the explanation, and some remarks.

There are two ways to do collision between rigidBody and soft body (that I discovered):
1. register only VF_SS, but in such case no btGImpactCollisionAlgorithm must be registered, otherwise no collision between soft and rigid from mesh will be done. Also no clusters must be used. Better simulation. No collision between concave rigid bodies will be present.
2. register CL_SS + CL_RS, generate clusters, and m_kLST = 1.0f (material) must be used. btGImpact collision algorithm can be used, thus all objects will collide. Worse soft body simulation than in the point1, but maybe this is because I used the same parameter values for the softBody configuration and material (better not from the point of performance, but visual results, because this second method is faster).

1. Every object in the scene must have the mass set depending on the volume of the object, but how to calculate this for the models that are imported from different model files, I don't know. (So it's up to you to decide, at import, the mass, based on the stability of the simulation)
2. The value of the force applied to the soft body must be based on the mass of the whole object. Otherwise the simulation may become unstable, objects blow up and disappear.
3. Bullet mixes up my vertex indexes at the creation of the softBody from a trimesh, so if I want to apply a texture on the softBody, it will not be applied correctly. I didn't figure out for the moment how to correct this. The only thing I can think about is to create a map, and to map every vertex of the softBody to the original index, but this will cost too mush computation performance. (The result of this can be observed in the video below)
4. Collision between softBody and other bodies is dependent of the tessellation, so you must find what's the best value for your case.
5. I must use collision margin for the soft bodies and rigid bodies of 0.5; I would like to know the relationship of the collision margin to the simulation stability, since if I leave collision margin = default, my soft bodies blow up. (No one on the bullet forum replied to this, so it's disappointing, and no solution for my case, of course, if I would spend some weeks trying to figure out what's the relationship, maybe I'll find it).
6. I can't move the soft body as I do in the case of rigid bodies, especially in the case of torque. There is no such functionality to get the current velocity of the entire soft body (we can access the velocity for every single node instead), and we can't apply a torque on the body, as in the case of the rigid bodies. In the case of the movement, Hooke's law and damping effect do they work.

And here is what I achieved (The application performance is very low because the haptic rendering, graphic rendering and simulation is done on the same thread. Using glut, that's the problem. Must change.):

Wednesday, April 6, 2011

Hanoi towers using OpenHaptics, OpenGL, Bullet and Assimp

At the moment I do my professional training, and I'm developing an application with haptics. I created a scene with Hanoi Towers, to demonstrate what I achieved. Currently, it runs OK, but I have problems like:
1. Low performance when objects collide (about 7-9 FPS). I use btGImpactMeshShape for all the objects in my scene, except walls.
2. I can't use Scheduler in OpenHaptics, well in fact I can, but program crashes when I try to shut down it properly. I use HL and HD at the same time. OpenHaptics documentation is very poor (v.3) at this subject and not only , so there are many problems also that cause me a headache.
3. I have also problems with collision detection in Bullet, since there are cases (and often), when I can push the objects in my scene through others. My world is a closed box, so even if theoretically must not allow objects to pass the walls, I can force them (by pushing with the arm, or turning simulation off, applying a force and then turning simulation on again) to pass the walls of the box, so they might fall into abyss :D.

Well, since I'm a beginner in all this, I'm satisfied to know that I managed to do at least this. Future work will be on soft objects. I would like to thank (Since the name of the author is not displayed on the blog, I would not publish it) for the code that it made public, and that I used as a reference to understand how OpenHaptics and Bullet can work together.
And here's my demo video: