import java.security.*;
import java.io.*;public class SKey {
private static byte [][] K;
private static int n=100;
private static String k="first";
public static void main(String[] args) throws Exception{
byte [] s= k.getBytes();
K[0]=s;
SKey my=new SKey();
my.generateDigest();
int i=99,j=0,l=2;
byte[]pre;
byte []p;
pre=K[100];
p=K[99];
while(j==0&&l<=101&&i>0)
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("please input your choice: ");
System.out.println("1:enter into the system:");
System.out.println("2:end");
String value = input.readLine();
String str1=new String("1");
String str2=new String("2");
if (value.compareTo(str1)==0)
{
System.out.println("please input your name: " );
String name = input.readLine();
System.out.println("your number is: "+l);//l
l++;
{testDigest(p,pre);
pre=p;
}
i--;
p=K[i];
}
else
{
System.out.println("End! ");
j=1;
}
} }
public void generateDigest()
{
try {
int i=0;
while(i<n)
{
java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1");
alga.update(K[i]);
K[i+1]=alga.digest();
i++;
//use K[] to record the digest msgs
} }
catch (java.security.NoSuchAlgorithmException ex) {
System.out.println("System goes wrong!");
} }
public static void testDigest(byte [] myinfo,byte [] prei)
{
try{
java.security.MessageDigest algb=java.security.MessageDigest.getInstance("SHA-1");
algb.update(myinfo);
if (algb.isEqual(prei,algb.digest())) {
System.out.println("OK!");
}
else
{
System.out.println("Wrong!");
}
}
catch (java.security.NoSuchAlgorithmException ex) {
System.out.println("no!");
}
}
}
==============================
编译的时候 发现JAVA对动态二维数组不能简单的赋值,
如:private static byte [][] K;
byte [] s= k.getBytes();
K[0]=s;于是网上查了下,可以用vector来做,于是改用vector存储数组.
byte [] s= k.getBytes();
ArrayList array = new ArrayList();
array.add(s);
K.add(array); 但是调用vector数组的时候,直接读出是什么类型的数据呢?
byte[]pre;
byte []p;
pre=K[100];
p=K[99];
我这样试了下,是错误的,那我如果想读出byte数组型的数据该怎么办呢?
import java.io.*;public class SKey {
private static byte [][] K;
private static int n=100;
private static String k="first";
public static void main(String[] args) throws Exception{
byte [] s= k.getBytes();
K[0]=s;
SKey my=new SKey();
my.generateDigest();
int i=99,j=0,l=2;
byte[]pre;
byte []p;
pre=K[100];
p=K[99];
while(j==0&&l<=101&&i>0)
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.println("please input your choice: ");
System.out.println("1:enter into the system:");
System.out.println("2:end");
String value = input.readLine();
String str1=new String("1");
String str2=new String("2");
if (value.compareTo(str1)==0)
{
System.out.println("please input your name: " );
String name = input.readLine();
System.out.println("your number is: "+l);//l
l++;
{testDigest(p,pre);
pre=p;
}
i--;
p=K[i];
}
else
{
System.out.println("End! ");
j=1;
}
} }
public void generateDigest()
{
try {
int i=0;
while(i<n)
{
java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1");
alga.update(K[i]);
K[i+1]=alga.digest();
i++;
//use K[] to record the digest msgs
} }
catch (java.security.NoSuchAlgorithmException ex) {
System.out.println("System goes wrong!");
} }
public static void testDigest(byte [] myinfo,byte [] prei)
{
try{
java.security.MessageDigest algb=java.security.MessageDigest.getInstance("SHA-1");
algb.update(myinfo);
if (algb.isEqual(prei,algb.digest())) {
System.out.println("OK!");
}
else
{
System.out.println("Wrong!");
}
}
catch (java.security.NoSuchAlgorithmException ex) {
System.out.println("no!");
}
}
}
==============================
编译的时候 发现JAVA对动态二维数组不能简单的赋值,
如:private static byte [][] K;
byte [] s= k.getBytes();
K[0]=s;于是网上查了下,可以用vector来做,于是改用vector存储数组.
byte [] s= k.getBytes();
ArrayList array = new ArrayList();
array.add(s);
K.add(array); 但是调用vector数组的时候,直接读出是什么类型的数据呢?
byte[]pre;
byte []p;
pre=K[100];
p=K[99];
我这样试了下,是错误的,那我如果想读出byte数组型的数据该怎么办呢?
private static int n = 100;
private static byte[][] K = new byte[n+1][];