Java自检程序(self-checksuming)

发布于 2024-10-09 17:24:09 字数 1317 浏览 8 评论 0原文

我必须做一些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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无名指的心愿 2024-10-16 17:24:09

也许您应该查看签名的 jar 文件

Maybe you should take a look at signed jar files.

清眉祭 2024-10-16 17:24:09

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

然后在运行时,您将需要加载该校验和,这将是您的预期结果。然后像您在代码中所做的那样对“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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文