java实现RSA加密

准备 公钥:pub.key 公钥:pri.key 私钥

import javax.crypto.Cipher;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSA {

    /**
     * The constant that denotes the algorithm being used.
     */
    private static final String algorithm = "RSA";

    /**
     * The private constructor to prevent instantiation of this object.
     */
    private RSA() {

    }

    /**
     * The method that will create both the public and private key used to encrypt and decrypt the data.
     *
     * @param publicKeyOutput
     * 		The path of where the public key will be created.
     *
     * @param privateKeyOutput
     * 		The path of where the private key will be created.
     *
     * @return {@code true} If this operation was successful, otherwise {@code false}.
     */
    public static boolean generateKey(String publicKeyOutput, String privateKeyOutput) {
        try {
            final KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
            keyGen.initialize(2048);

            final KeyPair key = keyGen.generateKeyPair();

            try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(publicKeyOutput)))) {
                dos.write(key.getPublic().getEncoded());
            }

            try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File(privateKeyOutput)))) {
                dos.write(key.getPrivate().getEncoded());
            }

            return true;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * The method that will encrypt an array of bytes.
     *
     * @param key
     * 		The public key used to encrypt the data.
     *
     * @param data
     * 		The data in the form of bytes.
     *
     * @return The encrypted bytes, otherwise {@code null} if encryption could not be performed.
     */
    public static byte[] encrypt(PublicKey key, byte[] data) {
        try {

            final Cipher cipher = Cipher.getInstance(algorithm);

            cipher.init(Cipher.ENCRYPT_MODE, key);

            return cipher.doFinal(data);

        } catch (Exception ex) {

        }

        return null;

    }

    /**
     * The method that will decrypt an array of bytes.
     *
     * @param key
     * 		The {@link PrivateKey} used to decrypt the data.
     *
     * @param encryptedData
     * 		The encrypted byte array.
     *
     * @return The decrypted data, otherwise {@code null} if decryption could not be performed.
     */
    public static byte[] decrypt(PrivateKey key, byte[] encryptedData) {

        try {

            final Cipher cipher = Cipher.getInstance(algorithm);

            cipher.init(Cipher.DECRYPT_MODE, key);

            return cipher.doFinal(encryptedData);

        } catch (Exception ex) {

        }

        return null;

    }

    /**
     * The method that will re-create a {@link PublicKey} from a serialized key.
     *
     *
     * @param publicKeyPath
     * 		The path of the public key file.
     *
     * @throws Exception
     * 		If there was an issue reading the file.
     *
     * @return The {@link PublicKey} object.
     */
    public static PublicKey getPublicKey(String publicKeyPath) throws Exception {
        return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(Files.readAllBytes(Paths.get(publicKeyPath))));
    }

    /**
     * The method that will re-create a {@link PrivateKey} from a serialized key.
     *
     *
     * @param privateKeyPath
     * 		The path of the private key file.
     *
     * @throws Exception
     * 		If there was an issue reading the file.
     *
     * @return The {@link PrivateKey} object.
     */
    public static PrivateKey getPrivateKey(String privateKeyPath) throws Exception {
        return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(Files.readAllBytes(Paths.get(privateKeyPath))));
    }

    /**
     * The method that will re-create a {@link PublicKey} from a public key byte array.
     *
     * @param encryptedPublicKey
     * 		The byte array of a public key.
     *
     * @throws Exception
     * 		If there was an issue reading the byte array.
     *
     * @return The {@link PublicKey} object.
     */
    public static PublicKey getPublicKey(byte[] encryptedPublicKey) throws Exception {
        return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(encryptedPublicKey));
    }

    /**
     * The method that will re-create a {@link PrivateKey} from a private key byte array.
     *
     *
     * @param encryptedPrivateKey
     * 		The array of bytes of a private key.
     *
     * @throws Exception
     * 		If there was an issue reading the byte array.
     *
     * @return The {@link PrivateKey} object.
     */
    public static PrivateKey getPrivateKey(byte[] encryptedPrivateKey) throws Exception {
        return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(encryptedPrivateKey));
    }

}

调用RSA类中的方法,实现RSA解密

String data = "SCDN";
PublicKey publicKey = RSA.getPublicKey("/Users/xxx/demo/src/main/java/pub.key");
byte[] encrypt = RSA.encrypt(publicKey, data.getBytes());
PrivateKey privateKey = RSA.getPrivateKey("/Users/xxx/demo/src/main/java/pri.key");
byte[] decrypt = RSA.decrypt(privateKey, encrypt);
System.out.println(new String(decrypt));
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值