dcsimg
Problem in decrypting encrypted data saved in a file
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, June 9, 2004 01:28 AM

Encrypting a string message and then decrypting it again works fine in following program. Encrypting data read from file also works fine but decrypting encrypted data read from file not works and throws "IllegalBlockSizeException"........ here's the code : import javax.crypto.*; import javax.crypto.spec.*; import java.security.*; import java.security.spec.*; import java.io.*; import sun.misc.*; public final class SecurityManager { private static String byteEncoding = "UTF8"; public static byte[] iv = new byte[] { (byte)0x8E, 0x12, 0x39, (byte)0x9C, 0x07, 0x72, 0x   More>>

Encrypting a string message and then decrypting it again works fine in following program. Encrypting data read from file also works fine but decrypting encrypted data read from file not works and throws "IllegalBlockSizeException"........



here's the code :



			
import javax.crypto.*;

import javax.crypto.spec.*;

import java.security.*;

import java.security.spec.*;

import java.io.*;

import sun.misc.*;



public final class SecurityManager

{

private static String byteEncoding = "UTF8";

public static byte[] iv = new byte[] { (byte)0x8E, 0x12, 0x39, (byte)0x9C, 0x07, 0x72, 0x6F, 0x5A };

public static AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

public static Cipher ecipher, dcipher;



public static void main(String s[])

{

try

{

byte plainText[] = "My name is HUMAYUN".getBytes();

byte encryptedText[];

String plainTextFileName = "c:\plainText.txt";

String encryptedTextFileName = "c:\encryptedText.txt";

String algo = "DES";



SecretKey key = getSecretKey(algo, 56, "SHA1PRNG");



System.out.println("Plain Text : " + plainText);

encryptedText = encrypt(plainText, algo, key);

System.out.println("Encrypted Text : " + new BASE64Encoder().encode(encryptedText));

System.out.println("Decrypted Text : " + new String(decrypt(encryptedText, algo, key)));



plainText = readBytesFromFile(plainTextFileName);

System.out.println("Plain Text : " + plainText);

encryptedText = encrypt(plainText, algo, key);

System.out.println("Encrypted Text : " + new BASE64Encoder().encode(encryptedText));

System.out.println("Decrypted Text : " + new String(decrypt(encryptedText, algo, key)));



plainText = readBytesFromFile(plainTextFileName);

System.out.println("Plain Text : " + plainText);

encryptedText = encrypt(plainText, algo, key);

System.out.println("Encrypted Text : " + new BASE64Encoder().encode(encryptedText));

saveBytesToFile(encryptedTextFileName,encryptedText);

encryptedText = readBytesFromFile(encryptedTextFileName);

System.out.println("Decrypted Text : " + new String(decrypt(encryptedText, algo, key)));

}

catch(Exception e)

{

System.out.println("ERROR : " + e.toString() + " - " + e.getMessage());

e.printStackTrace(System.out);

}

}



public static byte[] encrypt (byte plainText[], String algo, SecretKey key) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException,
InvalidAlgorithmParameterException

{

ecipher = Cipher.getInstance(algo);

ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

return ecipher.doFinal(plainText);

}



public static byte[] decrypt (byte cipherText[], String algo, SecretKey key) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException,
InvalidAlgorithmParameterException

{

dcipher = Cipher.getInstance(algo);

dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

return dcipher.doFinal(cipherText);

}



public static SecretKey getSecretKey(String keyGenAlgo, int keySize, String prngAlgo) throws NoSuchAlgorithmException,
UnsupportedEncodingException, InvalidKeyException, InvalidKeySpecException

{

KeyGenerator keyGen = KeyGenerator.getInstance(keyGenAlgo);

keyGen.init(keySize, SecureRandom.getInstance(prngAlgo));

return keyGen.generateKey();

}



public static void saveBytesToFile(String fileName, byte bytes[]) throws IOException

{

System.out.println("
==> saving bytes to '" + fileName + "' start.");

FileWriter writer = new FileWriter(fileName);

writer.write(new String(bytes));

writer.flush();

writer.close();

System.out.println("==> data wrote : " + new String(bytes));

System.out.println("==> saving bytes to '" + fileName + "' end.");

}



public static byte[] readBytesFromFile(String fileName) throws IOException

{

System.out.println("
==> reading bytes from '" + fileName + "' start.");

FileReader reader = new FileReader(fileName);

String data = "";

for(int aChar = reader.read(); aChar != -1; aChar = reader.read())

data += (char)aChar;

reader.close();

System.out.println("==> data read :
" + data);

System.out.println("==> reading bytes from '" + fileName + "' end.");

return data.getBytes(byteEncoding);

}

}

   <<Less

Re: Problem in decrypting encrypted data saved in a file

Posted By:   Chandrakala_Bairagoni  
Posted On:   Friday, July 23, 2004 11:14 PM

how i can encrypt and decrypt .txt file

Re: Problem in decrypting encrypted data saved in a file

Posted By:   Chandrakala_Bairagoni  
Posted On:   Friday, July 23, 2004 11:13 PM

how i can encrypt and decrypt .txt file
About | Sitemap | Contact