RSA Asymmetric Encryption Decryption instance

Source: Internet
Author: User
Tags asymmetric encryption

string content =  ""; for (int i=1;i<100;i++) content += i +  ")   Easy to say, hard to do.\n ";/*1, using public key encryption to file */{    /* 1) Reverse-construct the Publickkey object according to the public key, using */    string key_public  =  when calling cipher's Init method " mfwwdqyjkozihvcnaqebbqadswawsajbaiv38xxk06as11ocwq0ipqoqamz20zie8t5keamtubhzukgntaqygapua71r/ ineednsyyqs4pt6eawtjjop2rccaweaaq== ";     x509encodedkeyspec keyspec = new  x509encodedkeyspec (New base64decoder (). Decodebuffer (Key_public));     publickey  key = keyfactory.getinstance ("RSA"). Generatepublic (KeySpec);         cipher cipher = cipher.getinstance ("RSA");//rsa/ecb/pkcs1padding     cipher.init (Cipher.encrypt_mode, key);    /*      *  Direct encryption, because the content is too long will have an exception, need chunked encryption      * javax.crypto.illegalblocksizeexception: data must not be longer  Than 53 bytes     * cipher.dofinal (Content.getbytes ());   &NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;/*2) chunked encryption, with keypairgenerator.initialize (int  keysize), the keysize used in this example is 512*/    int blocksize = 53;// Cipher.getblocksize () is 0? 53    byte[] encryptbyte = content.getbytes ();    based on the exception prompt  //is divided by block size, the portion of the less piece is a piece of     int blocksNum =  (int) Math.ceil (1.0* encryptbyte.length)/blocksize);        fileoutputstream out  = new fileoutputstream ("License.lic");    for  (int i = 0;  i < blocksnum; i++)  {        if  (i  < blocksnum - 1)  {            out.write ( Cipher.dofinal (encryptbyte, i * blocksize, blocksize));         } else {             Out.write (Cipher.dofinal (encryptbyte, i * blocksize, encryptbyte.length - i  * blocksize));        }    }         out.close ();} /*2, using the private key to decrypt */{    /*1 from a file) Privatekey object is constructed according to the private key */    string key_ private =  "miibvaibadanbgkqhkig9w0baqefaascat4wgge6ageaakeai/ fzfettoczxu4jarqg9chacbpbrmitxpkp5oxnruhnsqy1mbdkacm5rvvh8g14qm2zljblg9porpzomminatwidaqabakbqdar7fbjbc15hgsqecv194d9wo3l 18dot9fnqgpsrogvyiviizp1/ Wihpmtn6uhwsoazqcoqv33gxlf6fkbwxaieav390q3x2cbjescxhnbqpiospe9ryd3ewsajn6q7ud7uciqc7htuzelhmcpV4xpzg6e1qzkwhbfuqkhg9aoeaniw0owiglw5tat3fhxqg4ek29sq34ufjcwjuuxcrljatqcl9gdeciqchqe+ Jzrxdbvsrcy9vb83jleo2hwpucjto24dbahsopwigev547ycgz+pyi1dnqhiljiifif+h1abzaih5mrkshtw= ";     pkcs8encodedkeyspec keyspec = new pkcs8encodedkeyspec (New BASE64Decoder (). Decodebuffer (key_private));     privatekey key = keyfactory.getinstance ("RSA "). Generateprivate (KeySpec);        cipher cipher =  Cipher.getinstance ("RSA");//rsa/ecb/pkcs1padding    cipher.init (Cipher.DECRYPT_MODE,  key);         /*2) block decryption, based on the size of each encrypted output to determine */     Fileinputstream cis = new fileinputstream ("License.lic");     Bytearrayoutputstream bos = new bytearrayoutputstream ();     /* encrypt time block encryption, Encrypted output result size fixed */    byte[] data = neW byte[cipher.getoutputsize (Cis.available ())];    int len = 0;     while ((Len = cis.read (data))  > 0) {         bos.write (Cipher.dofinal (Data, 0, len));    }     cis.close ();         system.out.print (New String ( Bos.tobytearray ()));}


RSA Asymmetric Encryption Decryption instance

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.