吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2478|回复: 1
收起左侧

[会员申请] 申请会员ID:zhushijie0608

[复制链接]
吾爱游客  发表于 2022-3-4 17:10
1、申 请 I D:zhushijie0608
2、个人邮箱:676988128@gmail.com
3、原创技术文章:https://gitee.com/hello65535/JLRSA

利用私钥加密,公钥解密,并验证加密数据签名,实现应用程序lisence授权发布。

public class JLRSACoder {
    public static final String KEY_ALGORITHM = "RSA";

    public static final String KEY_PROVIDER = "BC";

    public static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";

    static{
        try{
            Security.addProvider(new BouncyCastleProvider());
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 初始化密钥对
     */

    public static Map<String, Object> initKeys(String seed) throws Exception {


        Map<String, Object> keyMap = new HashMap<String, Object>();

        Security.addProvider(new BouncyCastleProvider());

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM, KEY_PROVIDER);


        keyPairGenerator.initialize(1024, new SecureRandom(seed.getBytes()));

        KeyPair pair = keyPairGenerator.generateKeyPair();

        RSAPublicKey rsaPublicKey = (RSAPublicKey) pair.getPublic();

        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) pair.getPrivate();


        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(rsaPublicKey.getModulus().toString()), new BigInteger(rsaPublicKey.getPublicExponent().toString()));

        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(rsaPrivateKey.getModulus().toString()), new BigInteger(rsaPrivateKey.getPrivateExponent().toString()));



        String pubKeystr= Base64.getEncoder().encodeToString((pubKeySpec.getModulus() + "----" + pubKeySpec.getPublicExponent()).getBytes());
        String priKeystr= Base64.getEncoder().encodeToString((priKeySpec.getModulus() + "----" + priKeySpec.getPrivateExponent()).getBytes());

        System.out.println("公钥:" + pubKeystr);

        System.out.println("私钥:" + priKeystr);

        keyMap.put("publicKey", pubKeystr);

        keyMap.put("privateKey", priKeystr);


        return keyMap;

    }



    /**
     * 私钥加密
     */

    public static byte[] encryptRSA(byte[] data, PrivateKey privateKey) throws Exception {

        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM, KEY_PROVIDER);

        cipher.init(Cipher.ENCRYPT_MODE, privateKey);

        int dataSize = cipher.getOutputSize(data.length);

        int blockSize = cipher.getBlockSize();

        int blockNum = 0;

        if (data.length % blockSize == 0) {
            blockNum = data.length / blockSize;
        } else {
            blockNum = data.length / blockSize + 1;
        }
        byte[] raw = new byte[dataSize * blockNum];
        int i = 0;
        while (data.length - i * blockSize > 0) {
            if (data.length - i * blockSize > blockSize) {
                cipher.doFinal(data, i * blockSize, blockSize, raw, i * dataSize);
            } else {
                cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * dataSize);
            }

            i++;

        }
        return raw;
    }


    /**
     * 公钥加密
     */

    public static byte[] decryptRSA(byte[] data, PublicKey publicKey) throws Exception {


        Cipher cipher = Cipher.getInstance(KEY_ALGORITHM, KEY_PROVIDER);

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        int dataSize = cipher.getOutputSize(data.length);

        int blockSize = cipher.getBlockSize();

        int blockNum = 0;

        if (data.length % blockSize == 0) {
            blockNum = data.length / blockSize;
        } else {
            blockNum = data.length / blockSize + 1;
        }
        byte[] raw = new byte[dataSize * blockNum];
        int i = 0;
        while (data.length - i * blockSize > 0) {
            if (data.length - i * blockSize > blockSize) {
                cipher.doFinal(data, i * blockSize, blockSize, raw, i * dataSize);
            } else {
                cipher.doFinal(data, i * blockSize, data.length - i * blockSize, raw, i * dataSize);
            }

            i++;

        }
        return raw;
    }



    public static String sign(byte[] encoderData, PrivateKey privateKey) throws Exception {
        Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM, KEY_PROVIDER);
        sig.initSign(privateKey);
        sig.update(encoderData);
        return new String(Base64.getEncoder().encode(sig.sign()));
    }

    /**
     * 校验数字签名
     */
    public static boolean verify(byte[] encoderData, String sign, PublicKey publicKey) throws Exception {

        Signature sig = Signature.getInstance(SIGNATURE_ALGORITHM, KEY_PROVIDER);
        sig.initVerify(publicKey);
        sig.update(encoderData);
        return sig.verify(Base64.getDecoder().decode(sign.getBytes()));
    }


    // 使用N、e值还原公钥
    public static PublicKey getPublicKey(String modulus, String publicExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger bigIntModulus = new BigInteger(modulus);
        BigInteger bigIntPrivateExponent = new BigInteger(publicExponent);
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus,
                bigIntPrivateExponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    }

    // 使用N、d值还原私钥
    public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
        BigInteger bigIntModulus = new BigInteger(modulus);
        BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(bigIntModulus,
        bigIntPrivateExponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
        return privateKey;
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Hmily 发表于 2022-3-7 14:58
抱歉,未能达到申请要求,申请不通过,可以关注论坛官方微信(吾爱破解论坛),等待开放注册通知。

ps:周年开放注册直接来注册吧。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 08:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表