首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java自检程序(自检和)

Java自检程序(自检和)
EN

Stack Overflow用户
提问于 2010-12-29 19:36:40
回答 2查看 1.4K关注 0票数 2

我必须做一些java自检程序(self-checksum)。下面是我的代码示例

代码语言:javascript
复制
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");
      }
}

我试过做这样的事情

代码语言:javascript
复制
DigestInputStream sha = new DigestInputStream(fr, MessageDigest.getInstance("SHA"));
byte[] digest = sha.getMessageDigest();

for(..)
{
result = result + digest[i]
}

但我真的不知道该怎么检查??

编辑:

感谢您的回答

@ Adam Paynter和SpoonBenders

当然,这不是我个人使用的。我不会用它来保护任何软件……

这是我的java课程必须做的“练习”……

EN

回答 2

Stack Overflow用户

发布于 2010-12-29 20:00:23

也许你应该看看signed jar files

票数 1
EN

Stack Overflow用户

发布于 2010-12-29 19:45:39

要进行检查,首先需要创建一个校验和。可能直接在编译之后,你需要在校验和创建模式下运行你的程序,该模式将创建校验和并将其存储在一个文件中或其他你可以在运行时访问的地方。

然后在运行时,您将需要加载该校验和,这将是您的预期结果。然后对"tamper.class“执行校验和,就像您在代码中所做的那样,并检查它是否与预期结果匹配。

这就是你想要做的吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4553726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档