Java自检程序(self-checksuming)
我必须做一些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 课程做的“练习”......
I have to do a little java self-checking programm (self-checksum).
here my code sample
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");
}
}
I have tried to do something like that
DigestInputStream sha = new DigestInputStream(fr, MessageDigest.getInstance("SHA"));
byte[] digest = sha.getMessageDigest();
for(..)
{
result = result + digest[i]
}
But then i don't really know how to check that ??
EDIT:
Thanks for your answers
@ Adam Paynter and SpoonBenders
Naturally this is not for my personal use. And I wont use that to protect any software.....
It's an "exercice" i have to do for my java course...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该查看签名的 jar 文件。
Maybe you should take a look at signed jar files.
要进行检查,您首先需要创建一个校验和。可能在编译后,您需要在校验和创建模式下运行程序,该模式将创建校验和并将其存储在文件或运行时可以访问的其他位置。
然后在运行时,您将需要加载该校验和,这将是您的预期结果。然后像您在代码中所做的那样对“tamper.class”执行校验和,并检查它是否与预期结果匹配。
这就是你想做的吗?
To check you first need to create a checksum. Possibly straight after compilation, you need to run you program in checksum creation mode that will create and store the check sum in a file or elsewhere where you can access at runtime.
Then at run time, you will need to load that checksum, which will be your expected result. Then perform checksumming on "tamper.class" as you have done in your code and check that it matches the expected result.
Is this what you are trying to do?