A simple implementation: a three-class keygenerater generates a public key private key pair, the signaturer class uses the Private Key signature, and the signprovider uses the public key for verification. The public key and private key use base64 to encrypt base64.
Public class keygenerater {
Private byte [] prikey;
Private byte [] pubkey;
Public void generater (){
Try {
Java. Security. keypairgenerator keygen = java. Security. keypairgenerator
. Getinstance ("RSA ");
Securerandom secrand = new securerandom ();
Secrand. setseed ("syj". getbytes (); // initialize the random Generator
Keygen. initialize (1024, secrand );
Keypair keys = keygen. genkeypair ();
Publickey pubkey = keys. getpublic ();
Privatekey prikey = keys. getprivate ();
Pubkey = base64.encodetobyte (pubkey. getencoded ());
Prikey = base64.encodetobyte (prikey. getencoded ());
System. Out. println ("pubkey =" + new string (pubkey ));
System. Out. println ("prikey =" + new string (prikey ));
} Catch (Java. Lang. Exception e ){
System. Out. println ("failed to generate the key pair ");
E. printstacktrace ();
}
}
Public byte [] getprikey (){
Return prikey;
}
Public byte [] getpubkey (){
Return pubkey;
}
}
Public class signaturer {
/**
*
* Description: Digital Signature
*
* @ Param prikeytext
* @ Param plaintext
* @ Return
* @ Author Sun Xiaojia
* @ Since: 10:51:48 AM
*/
Public static byte [] Sign (byte [] prikeytext, string plaintext ){
Try {
Pkcs8encodedkeyspec pripkcs8 = new pkcs8encodedkeyspec (base64
. Decode (prikeytext ));
Keyfactory keyf = keyfactory. getinstance ("RSA ");
Privatekey prikey = keyf. generateprivate (pripkcs8 );
// Use the private key to generate a digital signature
Java. Security. Signature signet = java. Security. Signature
. Getinstance ("md5withrsa ");
Signet. initsign (prikey );
Signet. Update (plaintext. getbytes ());
Byte [] signed = base64.encodetobyte (signet. Sign ());
Return signed;
} Catch (Java. Lang. Exception e ){
System. Out. println ("signature failed ");
E. printstacktrace ();
}
Return NULL;
}
}
Public class signprovider {
Private signprovider (){
}
/**
*
* Description: checks the digital signature. This method does not throw a task exception. if the task is successful, true is returned. If the task fails, false is returned. All parameters must not be blank.
*
* @ Param pubkeytext
* Public Key, base64 encoded
* @ Param plaintext
* Plaintext
* @ Param signtest
* Ciphertext of the digital signature, base64 encoded
* @ Return returns true if verification is successful. returns false if verification fails.
* @ Author Sun Xiaojia
* @ Since: 09:33:55 AM
*/
Public static Boolean verify (byte [] pubkeytext, string plaintext,
Byte [] signtext ){
Try {
// Decrypt the base64-encoded public key and construct the x509encodedkeyspec object
Java. Security. spec. x509encodedkeyspec bobpubkeyspec = new java. Security. spec. x509encodedkeyspec (
Base64.decode (pubkeytext ));
// RSA symmetric encryption algorithm
Java. Security. keyfactory = java. Security. keyfactory
. Getinstance ("RSA ");
// Public key object
Java. Security. publickey pubkey = keyfactory
. Generatepublic (bobpubkeyspec );
// Decrypt the base64-encoded Digital Signature
Byte [] signed = base64.decode (signtext );
Java. Security. Signature signaturechecker = java. Security. Signature
. Getinstance ("md5withrsa ");
Signaturechecker. initverify (pubkey );
Signaturechecker. Update (plaintext. getbytes ());
// Verify whether the signature is normal
If (signaturechecker. Verify (Signed ))
Return true;
Else
Return false;
} Catch (throwable e ){
System. Out. println ("Signature Verification Failed ");
E. printstacktrace ();
Return false;
}
}
}