The Artima Developer Community
Sponsored Link

Java Answers Forum
Directory Encryption-file list problem and error

6 replies on 1 page. Most recent reply: Sep 9, 2003 5:53 AM by Greg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Sanjay Gurav

Posts: 1
Nickname: sans
Registered: May, 2002

Directory Encryption-file list problem and error Posted: May 23, 2002 1:07 PM
Reply to this message Reply
Advertisement
hi,
I am trying to do directory encryption. But it is giving error while enlisting files as (Java.lang.FileDefNotFoundError). I am sending program herewith. Please guide me in this.

Program:


import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.io.File;
/**
* This class encrypts and decrypts a file using CipherStreams
* and a 256-bit Rijndael key stored in the filesystem.
*/

public class FileEncrypto
{

private static String KEY_FILENAME="rijndaelkey.bin";
private static int ITERATIONS=1000;
String dirname="/Bin";

public static void main (String[] args)
throws IOException
{
System.err.println("check the error");
}

/**
* Creates a 256-bit Rijndael key and stores it to
* the filesystem as a KeyStore.
*/
private static void createKey(password)
throws Exception
{
// System.out.println("Generating a Rijndael key...");

// Create a Rijndael key
KeyGenerator keyGenerator = KeyGenerator.getInstance("Rijndael");
keyGenerator.init(256);
Key key = keyGenerator.generateKey();


// System.out.println("Done generating the key.");

// Now we need to create the file with the key,
// encrypting it with a password.
byte[] salt = new byte[8];
SecureRandom random = new SecureRandom();
random.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(
"PBEWithSHAAndTwofish-CBC");
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

// Encrypt the key
byte[] encryptedKeyBytes = cipher.doFinal(key.getEncoded());

// Write out the salt, and then the encrypted key bytes
FileOutputStream fos = new FileOutputStream(KEY_FILENAME);
fos.write(salt);
fos.write(encryptedKeyBytes);
fos.close();

}

/**
* Loads a key from the filesystem
*/
private static Key loadKey( password)
throws Exception
{
// Load the bytes from the encrypted key file.
FileInputStream fis = new FileInputStream(KEY_FILENAME);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while ((i=fis.read()) != -1) {
baos.write(i);
}
fis.close();
byte[] saltAndKeyBytes = baos.toByteArray();
baos.close();

// get the salt, which is the first 8 bytes
byte[] salt = new byte[8];
System.arraycopy(saltAndKeyBytes,0,salt,0,8);

// get the encrypted key bytes
int length = saltAndKeyBytes.length - 8;
byte[] encryptedKeyBytes = new byte[length];
System.arraycopy(saltAndKeyBytes,8,encryptedKeyBytes,0,length);

// Create the PBE cipher
PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(
"PBEWithSHAAndTwofish-CBC");
SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, ITERATIONS);
Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
cipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);

// Decrypt the key bytes
byte[] decryptedKeyBytes = cipher.doFinal(encryptedKeyBytes);

// Create the key from the key bytes
SecretKeySpec key = new SecretKeySpec(decryptedKeyBytes, "Rijndael");
return key;

}

/**
* Encrypt a file using Rijndael. Load the key
* from the filesystem, given a password.
*/
private static void encrypt(char [] password, Inputdirectory, OutPutdirectory)
throws Exception
{
System.out.println("Loading the key.");
Key key = loadKey(password);
System.out.println("Loaded the key.");

// Create a cipher using that key to initialize it
Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");

System.out.println("Initializing SecureRandom...");

// Now we need an Initialization Vector for the cipher in CBC mode.
// We use 16 bytes, because the block size of Rijndael is 256 bits.
SecureRandom random = new SecureRandom();
byte[] iv = new byte[16];
random.nextBytes(iv);

// Enlist the files in the directory
if (f1.isdirectory() )
{
String s[]=f1.list();
for (int i=0; i<s.length; i++)
{
File f = new file (s);
FileInputStream fis = new FileInputStream(s);
FileOutputStream fos = new FileOutputStream(s);

// Write the IV as the first 16 bytes in the file
fos.write(iv);
IvParameterSpec spec = new IvParameterSpec(iv);

System.out.println("Initializing the cipher.");

cipher.init(Cipher.ENCRYPT_MODE, key, spec);

CipherOutputStream cos = new CipherOutputStream(fos, cipher);

System.out.println("Encrypting the file...");

int theByte = 0;
while ((theByte = fis.read()) != -1)
{
cos.write(theByte);
}
fis.close();
cos.close();
}

// Else println ("F1 is not a directory");
}


/**
* Decrypt a file using Rijndael. Load the key
* from the filesystem, given a password.
*/
private static void decrypt(char [] password, BIN, BIN)
throws Exception
{
System.out.println("Loading the key.");
Key key = loadKey(password);
System.out.println("Loaded the key.");

// Create a cipher using that key to initialize it
Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5Padding");

// Enlist the files in the directory and decrypt
String m[]=f1.list();
for (int j=0; j<m.length; j++)
{
FileInputStream fis = new FileInputStream(m[j]);
FileOutputStream fos = new FileOutputStream(m[j]);


// Read the IV from the file. It's the first 16 bytes.
byte[] iv = new byte[16];
fis.read(iv);

IvParameterSpec spec = new IvParameterSpec(iv);

System.out.println("Initializing the cipher.");
cipher.init(Cipher.DECRYPT_MODE, key, spec);

CipherInputStream cis = new CipherInputStream(fis, cipher);

System.out.println("Decrypting the file...");

int theByte = 0;
while ((theByte = cis.read()) != -1)
{
fos.write(theByte);
}
cis.close();
fos.close();
}
}
// Else Println("f1 is not a diretory");
}
}
}





Thanks,

Sanjay Gurav


Ravetier

Posts: 2
Nickname: fjravetier
Registered: Sep, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 1:28 AM
Reply to this message Reply
Hello,

I need help to use the Rijndael java implementation.
Could you help me please?

Thanks,
Fred.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 1:52 AM
Reply to this message Reply
Fred, I don't see how this is related to this topic.

David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 1:52 AM
Reply to this message Reply
Sanjay,

Please post the Exception stack trace.

Ravetier

Posts: 2
Nickname: fjravetier
Registered: Sep, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 1:58 AM
Reply to this message Reply
You used the Rijndael algo and i need to encrypt a String with this algo.

Could you give a piece of code where you give a String, encrypt and decrypt?

I tryied with the jar found at http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ and I did something like :

public static void main(String[] args){
try{
int keysize=24;
String in=args[0];
byte[] kb = new byte[keysize];
for (int i = 0; i < keysize; i++) kb = (byte) i;
System.out.println("[CryptoRijndael(...)] : Before encoding in = "+in+" .length="+in.length());
byte[] inByte = in.getBytes();
System.out.println("[CryptoRijndael(...)] : Before encoding inByte = "+toString(inByte)+". .length="+inByte.length);
int blockSize=in.length();
System.out.println("[CryptoRijndael(...)] : keysize = "+keysize+" ; blockSize = "+blockSize);

Object key=Rijndael.Rijndael_Algorithm.makeKey(kb,blockSize);
System.out.println("[CryptoRijndael(...)] : key = "+toString(key));

byte[] inValueByteEnc = Rijndael.Rijndael_Algorithm.blockEncrypt(inByte, 0, key,blockSize);
String inValueEnc=new String(inValueByteEnc);
System.out.println("[CryptoRijndael(...)] : After encoding : "+inValueEnc+". .length="+inValueEnc.length());
//Test decoding : BEGIN
byte[] decByte=inValueEnc.getBytes();
byte[] outValueByte=Rijndael.Rijndael_Algorithm.blockDecrypt(decByte,0,key,blockSize);
String out=new String(outValueByte);
System.out.println("[CryptoRijndael(...)] : Decoding : "
+out+". .length="+out.length());
//Test decoding : END
}catch(Exception e){
System.out.println("Error : "+e);
Debug.err(e);
}
}

----------------
But when I try it, I get :
1- The String length is 24, the encryption is OK.
C:\>java test.Rijndael.CryptoRijndael 123456789012345678901234
[CryptoRijndael(...)] : Before encoding in = 123456789012345678901234
.length=24
[CryptoRijndael(...)] : Before encoding inByte = 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 . .length=24
[CryptoRijndael(...)] : keysize = 24 ; blockSize = 24
[CryptoRijndael(...)] : key = [Ljava.lang.Object;@8786b
Fÿ&#9500;éN¼&#9788;d. .length=24] : After encoding : Ô0xn#&#8616;2 Z&#9492;&#9644;&#8962;æg)
[CryptoRijndael(...)] : Decoding : 123456789012345678901234. .length=24

2-The String length is 25 char and the result is cut.
C:\>java test.Rijndael.CryptoRijndael 1234567890123456789012345
[CryptoRijndael(...)] : Before encoding in = 1234567890123456789012345
.length=25
[CryptoRijndael(...)] : Before encoding inByte = 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57 48 49 50 51 52 53 . .length=25
[CryptoRijndael(...)] : keysize = 24 ; blockSize = 25
[CryptoRijndael(...)] : key = [Ljava.lang.Object;@8786b
Fÿ&#9500;éN¼&#9788;d . .length=25 : After encoding : Ô0xn#&#8616;2 Z&#9492;&#9644;&#8962;æg)
[CryptoRijndael(...)] : Decoding : 123456789012345678901234 . .length=25

3-The String lenght is 23 chars and the result is not decrypted.
C:\>java test.Rijndael.CryptoRijndael 12345678901234567890123
[CryptoRijndael(...)] : Before encoding in = 12345678901234567890123
.length=23
[CryptoRijndael(...)] : Before encoding inByte = 49 50 51 52 53 54 55 56 57
48 49 50 51 52 53 54 55 56 57 48 49 50 51 . .length=23
[CryptoRijndael(...)] : keysize = 24 ; blockSize = 23
[CryptoRijndael(...)] : key = [Ljava.lang.Object;@8786b
[CryptoRijndael(...)] : After encoding : &#9580;[G½r-]&#9496;°¨PÝ&#9619;Z8Qï&#9474;&#8593;= . .length=23
[CryptoRijndael(...)] : Decoding : ·&#9632;Äs[èå÷û7¦&#9552;7?¾Nå»&#9787;¥ . .length=23



Thanks for your help.
Fred.

Greg

Posts: 18
Nickname: singleton
Registered: Jun, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 5:51 AM
Reply to this message Reply
You might take a look at the cryptix library.
I has some tests included that might shed some light
on the problem.

www.cryptix.org

Greg

Posts: 18
Nickname: singleton
Registered: Jun, 2003

Re: Directory Encryption-file list problem and error Posted: Sep 9, 2003 5:53 AM
Reply to this message Reply
You might take a look at the cryptix library.
I has some tests included that might shed some light
on the problem.

www.cryptix.org

Flat View: This topic has 6 replies on 1 page
Topic: Interview questions Previous Topic   Next Topic Topic: ASCII value to character

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use