Java自检程序(自检程序)
我必须做一个java自检程序(自检和)。 这是我的代码示例Java自检程序(自检程序),java,checksum,self,Java,Checksum,Self,我必须做一个java自检程序(自检和)。 这是我的代码示例 public class tamper { public static int checksum_self () throws Exception { File file = new File ("tamper.class"); FileInputStream fr = new FileInputStream (file); int result;
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]
}
但我真的不知道如何检查
编辑:
谢谢你的回答
@亚当·佩恩特和斯潘本德斯
当然这不是我个人用的。我不会用它来保护任何软件
这是我的java课程必须做的“练习”…要检查,首先需要创建一个校验和。可能在编译之后,您需要在校验和创建模式下运行程序,该模式将创建校验和并将其存储在文件或运行时可以访问的其他位置 然后在运行时,您将需要加载该校验和,这将是您的预期结果。然后像在代码中一样对“tamper.class”执行校验和运算,并检查它是否与预期结果匹配
这就是您想要做的吗?也许您应该看看。请注意java方法名称标准。这不是为了个人利益吗?我不会依赖这种技术来保护代码不被修改。如果您的代码纯粹在客户端运行,那么很难保护。另一方面,有些软件只有在互联网连接可用时才有意义。这样的软件很容易得到有效的保护:只需在服务器端(您完全控制的服务器端)进行部分计算即可。想一想:没有有效的许可证,没有人能在魔兽世界经济/暴雪服务器上玩。没有人没有一个海盗组织能够解决这个问题。这就是强大的客户机/服务器保护功能。@SpoonBender:完全同意,事实上,我甚至可以说,如果整个应用程序都在客户机上,就不可能进行保护。当然,您可能会使其变得非常困难,但任何机构的所有组件的访问最终都可能被绕过或禁用。