Joke Collection Website - Blessing messages - How to realize electronic signature in Java
How to realize electronic signature in Java
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/
- Related articles
- What are the dangers of "checking points in advance"?
- Closing speech of May Day holiday
- How should I reply when I receive a text message of successful interview?
- Now the teacher sends homework to parents' mobile phones. What's the use?
- It is said that there is a short message card in Taiwan Province Province, which is specially used to send short messages to Taiwan Province Province. Has anyone used it? How do you feel?
- My agricultural bank card was swallowed by the rural credit cooperatives, but I don't know why the card number was half missing.
- The 360 assistant has a short message, but the formatted mobile phone can't be imported and important information can't be read. Please help.
- Does the rookie in Luohu, Shenzhen have an epidemic?
- How to write the speech of the host of the job fair?
- Is the photography scam of Nanchang Meiyan Society true?