Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Still having problems with reading files

7 views
Skip to first unread message

George

unread,
Jan 12, 2003, 10:53:08 AM1/12/03
to
Ok - so if i us a "FileInputStream" and then use the "read()" method to read
byte by byte,
the read method returns and "int". what do i do with that int in order to
update my message digest.

myDigest.update(myByte); // where do i get the myByte variable from. there
is not method for to "getBytes[]" from an interger.

Im maybe making more of this than i should be. Anyway - if someone could
give me some sample code to look at i would really appriciate it.

thanks


Michael Amling

unread,
Jan 12, 2003, 12:16:57 PM1/12/03
to
George wrote:
> Ok - so if i use a "FileInputStream" and then use the "read()" method to
> read byte by byte,

You needn't read byte-by-byte. E.g.,
MessageDigest msgdig=..whatever..;
InputStream filein=..whatever..;
byte[] buffer=new byte[1024];
int howManyBytes;
while ((howManyBytes=filein.read(buffer))>0) {
msgdig.update(buffer, 0, howManyBytes);
}

> the read method returns an "int". what do i do with that int in order to


> update my message digest.
>
> myDigest.update(myByte); // where do i get the myByte variable from. there

> is not method for to "getBytes[]" from an integer.

If the int is -1, it indicates end of file. Otherwise, the int will
be in the range 0..255, and you cast it to a byte. E.g.,

int datum;
while ((datum=filein.read())>=0) {
msgdig.update((byte)datum);
}

HTH,

--Mike Amling

George

unread,
Jan 12, 2003, 1:29:21 PM1/12/03
to
The below code is a simple RSA encryption / decryption issue
I can get the file to encrypt - but I get the following error in my compiler
which seems to be an issue with the decryption.

the error is on line 50 - which is where the cipher is being used to decrypt
the file. I really dont know if what im outputting as an encrypted file
is correct so therefore the input would of course not be correct. Im brand
new to this so any help would be appriciated. And once i get a bit better
at this ill start helping other people on the board out - cheers guys.

Ive included my code (& compiler output) and im using the bouncy castle
providor which seems itself to be working ok.
I did get an RSA example working just using a simple short string, but i
dont want
to work with strings because I want to be able to encrypt and decrypt files
of any type.

--Mike Amling - has helped me out quite a bit with and if u see this i just
wanted to thank you for everything thus far.


//START - OF COMPILER OUTPUT //
Start generating RSA key
Finish generating RSA key

Start encryption

Finished encryption

Start decryption
javax.crypto.BadPaddingException: unknown block type
at
org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(JCERSACipher.java:2
25)
at javax.crypto.Cipher.doFinal(DashoA6275)
at FileRSATest.main(FileRSATest.java:50)
Exception in thread "main"

/******************************************************MY
CODE********************************************8
import java.io.*;
import java.security.*;
import javax.crypto.*;
import sun.misc.*;


public class FileRSATest {

public static void main (String[] args) throws Exception
{
// generate an RSA key
System.out.println( "\nStart generating RSA key" );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
System.out.println( "Finish generating RSA key" );

// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

FileInputStream fs = new FileInputStream("c:/SPADE/test.txt");
FileOutputStream os = new FileOutputStream("c:/SPADE/test.enc");
//os.
byte[] buffer=new byte[64];
int howManyBytes;
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
// encrypt the plaintext using the public key
System.out.println( "\nStart encryption" );
while ((howManyBytes=fs.read(buffer))>0) {


os.write(cipher.doFinal(buffer));

}

System.out.println( "\nFinished encryption" );


FileInputStream ef = new FileInputStream("c:/SPADE/test.enc");
FileOutputStream df = new
FileOutputStream("c:/SPADE/decrypted.txt");
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
System.out.println( "\nStart decryption" );
while ((howManyBytes=ef.read(buffer))>0) {


df.write(cipher.doFinal(buffer));

}
// decrypt the ciphertext using the private key

System.out.println( "\nFinished decryption" );
}
}


0 new messages