Sunday, 18 January 2009

RSA in C# and Java

Following up on the previous post, follows an implementation of RSA in C# and Java.

First the Java version that generates a public key:


KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(4096);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
RSAPublicKey rsaPublicKey = (RSAPublicKey)publicKey;
BigInteger publicExponent =rsaPublicKey.getPublicExponent();
BigInteger modulus = rsaPublicKey.getModulus();



with the public exponent and the modulus, you can export this key in an xml file or stream that will be used by the C# client to read the public key.

The C# part:


var rsaCryptoServiceProvider = new RSACryptoServiceProvider();
const string keyfileName = "PKI2.xml";
var streamReader = new StreamReader(@"C:\TEMP\RSAJavaPubKey.xml");

string publicKey = streamReader.ReadToEnd();
rsaCryptoServiceProvider.FromXmlString(publicKey);
streamReader.Close();



Now what?

Let's say you are using Rendez-Vous as a protocol and you are looking for a way to cipher messages between the client and the server. the Java server can generate a public key on start-up, initiate a handshake with the C# (or Java client). The client generates a symmetric key for DES-3 or AES, the client then encrypts the symmetric key with the server's public key, sends it to the server which deciphers the symmetric key, and then you have your SSL-like protocol on top of RV.

This obviously does not solve the man in the middle attack.

Thursday, 8 January 2009

AES in Java and C#

In a recent project I had to implement some symmetric encryption algorithm (AES) in both Java and C# and make sure that the Java implementation could encrypt/decrypt the data decrypted/encrypted with the C# version..
So..

In Java:


Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey secretKey = new SecretKeySpec(passwordKey, "AES");

Note passwordKey must be of 16 bytes in length for 128 bits encryption.

IvParameterSpec ivParameterSpec = new IvParameterSpec(rawSecretKey);

Then I decided to use Base 64 encoding for sharing data between C# and Java.
The cipher method is:

public String encryptAsBase64(byte[] clearData) throws Exception {
BASE64Encoder _64e = new BASE64Encoder();
byte[] encryptedData = encrypt(clearData);
return _64e.encode(encryptedData);
}

public byte[] encrypt(byte[] clearData) throws Exception {
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedData = aesCipher.doFinal(clearData);
return encryptedData;
}


Now, let's see how do decrypt in C#:


const string password = ...;
RijndaelManaged rijndael = new RijndaelManaged();
ICryptoTransform rijndaelDecryptor =
rijndael.CreateDecryptor(passwordKey, passwordKey);

Then

byte[] newClearData =
rijndaelDecryptor.TransformFinalBlock(cryptedData, 0, cryptedData.Length);


You can use the class Convert for Base 64 encoding.

Sunday, 4 January 2009

MacBook Hard Drive

May be I was too harsh on TM.. the issue is coming from the hard drive.
So I started to google 'replace' and the 6th suggestion was 'replace macbook hard drive'!!! That surely must tell you things about MacBook Quality Hard-Drives...

Saturday, 3 January 2009

Closures

Good intro to closures

Apple Time Machine

does not work. I had to re-install my Mac OS X 10.5 because the computer kept rebooting all the time on start-up.
I have tried repairing the disk, permissions, and the what-have-you-s I have found on the web... nothing.
So, knowing that I had a back-up, via Time Machine, I simply re-installed the OS... did a software upgrade.. then comes the Migration Utility Assistant..

I used the MUA (Utilities) and plugged my external hard-drive (Time Machine Backup), and after 1h, the whole computer froze again...it did not work. I re-installed everything again.... let the TM run for 6 hours, nothing...It did not work at all.

Mac products are over-rated. It is fine to add new functionalities to a new OS only if they do work... considering the number of TM issues on the web, Time Machine is not resilient.

I am seriously considering not using the Mac anymore, and simply forget about iTunes.

Blog Archive