Joke Collection Website - Blessing messages - How to realize electronic signature in Java

How to realize electronic signature in Java

Import? Java . security . invalidkeyexception;

Import? Java . security . key;

Import? Java . security . key pair;

Import? Java . security . key pair generator;

Import? Java . security . nosuchalgorithm exception;

Import? Java . security . private key;

Import? Java . security . public key;

Import? Java . security . securerandom;

Import? Java . security . signature;

Import? Java . security . signature exception;

Import? Java . util . logging . level;

Import? Java . util . logging . logger;

/**

*? ECDSA? 160bit? Signature and signature verification example

*/

Public? Class? ECDSASignature? {

Public? Static electricity Invalid? Main (string? argv[])? {

signatureTest();

}

Public? Static electricity Invalid? signatureTest()? {

/**

*? Key pair generation

*/

KeyPairGenerator? keyPairGenerator? =? null

Try it? {

keyPairGenerator? =? keypairgenerator . getinstance(" EC ");

}? Catch? (NoSuchAlgorithmException? ex)? {

Return;

}

SecureRandom? secureRandom? =? New? SecureRandom();

keypairgenerator . initialize( 160,securer andom);

KeyPair? Pairing? =? keypairgenerator . generatekeypair();

Keys? Public key? =? pair . get public();

Keys? privateKey? =? pair . get private();

//? character string

String? hako? =? "test";

/**

*? Private key signature (ECDSA? 160th bit)

*/

byte[]? Sign? =? null

Try it? {

Signature? Sign your resignation? =? null

Sign your resignation? =? signature . getinstance(" NONEwithECDSA ");

signature . init sign((private key)? privateKey? securer andom);

signature . update(hako . getbytes());

Sign? =? signature . sign();

System.out.println("sign:"? +? New? Character string (symbol));

}? Catch? (InvalidKeyException? |? NoSuchAlgorithmException? |? SignatureException? ex)? {

logger . get logger(ECD SAS signature . class . getname())。 Log (level. Is it serious? null,? ex);

}

/**

*? Signature verification with public key

*/

Signature? signatureVerify? =? null

Try it? {

signatureVerify? =? signature . getinstance(" NONEwithECDSA ");

signatureverify . init verify((public key)? public key);

signatureverify . update(hako . getbytes());

Bull? verifyResult? =? SignatureVerify.verify (signature);

System. Out.println (verification result "signature OK"? :? "signature ng");

}? Catch? (NoSuchAlgorithmException? |? InvalidKeyException? |? SignatureException? e)? {

logger . get logger(ECD SAS signature . class . getname())。 Log (level. Is it serious? null,? e);

}

}

}/Java-% E7 % 94% B5 % E5 % AD % 90% E7 % AD % BE % E5 % 90% 8D % E8 % 8C % 83% E4 % BE % 8B % E4 % BB % A3 % E7 % A0 % 8 1/