`
zeeeitch
  • 浏览: 83634 次
  • 性别: Icon_minigender_1
  • 来自: 九江
社区版块
存档分类
最新评论

java rsa算法

    博客分类:
  • java
阅读更多
rsa算法中的公钥是 modulus+publicExponent publicExponent一般是65537 0x10001
私钥是:modulus+privateExponent

一个项目需要从加密狗中读取公钥,与证书中公钥格式不同,他是最原始的公钥格式,publicExponent 为0x10001用驱动可读出modulus。

为了在java中用公钥加解密,采用了java包:
<dependency>
            <groupId>bouncycastle</groupId>
            <artifactId>bcprov-jdk16</artifactId>
            <version>136</version>
</dependency>


这个东西在编辑器中始终无法使用,放在独立的tomcat可以用。
下面是java加密代码。

 

        String encryptText = "li79b3NwrCAU2e4ba/4jFQPG20YAOl1mzxyIgbujM12Iu5p3myA/cN/WysSdadUj6fGCL2tQ06Z6RsDaJ4z6RHaEBkoeJCyRBDx+N86tnnhHx0k42+nPJnUqvUX4j36yXvqJxUoz8p2EjsdT+MwMGLfaAlEPga6txhPewOwBZxk=";
        byte[] privateExponent = {(byte) 0x6c,……我的私钥,保密:)};
        byte[] modulus = {(byte) 0x00, (byte) 0x9A, (byte) 0xCD, (byte) 0x59, (byte) 0x2C, (byte) 0x74, (byte) 0x07, (byte) 0x48, (byte) 0x79, (byte) 0x05, (byte) 0x07, (byte) 0xC5, (byte) 0x37, (byte) 0x9C, (byte) 0x56, (byte) 0x97,
                (byte) 0xFA, (byte) 0xF6, (byte) 0xBC, (byte) 0x90, (byte) 0x70, (byte) 0x84, (byte) 0x6F, (byte) 0x26, (byte) 0xFB, (byte) 0x90, (byte) 0x15, (byte) 0xE9, (byte) 0x1B, (byte) 0x4A, (byte) 0x15, (byte) 0xFE,
                (byte) 0x74, (byte) 0x58, (byte) 0xD0, (byte) 0x45, (byte) 0x5B, (byte) 0x48, (byte) 0x00, (byte) 0xDF, (byte) 0xA4, (byte) 0x9B, (byte) 0x82, (byte) 0x72, (byte) 0xC4, (byte) 0x6D, (byte) 0x25, (byte) 0xFC,
                (byte) 0xE2, (byte) 0x91, (byte) 0x1E, (byte) 0xFD, (byte) 0x4C, (byte) 0xCC, (byte) 0x6E, (byte) 0x1D, (byte) 0xFD, (byte) 0xF8, (byte) 0x2B, (byte) 0xBB, (byte) 0xCE, (byte) 0xA3, (byte) 0x99, (byte) 0xB6,
                (byte) 0xF1, (byte) 0xBE, (byte) 0x57, (byte) 0xD2, (byte) 0x6B, (byte) 0x51, (byte) 0xF5, (byte) 0x03, (byte) 0xFB, (byte) 0x98, (byte) 0xF2, (byte) 0x74, (byte) 0x17, (byte) 0xD3, (byte) 0x6D, (byte) 0x16,
                (byte) 0x74, (byte) 0x61, (byte) 0x48, (byte) 0x24, (byte) 0x6B, (byte) 0x68, (byte) 0x11, (byte) 0xB2, (byte) 0x72, (byte) 0xBD, (byte) 0x86, (byte) 0xA0, (byte) 0x0A, (byte) 0x6C, (byte) 0x71, (byte) 0x08,
                (byte) 0xF9, (byte) 0xC1, (byte) 0x3B, (byte) 0x8E, (byte) 0x4B, (byte) 0x70, (byte) 0x1B, (byte) 0x74, (byte) 0x97, (byte) 0xEA, (byte) 0xB7, (byte) 0x84, (byte) 0xDD, (byte) 0x3C, (byte) 0xD6, (byte) 0x91,
                (byte) 0xDD, (byte) 0xB3, (byte) 0xE7, (byte) 0x59, (byte) 0x32, (byte) 0x04, (byte) 0x7A, (byte) 0x00, (byte) 0xCB, (byte) 0x1E, (byte) 0x60, (byte) 0xF5, (byte) 0xB5, (byte) 0x51, (byte) 0xC4, (byte) 0x98,
                (byte) 0xB1};

        RSAPrivateKey PrivateKey = encrypt.generateRSAPrivateKey(modulus, privateExponent);
        Base64 base64 = new Base64();
        byte[] text = base64.decode(encryptText.getBytes());
        byte[] e = encrypt.decrypt(PrivateKey , text);



 protected byte[] decrypt(RSAPrivateKey privateKey, byte[] obj) throws Exception {
        if (privateKey != null) {
//            try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return cipher.doFinal(obj);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }

        return null;
    }

    protected RSAPublicKey generateRSAPublicKey(byte[] modulus, byte[] publicExponent) throws RuntimeException {
        KeyFactory keyFac = null;
        try {
            keyFac = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex.getMessage());
        }

        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulus), new BigInteger(publicExponent));
        try {
            return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
        } catch (InvalidKeySpecException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

    protected RSAPrivateKey generateRSAPrivateKey(byte[] modulus, byte[] privateExponent) throws RuntimeException {
        KeyFactory keyFac = null;
        try {
            keyFac = KeyFactory.getInstance("RSA", new BouncyCastleProvider());
        } catch (NoSuchAlgorithmException ex) {
            throw new RuntimeException(ex.getMessage());
        }

        RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(modulus), new BigInteger(privateExponent));
        try {
            return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
        } catch (InvalidKeySpecException ex) {
            throw new RuntimeException(ex.getMessage());
        }
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics