- 论坛徽章:
- 0
|
There are many open source today. this also works for the crypt projects.
Yet the different crypt implementation have some compatibility issue between them.
1. Different implementations
C Crypt implementation
http://www.libtomcrypt.org/
Sun Crypt implematation:
http://java.sun.com
Bouncy Castle Crypt implematation:
http://www.bouncycastle.org/
2. Compatibilty issue.
[color="#800080"]libtomcrypt generate DSA key format can not be supported by java DSA implemation.
[color="#800080"]while bouncycastle do.
[color="#800080"]3. Implementation code.
import java.math.*;
import org.bouncycastle.crypto.params.*;
import org.bouncycastle.crypto.signers.DSASigner;
import org.bouncycastle.crypto.digests.SHA1Digest;
public class SignatureVerifier
{
private DSASigner dsaSigner = null;
/** Build Public Key object from the y/p/q/g data files */
public SignatureVerifier()
throws FileNotFoundException, IOException
{
// Load key parameters from data files
BigInteger y = readBigInt("y.dat");
BigInteger p = readBigInt("p.dat");
BigInteger q = readBigInt("q.dat");
BigInteger g = readBigInt("g.dat");
System.out.println( "Y: " + y + "
P: " + p + "
Q: " + q + "
G: " + g);
DSAParameters dsaParams = new DSAParameters( p, q, g);
DSAPublicKeyParameters dsaPubParams = new DSAPublicKeyParameters( y, dsaParams );
dsaSigner = new DSASigner();
dsaSigner.init( false, dsaPubParams );
}
/** Verify the header data
headerData should be
*/
public boolean verify( byte[] headerData, byte[] signature )
{
boolean valid = false;
// Build SHA1 digest from the passed in header data
SHA1Digest sha1 = new SHA1Digest();
sha1.update( headerData, 0, headerData.length );
byte[] sha1hash = new byte[ sha1.getDigestSize() ];
sha1.doFinal( sha1hash, 0 );
// Decompose signature into the r and s values
byte[] rBytes = new byte[20];
byte[] sBytes = new byte[20];
System.arraycopy( signature, 0, rBytes, 0, 20);
System.arraycopy( signature, 20, sBytes, 0, 20);
BigInteger r = new BigInteger( 1, rBytes );
BigInteger s = new BigInteger( 1, sBytes );
valid = dsaSigner.verifySignature( sha1hash, r, s );
return(valid);
}
/** Reads a BigInteger from a binary file */
static public BigInteger readBigInt( String filename )
throws FileNotFoundException, IOException
{
FileInputStream intFIS = new FileInputStream( filename );
int numBytes = intFIS.available();
byte[] fileBytes = new byte[numBytes];
intFIS.read( fileBytes );
intFIS.close();
BigInteger bigInt = new BigInteger( 1, fileBytes );
return(bigInt);
}
public static void main( String args[])
throws Exception
{
SignatureVerifier sigVerify = new SignatureVerifier();
try{
// Read signature from test file
byte[] signature = new byte[40];
FileInputStream fis = new FileInputStream( "test.txt.sig" );
fis.read(signature );
fis.close();
// Read content file (simulated header)
byte[] header = new byte[140];
fis = new FileInputStream( "test.txt" );
fis.read( header );
fis.close();
boolean sigOkay = sigVerify.verify( header, signature );
System.out.println("signatureOkay=" + sigOkay );
}catch(Exception e){
e.printStackTrace();
}
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/9924/showart_52953.html |
|