Use the RSA Algorithm in Java to encrypt and decrypt the private key.

Source: Internet
Author: User

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;
}
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.