Search on this blog

Monday, November 5, 2007

Encryption is not only from plain text to cipher text

The encryption definition you read in the text book, I cant say that is wrong but that is old. Basically what we mean by encryption is change a readble file to unreadable format and in text book you will see plain-text->Cipher-text etc. But have you ever tried to encrypt a binary file. This is not so tough use the same algorithm and it will produce a encrypted file. Try it yourself and if you stuck up do a google and search for the algos and codes. This is very much important for data transferring in this generation through internet. A sample java code is given below, dont copy it ;).



private void doEncryption(final StringfilePath, final String outString,
final byte[] keyBytes) throws EncryptException {
FileOutputStream fos = null;
CipherOutputStream cOut = null;

try {

final File file = new File(filePath);
final byte[] fileByte = getBytesFromFile(file);
final String transformation = EncryptUtilConst.TRANSFORMATION;
final Cipher encrypt = Cipher.getInstance(transformation);

final SecretKeySpec skeySpec = new SecretKeySpec(keyBytes,
EncryptUtilConst.JAR_ENC_ALG);

final byte[] ivByte = new byte[] { (byte) 0x2A, (byte) 0x98,
(byte) 0xB2, (byte) 0xBA, (byte) 0x99, (byte) 0xC0,
(byte) 0xF1, (byte) 0x22, (byte) 0x72, (byte) 0x54,
(byte) 0xBC, (byte) 0xB5, (byte) 0x34, (byte) 0x2F,
(byte) 0xBA, (byte) 0xC4 };

encrypt.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(
ivByte));

fos = new FileOutputStream(outString);

cOut = new CipherOutputStream(fos, encrypt);

// Your plaintext MUST be multiple of 16 bytes because NoPadding
// requires your input text
// to be a multiple of block size.
cOut.write(fileByte);

// check for successful encryption of the algorithm
cOut.close();

} catch (Exception e) {
throw new EncryptException(e.getMessage());
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new EncryptException(e.getMessage());
}
}
if (cOut != null) {
try {
cOut.close();
} catch (IOException e) {
throw new EncryptException(e.getMessage());
}
}
}
}

0 comments:

Post a Comment

Featured Video

Featured Article

Poems