Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Tuesday, April 7, 2009

Simple JAVA 3DES application

Because of there are lots of request for this application..I surrender..hehe..here are the codes..i will not hold responsible if something happens to your assignment..hehe..this is for education only..there are some simple errors..so watch out..you can also donate some $$ in my paypal..hehe..

ATTENTION to all lecturer, plz check you students JAVA projects related to 3des cause the student might be using this code as their work...haha..






*************************

Tuesday, May 6, 2008

AES : JAVA coding

Hello..it's been a long time since my last post. Actually i am a bit busy with my project going to ITEX exhibition this friday.

Now i am going to share with you all an example of AES encryption. This AES encryption used password from user as a key. Firstly, let me tell you more about AES.

Strictly speaking, AES is not precisely Rijndael (although in practice they are used interchangeably) as Rijndael supports a larger range of block and key sizes; AES has a fixed block size of 128 bits and a key size of 128, 192, or 256 bits, whereas Rijndael can be specified with key and block sizes in any multiple of 32 bits, with a minimum of 128 bits and a maximum of 256 bits.

Due to the fixed block size of 128 bits, AES operates on a 4×4 array of bytes, termed the state (versions of Rijndael with a larger block size have additional columns in the state). Most AES calculations are done in a special finite field. [source: wikipedia]
PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator();

byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99,
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

int count = 16;

IvParameterSpec iv = new IvParameterSpec(salt);

generator.init(pBytes, salt, count);

ParametersWithIV params = (ParametersWithIV)
generator.generateDerivedParameters(128, 128);

KeyParameter keyParam = (KeyParameter) params.getParameters();

SecretKeySpec key = new SecretKeySpec(keyParam.getKey(), "AES");

String password = "secretkey";
String PT = "Attack at dawn";

byte[] pBytes = password.getBytes();

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC");
cipher.init(Cipher.ENCRYPT_MODE,key,iv);

byte[] cipherText = cipher.doFinal(PT.getBytes("ASCII"));
Base64 b64 = new Base64();

String b64ctxt = new String(Base64.encode(cipherText), "ASCII");
String b64key = new String(Base64.encode(key.getEncoded()), "ASCII");

System.out.println("Ciphertext = " + b64ctxt);
System.out.println("B64 key = " + b64key);

byte[] newCtxt = Base64.decode(b64ctxt);
cipher.init(Cipher.DECRYPT_MODE, key,iv);
byte[] plainBytes = cipher.doFinal(newCtxt);
String newPlainText = new String(plainBytes, "ASCII");
System.out.println("Plaintext = [" + newPlainText + "]");

Thats all folks..Hope this would help. Just drop a comment if something is not right about the coding.

Friday, April 4, 2008

DES : JAVA

Today i am gonna post about DES aka Data Encryption Standard. Actually it's a little not-up-to-date if you are using this type of algorithm. But for education purpose it's ok..hee..My final year project used this algorithm cause it's the SUN JCE have it's library built in it. First we need to know what is DES.

DES used 56 bits key and used 64 bit block size. I take my Cryptography subject this year so it's really an advantage for me for my final year project which has been successfully done. DES considered not save anymore from attack because of the key length. They already develope a machine called COPACOBANA which used to key search for DES for 6.4 days.

The process are really complicated..trust me, i have don't the calculation for DES during my Cryptography subject..hate math=p hehe..below is the coding that i used for DES. Actually it's a Password Based Encryption or PBE for DES. It used Cipher Block Chaining mode and padding.
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;

// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

// Iteration count
int count = 20;

// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);

String pass = "my secret";

try {

keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher;
pbeCipher = Cipher.getInstance("PBEWithMD5AndDES/CBC/PKCS5Padding");
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

}

Anyway hope that the code above helps you all.have a nice days..wished me luck for my Cisco Network Competition Skills tomorrow.buh bye..

Monday, February 18, 2008

JAVA : Triple DES or DESede Algorithm

3DES3DES or Triple DES or DESede are well known encryption algorithm which are the "child" of DES(Data Encryption Standard). Although it was replace by AES (Advance Encryption Standard) still there are some people like me studies this old thing.

Triple DES is part of my final year project. I asked my lecturer if i perform DES three times, is it same like Triple DES. The answer is NO because Triple DES use different keys.

The keys use is 56 bit. I just finished a simple JAVA application on Triple DES. The application receive input and password from user. Then display the output which the user can choose between encrypt or decrypt.

If you want to try my newly developed JAVA application, you can download it here. If you want the sample of the code?!Please contact me at n a w u z a at g m a i l . c o m.


sample DES




Above is the screen shot of my JAVA application.