Java RSA encryption algorithm generates public and private keys

Source: Internet
Author: User
Tags base64 decrypt

Original: HTTP://JINGYAN.BAIDU.COM/ARTICLE/6DAD5075F33466A123E36ECB.HTML?QQ-PF-TO=PCQQ.C2C

So far, RSA is the most widely used public key encryption algorithm, which can resist the most known password attacks, and has been recommended by ISO as the public key data Encryption standard.

In the RSA algorithm, each communication body has two keys, a public key is used to encrypt the data, and a private key is used to decrypt the data.

Let's look at how Java uses keypairgenerator to generate Keymap and parse Publickkey and privatekey from it.

Tools/Materials
    • Eclipse
Create Java Project keys
    1. 1

      Open Eclipse and create a new Java project.

      Action: Click "File", "New", "Java Project"

    2. 2

      Enter keys in the project name and click "Finish" to

      END
Introduce the package that you want to use
    1. 1

      To use RSA encryption, you need to introduce the java.security child package in your code, and Base64 encrypt and decrypt the package. Specific as follows:

      END
Writing code
    1. 1

      Write generate key Map Initkey,

    2. 2

      Write get public key function and auxiliary Base64 decoding function

    3. 3

      Write test code in main function (main function)

      END
Full code
  1. 1

    Full code:

    Import Java.security.Key;

    Import Java.security.KeyPair;

    Import Java.security.KeyPairGenerator;

    Import Java.security.interfaces.RSAPrivateKey;

    Import Java.security.interfaces.RSAPublicKey;

    Import Java.util.HashMap;

    Import Java.util.Map;

    Import Sun.misc.BASE64Decoder;

    Import Sun.misc.BASE64Encoder;

    @SuppressWarnings ("unused")

    public class Keys {

    public static final String key_algorithm = "RSA";

    public static final String signature_algorithm = "Md5withrsa";

    private static final String Public_key = "Rsapublickey";

    private static final String Private_key = "Rsaprivatekey";

    public static void Main (string[] args) {

    Map<string, object> KeyMap;

    try {

    KeyMap = Initkey ();

    String PublicKey = Getpublickey (KEYMAP);

    System.out.println (PublicKey);

    String Privatekey = Getprivatekey (KEYMAP);

    System.out.println (Privatekey);

    } catch (Exception e) {

    E.printstacktrace ();

    }

    }

    public static String Getpublickey (map<string, object> keyMap) throws Exception {

    Key key = (key) keymap.get (Public_key);

    byte[] PublicKey = key.getencoded ();

    Return encryptBASE64 (key.getencoded ());

    }

    public static String Getprivatekey (map<string, object> keyMap) throws Exception {

    Key key = (key) keymap.get (Private_key);

    Byte[] Privatekey =key.getencoded ();

    Return encryptBASE64 (key.getencoded ());

    }

    public static byte[] decryptBASE64 (String key) throws Exception {

    Return (new Base64decoder ()). Decodebuffer (key);

    }

    public static String encryptBASE64 (byte[] key) throws Exception {

    Return (new Base64encoder ()). Encodebuffer (key);

    }

    public static map<string, Object> Initkey () throws Exception {

    Keypairgenerator Keypairgen = keypairgenerator.getinstance (key_algorithm);

    Keypairgen.initialize (1024);

    KeyPair KeyPair = Keypairgen.generatekeypair ();

    Rsapublickey PublicKey = (rsapublickey) keypair.getpublic ();

    Rsaprivatekey Privatekey = (rsaprivatekey) keypair.getprivate ();

    map<string, object> keyMap = new hashmap<string, object> (2);

    Keymap.put (Public_key, PublicKey);

    Keymap.put (Private_key, Privatekey);

    return KEYMAP;

    }

    }

  2. 2

    Test results:

Java RSA encryption algorithm generates public and private keys

Related Article

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.