我必须做一些java自检程序(self-checksum)。下面是我的代码示例
public class tamper {
public static int checksum_self () throws Exception {
File file = new File ("tamper.class");
FileInputStream fr = new FileInputStream (file);
int result; // Compute the checksum
return result;
}
public static boolean check1_for_tampering () throws Exception {
return checksum_self () != 0; // TO BE UPDATED!
}
public static int check2_for_tampering () throws Exception {
return checksum_self ();
}
public static void main (String args[]) throws Exception {
if (check1_for_tampering ()) {
System.exit (-1);
}
float celsius = Integer.parseInt (args[0]);
float fahrenheit = 9 * celsius / 5 + 32;
System.out.println (celsius + "C = " + fahrenheit + "F");
}
}我试过做这样的事情
DigestInputStream sha = new DigestInputStream(fr, MessageDigest.getInstance("SHA"));
byte[] digest = sha.getMessageDigest();
for(..)
{
result = result + digest[i]
}但我真的不知道该怎么检查??
编辑:
感谢您的回答
@ Adam Paynter和SpoonBenders
当然,这不是我个人使用的。我不会用它来保护任何软件……
这是我的java课程必须做的“练习”……
发布于 2010-12-29 20:00:23
也许你应该看看signed jar files。
发布于 2010-12-29 19:45:39
要进行检查,首先需要创建一个校验和。可能直接在编译之后,你需要在校验和创建模式下运行你的程序,该模式将创建校验和并将其存储在一个文件中或其他你可以在运行时访问的地方。
然后在运行时,您将需要加载该校验和,这将是您的预期结果。然后对"tamper.class“执行校验和,就像您在代码中所做的那样,并检查它是否与预期结果匹配。
这就是你想要做的吗?
https://stackoverflow.com/questions/4553726
复制相似问题