RSA asymmetric algorithm, encryption and decryption of data!

Source: Internet
Author: User

package com.yjm.test509;import java.io.bufferedinputstream;import java.io.fileinputstream; import java.io.filenotfoundexception;import java.io.ioexception;import  java.io.unsupportedencodingexception;import java.security.invalidkeyexception;import  java.security.key;import java.security.keyfactory;import java.security.keypair;import  java.security.keypairgenerator;import java.security.keystore;import java.security.keystoreexception; import java.security.messagedigest;import java.security.nosuchalgorithmexception;import  Java.security.nosuchproviderexception;import java.security.privatekey;import java.security.signature ;import java.security.signatureexception;import java.security.unrecoverablekeyexception;import  java.security.cert.certificate;import java.security.cert.certificateexception;import  java.security.cert.certificatefactory;import java.security.cert.x509certificate;Import java.security.interfaces.rsaprivatekey;import java.security.interfaces.rsapublickey;import  java.security.spec.invalidkeyspecexception;import java.security.spec.pkcs8encodedkeyspec;import  java.security.spec.X509EncodedKeySpec;import javax.crypto.BadPaddingException;import  javax.crypto.cipher;import javax.crypto.illegalblocksizeexception;import  Javax.crypto.nosuchpaddingexception;public class test {public static void main ( String[] args)  {// test ();// test1 ();// test2 ();// test3 (); Test4 ();} PRIVATE STATIC VOID TEST4 ()  {try {//RSA  key pair keypairgenerator  Keypairgenerator = keypairgenerator.getinstance ("RSA"); Keypairgenerator.initialize (1024); Keypair keypair = keypairgenerator.generatekeypair ();//Get Public key Rsapublickey rsapublickey  =  (Rsapublickey)  keypair.getpublic (); Rsaprivatekey rsaprivatekey =  (Rsaprivatekey)  keypair.getprivate ();//Get Algorithm keyfactory keyfactory =  Keyfactory.getinstance ("RSA"); Cipher cipher = cipher.getinstance (Keyfactory.getalgorithm ());//Encryption Cipher.init (Cipher.ENCRYPT_ Mode, keypair.getprivate ()); Byte[] b = cipher.dofinal ("Test". GetBytes ()); System.out.println (new string (b));//Decryption Cipher.init (Cipher.decrypt_mode, keypair.getpublic ()); byte[]  p = cipher.dofinal (b); System.out.println (New string (P));}  catch  (nosuchalgorithmexception e)  {e.printstacktrace ();}  catch  (nosuchpaddingexception e)  {e.printstacktrace ();}  catch  (invalidkeyexception e)  {e.printstacktrace ();}  catch  (illegalblocksizeexception e)  {e.printstacktrace ();}  catch  (badpaddingexception e)  {e.printstacktrace ();}}



Input results

' ~y#kq ' ~j& #NON "@uU +*,z0kvst+usnf! (=φkphans$

Test


RSA asymmetric algorithm, encryption and decryption of data!

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.