whats wrong with this code? import java.io.*; import java.security.*; class demo
ID: 3606488 • Letter: W
Question
whats wrong with this code?
import java.io.*;
import java.security.*;
class demo2
{
public static void main(String args[])
{
try
{
FileInputStream fis = new FileInputStream("demo1test");
ObjectInputStream ois = new ObjectInputStream(fis);
Object ob1 = ois.readObject();
String s1 = (String) ob1;
System.out.println(s1);
Object ob2 = ois.readObject();
byte[] array1= (byte[]) ob2;
MessageDigest md = MessageDigestgetInstance("SHA"); //i believe the error is on this line
md.update(s1.getBytes());
if(MessageDigest.isEqual(md.digest(), array1))
{ System.out.println("valid"); }
else
{ System.out.println("corrupted");
}
} catch(Exception e1)
{ System.out.println(""+e1);}
}
}
Explanation / Answer
import java.io.*;
import java.security.*;
class Main
{
public static void main(String args[])
{
try
{
FileInputStream fis = new FileInputStream("demo1test");
ObjectInputStream ois = new ObjectInputStream(fis);
Object ob1 = ois.readObject();
String s1 = (String) ob1;
System.out.println(s1);
Object ob2 = ois.readObject();
byte[] array1= (byte[]) ob2;
//You missed the . after MessageDigest
MessageDigest md = MessageDigest.getInstance("SHA"); //i believe the error is on this line
md.update(s1.getBytes());
if(MessageDigest.isEqual(md.digest(), array1))
{ System.out.println("valid"); }
else
{ System.out.println("corrupted");
}
} catch(Exception e1)
{ System.out.println(""+e1);}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.