SHA512withRSA,加解密验证

时间:2025-01-31 08:08:01
import .Base64;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .PKCS8EncodedKeySpec;
import .X509EncodedKeySpec;
import ;
import ;
import ;


/**
 *  * 数字签名
 *  * 1:SHA512withRSA,:将正文通过SHA512数字摘要后,将密文 再次通过生成的RSA密钥加密,生成数字签名,
 *  * 将明文与密文以及公钥发送给对方,对方拿到私钥/公钥对数字签名进行解密,然后解密后的,与明文经过MD5加密进行比较
 *  * 如果一致则通过
 *  * 2:使用Signature的API来实现SHA512withRSA
 *  *
 *  
 */
public class SHA512withRSA {


    /**
     * 使用RSA生成一对钥匙
     *
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair getKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = ("RSA");
        (1024);
        //生成返回带有公钥和私钥的对象
        KeyPair generateKeyPair = ();
        return generateKeyPair;
    }

    /**
     * 生成私钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     * @return 
     */
    public static PrivateKey getPrivateKey(KeyPair key) {
        PrivateKey generatePrivate = null;
        try {
            PrivateKey private1 = ();
            byte[] encoded = ();
            byte[] bytes = Base64.encodeBase64(encoded);
            String string = new String(bytes, "UTF-8");
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);
            KeyFactory factory = ("RSA");
            generatePrivate = (keySpec);
        } catch (Exception e) {
// TODO: handle exception
        }
        return generatePrivate;
    }

    /**
     * 私钥加密
     *
     * @throws BadPaddingException 
     * @throws IllegalBlockSizeException 
     */
    public static byte[] encrtpyByPrivateKey(byte[] bb, PrivateKey key) throws IllegalBlockSizeException, BadPaddingException {
        byte[] doFinal = null;
        try {
            Cipher cipher = ("RSA");
            (Cipher.ENCRYPT_MODE, key);
            doFinal = (bb);
        } catch (Exception e) {
// TODO: handle exception
        }
        return doFinal;
    }

    /**
     * 获取公钥
     *
     * @return
     * @throws NoSuchAlgorithmException
     * @return 
     */
    public static PublicKey getPublicKey(KeyPair keyPair) {
        PublicKey publicKey = null;
        try {
            PublicKey public1 = ();
            byte[] encoded = ();
            byte[] bytes = Base64.encodeBase64(encoded);
            String string = new String(bytes, "UTF-8");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encoded);
            KeyFactory factory = ("RSA");
            publicKey = (keySpec);
        } catch (Exception e) {
        // TODO: handle exception
        }
        return publicKey;
    }

    /**
     * 使用公钥解密
     *
     * @return
     * @throws NoSuchAlgorithmException
     * @return 
     */
    public static byte[] decodePublicKey(byte[] b, PublicKey key) {
        byte[] doFinal = null;
        try {
            Cipher cipher = ("RSA");
            (Cipher.DECRYPT_MODE, key);
            doFinal = (b);
        } catch (Exception e) {
// TODO: handle exception
        }
        return doFinal;
    }

    //通过SHA1加密
    public static byte[] encryptMD5(String str) throws NoSuchAlgorithmException {
        MessageDigest digest = ("SHA1");
        byte[] digest2 = (());
        return digest2;
    }

    //sign签名
    public static byte[] sign(String str, PrivateKey key) throws NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException {
        byte[] encryptMD5 = encryptMD5(str);
        byte[] encrtpyByPrivateKey = encrtpyByPrivateKey(encryptMD5, key);
        return encrtpyByPrivateKey;
    }

    //校验
    public static boolean verify(String str, byte[] sign, PublicKey key) throws NoSuchAlgorithmException {
        byte[] encryptMD5 = encryptMD5(str);
        byte[] decodePublicKey = decodePublicKey(sign, key);
        String a = new String(encryptMD5);
        String b = new String(decodePublicKey);
        if ((b)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Signature的用法
     * 数字签名
     *
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException 
     * @throws SignatureException 
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] signMethod(String str, PrivateKey key) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    //初始化 MD5withRSA
        Signature signature = ("SHA512withRSA");
    //使用私钥
        (key);
    //需要签名或校验的数据
        (());
        return ();//进行数字签名
    }

    /**
     * 数字校验
     *
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException 
     * @throws SignatureException 
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static boolean verifyMethod(String str, byte[] sign, PublicKey key) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
        Signature signature = ("SHA512withRSA");
        (key);
        (());
        return (sign);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, SignatureException {
        //获取钥匙对
        KeyPair keyPair = getKeyPair();
        //获取公钥
        PublicKey publicKey = getPublicKey(keyPair);
        //获取私钥
        PrivateKey privateKey = getPrivateKey(keyPair);

        /********************基于SignatureAPI签名*************************************/
        String signStr = "的方法十分士大夫";
        byte[] signMethod = signMethod(signStr, privateKey);

        boolean verifyMethod = verifyMethod(signStr, signMethod, publicKey);
        ("使用SignatureAPI 数字签名是否一致:" + verifyMethod);
    }
}