This article describes the RSA encryption and decryption algorithm implemented in Java. Share it for your reference, as follows:
import java.awt.AlphaComposite;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.RenderingHints;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.security.Key;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.SecureRandom;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import javax.crypto.Cipher;public class RSAUtils{ public static String makekeyfile(String pubkeyfile, String Prikeyfile) { String result = "Creating public and private key file failed"; try{ // KeyPairGenerator is used to generate public and private key pairs, and the object is generated based on the RSA algorithm. KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); // Initialize the key pair generator, the key size is 1024 bits gen.initialize(1024); // // Generate strong random number// SecureRandom random = new SecureRandom();// gen.initialize(1024,random); // Generate a key pair and save it in the pair KeyPair pair = gen.generateKeyPair(); // Get the private key RSAPrivateKey priKey = (RSAPrivateKey) pair.getPrivate(); // Get the public key RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic(); // Generate private key file ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(prikeyfile)); os.writeObject(priKey); os.flush(); os.close(); // Generate public key file os = new ObjectOutputStream(new FileOutputStream(pubkeyfile)); os.writeObject(pubKey); os.flush(); os.close(); result = "Generate public key file ["+pubkeyfile+"] Generate private key file ["+prikeyfile+"]"; } catch(Exception e){ e.printStackTrace(); } return result; } public static void main(String[] args) { try{ String pubfile = "F:/images/pub.key"; String license = "F:/images/pri.key"; String result = null; //result = makekeyfile(pubfile, license); result = markPuPra(pubfile, license); System.out.println(result); }catch(Exception e){ e.printStackTrace(); } } public static String markPuPra(String pubfile,String license){ String results = "Encryption error occurred"; try{ ObjectInputStream os = new ObjectInputStream(new FileInputStream(pubfile)); RSAPublicKey pubkey = (RSAPublicKey) os.readObject(); os.close(); os = new ObjectInputStream(new FileInputStream(prifile)); RSAPrivateKey Prikey = (RSAPrivateKey) os.readObject(); os.close(); String utf = "UTF-8"; String msg = "##China% of % people @+_"; // Use public key to encrypt private key to decrypt System.out.println("Original text: " + msg); byte[] puk = handleData(pubkey, msg.getBytes(utf), 1); System.out.println("Encrypted file data: " + new String(puk, utf)); byte[] dpuk = handleData(prikey, puk, 0); System.out.println("Decrypted file data: " + new String(dpuk, utf)); msg = "jd#Our ¥+=#New"; // Use private key to encrypt public key to decrypt System.out.println("Original text: " + msg); byte[] prk = handleData(prikey, msg.getBytes(utf), 1); System.out.println("Encrypted file data: " + new String(prk, utf)); byte[] dprk = handleData(pubkey, prk, 0); System.out.println("Decrypted file data: " + new String(dprk, utf)); results = "Encryption and decryption completed"; }catch(Exception e){ e.printStackTrace(); } return results; } /** * * @param k * @param data * @param encrypt 1 Encryption 0 decryption* @return * @throws Exception */ public static byte[] handleData(Key key, byte[] data, int type) throws Exception { if (key != null) { Cipher ci = Cipher.getInstance("RSA"); if (type == 1) { ci.init(Cipher.ENCRYPT_MODE, key); byte[] res = ci.doFinal(data); return res; } if (type == 0) { ci.init(Cipher.DECRYPT_MODE, key); byte[] res = ci.doFinal(data); return res; } } return null; }}PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.VeVB.COM/password/txt_encode
MD5 online encryption tool:
http://tools.VeVB.COM/password/CreateMD5Password
Online hash/hash algorithm encryption tool:
http://tools.VeVB.COM/password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools.VeVB.COM/password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools.VeVB.COM/password/sha_encode
For more information about Java related content, please check out the topics of this site: "Summary of Java Mathematical Operation Skills", "Tutorial on Java Data Structures and Algorithms", "Summary of Java Characters and String Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.