Message Digest Error (plz help)
843851May 13 2004 — edited May 18 2004Hi,
I am from India and working on Gemplus Java cards (211 PK) . and using the Gemplus RAD III Kit 3.2
For Java card Application development (key pair generation and Message DIgest) , I am using the Javacard.security package and MessageDigest class for creating the message digest.
But I am facing some problems.
So, I am attaching the .java File. The source Code gets compiled properly but raises a Null Pointer Exception at RunTime (See attached text file) .
Could you please tell me what the error is?
Being in India, do these cards support Limited Cryptography or is Message Digesting allowed.
My .java file (i.e. My source code)
//-----------------------------------------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.io.FileInputStream;
import javacard.security.MessageDigest;
import javacard.security.CryptoException;
class MsgDigest
{
public static void main(String args[])
{
FileInputStream fIn; // File Input stream
FileOutputStream fOut; // File output stream
boolean resRead; //result of "Is file readable?"
int resCompare; //resultof comparing 2 file's equality
String fNm,temp; //open file name,temparary variable for other testing
long fLength; //open file length
int resReadFile;// result of reading the file through read()
int fileLength=0; //file Length in int
int cnt=0;
try
{
//---------------------------------------------------------------
//open the file on which message digest is to be performed
fNm=new String("c:\\sample.txt");
File fSamp=new File(fNm);
//checking it for reading operation
resRead=fSamp.canRead();
System.out.println(resRead);
//----------------------------------------------------------------
//creating input stream of file and writting it into another file output.txt
fIn=new FileInputStream(fNm);
fLength=fNm.length();
System.out.println("in long file size is : " fLength"\n");
fOut= new FileOutputStream("C:\\output.txt");
//----------------------------------------------------------------
//reading the file and calculating the length
do
{
resReadFile=fIn.read();
if (resReadFile!=-1)
{
fOut.write(resReadFile);
fileLength=fileLength+1;
}
}while (resReadFile!=-1);
fIn.close();
//----------------------------------------------------------------
//reading the file and storing it in a byte array
fIn=new FileInputStream(fNm);
System.out.println("in int File size is : "+fileLength+ "\n");
byte readData[]=new byte[fileLength]; //data in file stored in this byte array
int i;
i=fIn.read(readData);
System.out.println("bytes in readData : "+i+"\n");
//------------------------------------------------------------------
//creating message digest object and update message Digest to work on our message : readData
byte midData[]=new byte[fileLength];
MessageDigest md;
try
{
md = MessageDigest.getInstance(MessageDigest.ALG_SHA,false);
}
catch(Exception e1)
{
e1.printStackTrace();
}
//md.update(readData,(short)0,(short)(readData.length));
//md.doFinal(readData,(short)0,(short)(readData.length),midData,(short)0);
//---------------------------------------------------------------------------------------------
}//try
catch (CryptoException e)
{
System.err.println("ERROR : "+e);
System.exit(1);
}
catch (FileNotFoundException e)
{
System.err.println("ERROR : "+e);
System.exit(1);
}
catch (ArrayIndexOutOfBoundsException e)
{
System.err.println("ERROR : "+e);
System.exit(1);
}
catch (Exception e)
{
System.err.println("ERROR : "+e);
System.exit(1);
}
}//main
}//class
//-----------------------------------------------------------------------------------------------------------------------------------------------
Getting THIS error
true
in long file size is : 13
in int File size is : 241
bytes in readData : 241
javacard.security.CryptoException
at javacard.security.CryptoException.throwIt(Unknown Source)
at javacard.security.MessageDigest.getInstance(Unknown Source)
at MsgDigest.main(MsgDigest.java, Compiled Code)
Press any key to continue...