Java Implementation Digital Signature

Source: Internet
Author: User
Tags base64

    1. Generate public and private key pairs

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 ()); //  Initializes a 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 ("Generate key pair failed ");    e.printstacktrace ();   } } public byte[] getprikey ()  {   return prikey; } public byte[] getpubkey ()  {  return  pubkey; }}

2. Encryption process (digital signature): Data encryption of transmitted data with private key

Public class signaturer { /**  *   * description: Digital Signature    *   *  @param  priKeyText  *  @param  plaintext  *   @return     */ 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);   //  generate digital signatures for information with private key     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; }}

3. Verify the digital signature by using the public key and the data plaintext to encode the data check of the "Data word signature".

Public class signprovider { private signprovider ()  { } /**   *   * description: Verifying the digital signature, this method does not throw a task exception, returns true successfully, fails to return false, requires that all parameters cannot be empty   *    *  @param  pubKeyText  *              public Key, base64 encoding   *  @param  plainText  *              Clear   *  @param  signTest  *              digitally signed ciphertext, base64 encoding   *  @return   Verify successful return true  failure return false    */ public static boolean verify ( Byte[] pubkeytext, string plaintext,   byte[] signtext)  {   try {   //  decrypts the public key encoded by Base64 and constructs X509encodedkeyspec objects     Java.security.spec.x509encodedkeyspec bobpUbkeyspec = new java.security.spec.x509encodedkeyspec (      Base64.decode (Pubkeytext));    // rsa symmetric encryption Algorithm    java.security.KeyFactory  Keyfactory = java.security.keyfactory     .getinstance ("RSA");    //  access to public key objects    java.security.PublicKey pubKey = keyFactory      .generatepublic (Bobpubkeyspec);   //  decryption of digital signatures encoded by Base64     Byte[] signed = base64.decode (Signtext);   java.security.signature  Signaturechecker = java.security.signature     .getinstance ("MD5withRSA");    signaturechecker.initverify (PubKey);    signaturechecker.update ( Plaintext.getbytes ());   //  Verify that the signature is normal    if  (signaturechecker.verify ( Signed))     RETURN TRUE;&Nbsp;  else    return false;  } catch  (Throwable e)  {   system.out.println ("Verify signature Failed");    e.printstacktrace ();    return false;  } }}


Java Implementation Digital Signature

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.