lping9188
发贴: 0
积分: 0
|
于 2003-03-11 19:43
Hi, I want to implement one protocol, which used the verifciation: Be given a multiplicative of a random number and a signed data, then decrypt the multiplicative by using public key, the result should be equal to a multiplicative of the hash data and the encrypted data of the reandom number by public key. I made such a programme, if I replace hash value by another byte[] data,it success, but when use hash value, it always failed.
MessageDigest md= MessageDigest.getInstance("SHA-1"); md.update("data".getBytes()); byte[] hash= md.digest();
/* if I use that one replace the hash value, it success; byte[] hash="MessageDigest md= MessageDigest.getInstance;".getBytes();*/
Cipher c = Cipher.getInstance("RSA","BC"); c.init(Cipher.ENCRYPT_MODE,pri1); byte[] encrypted = c.doFinal(hash);
BigInteger encryptedbig=new BigInteger(encrypted);
byte[] r="6".getBytes(); //just take as a random number;
BigInteger rbig=new BigInteger(r);
BigInteger Xb=rbig.multiply(encryptedbig);
BigInteger XB=Xb.modPow(exp1,n1);
BigInteger XBm=XB.mod(n1); RSAKeyGenParameterSpec para=new RSAKeyGenParameterSpec(2048,exp1);
KeyPairGenerator kg2 = KeyPairGenerator.getInstance("RSA","BC"); kg2.initialize(para); KeyPair kp2 = kg2.generateKeyPair();
RSAPrivateCrtKey pri2 = ( RSAPrivateCrtKey) kp2.getPrivate();
RSAPublicKey pub2 = ( RSAPublicKey) kp2.getPublic();
BigInteger n2=pub2.getModulus();
BigInteger n3=n1.multiply(n2); BigInteger Yb=rbig.modPow(exp1,n3); BigInteger Ybb=rbig.modPow(exp1,n1);
BigInteger Yb1=Yb.mod(n1);
BigInteger hashbig=new BigInteger(hash);
BigInteger Ybbig=hashbig.multiply(Yb1);
BigInteger Ybbb=Ybbig.mod(n1);
if(XBm.equals(Ybbb)) { System.out.println("Success"); } else {
System.out.println("Failed"); }
|