RSA key pair, encryption, decryption

RSA key pair

A recent project has requirements for data transmission encryption and decryption, so we used the RSA asymmetric encryption, so here record it.

What is at first introduced the RSA encryption algorithm it (copy)

RSA encryption algorithm is an asymmetric encryption algorithm . In public-key encryption and electronic commerce in the RSA is widely used. RSA 1977 by the Ronald Rivest (Ron Rivest), Adi Shamir (Adi Shamir) and Leonard Adleman (Leonard Adleman) presented together. At that time the three of them are in the MIT work. RSA is the three of them last names beginning with the letter composed pieces together.
In public-key cryptosystem , the encryption key (that is, the public key) PK is public information, and the decryption key (ie secret key) SK is confidential. Encryption algorithm E and the decryption algorithm D are also disclosed. Although the decryption key SK is determined by the public key PK, and large numbers can not be calculated because the n Euler function phi (N), it can not be calculated based on the PK SK
is based on this theory, in 1978 there was the famous RSA algorithm, it is usually Mr. into a pair of RSA keys, one of which is a secret key stored by the user; the other is the public key can be made public, even in a network server registration. In order to improve the strength of confidentiality, the RSA key length of at least 500, generally 1024 recommended. This allows a large amount of computation encryption. To reduce the calculation amount, the information is transferred, often using conventional encryption methods and public keyA combination of encryption methods, i.e., the information of the improved encryption or DES IDEA key and use the session key RSA key encryption and message digest. After receiving the information the other side, with a different key to decrypt and verify the message digest. RSA algorithm can be used for both the first encryption and digital signature algorithm, also easy to understand and operate. RSA is the most widely studied of public-key algorithm, presented to thirty years from today's, have withstood the test of various attacks, gradually accepted, as of 2017, is widely regarded as the best program of public One.

From: https: //www.jianshu.com/p/d614ba4720ec

The following is a data encryption and decryption

public class RSAGenerator{
   public static void main(String[] args) throws Exception {

        String data = "abc";
        KeyPair keyPair = generateRSAKeyPair(DEFAULT_KEY_SIZE);
        PublicKey aPublic = keyPair.getPublic(); // 公钥
        PrivateKey aPrivate = keyPair.getPrivate(); // 私钥
        String strPriKey = new String(Base64.getEncoder().encode(aPrivate.getEncoded()));
        String strPubKey = new String(Base64.getEncoder().encode(aPublic.getEncoded()));

        System.out.println(strPubKey);
        System.out.println("-------------");
        System.out.println(strPriKey);
        System.out.println("--------------");
        System.out.println("加密如下");
        System.out.println("vvvvvvvvvvvv");
        byte[] encrypt = encrypt(data.getBytes(), aPublic);
        System.out.println(new String(encrypt));
        System.out.println("解密如下");
        System.out.println("vvvvvvvvvvvv");
        byte[] bytes1 = decrypt(encrypt, aPrivate);
        System.out.println(new String(bytes1));


    }

    public static final String RSA = "RSA";// 非对称加密密钥算法
    public static final String ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";//加密填充方式
    public static final int DEFAULT_KEY_SIZE = 1024;//秘钥默认长度
    public static final byte[] DEFAULT_SPLIT = "#PART#".getBytes();    // 当要加密的内容超过bufferSize,则采用partSplit进行分块加密
    public static final int DEFAULT_BUFFERSIZE = (DEFAULT_KEY_SIZE / 8) - 11;// 当前秘钥支持加密的最大字节数

    public static KeyPair generateRSAKeyPair(int keyLength) {
        try {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance(RSA);
            kpg.initialize(keyLength);
            return kpg.genKeyPair();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }


    //公钥加密
    public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{
        Cipher cipher=Cipher.getInstance(ECB_PKCS1_PADDING);//java默认"RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(content);
    }

    //私钥解密
    public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{
        Cipher cipher=Cipher.getInstance(ECB_PKCS1_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(content);
    }
}
Published 35 original articles · won praise 8 · views 2965

Guess you like

Origin blog.csdn.net/qq_41946543/article/details/104662927