如何解决“数据不得超过256字节”。在Java中?

问题描述 投票:0回答:0

我正在使用 Java,我的同事正在使用 .NET。我们每个人都必须链接到彼此网站上的页面。连接时需要进行SSO处理,此时必须通过RSA加密发送和接收‘当前时间(yyyy-MM-dd HH:mm:ss)’。

首先,我正在测试RSA加密和解密是否正常。我的同事共享了一个公钥。当我用它加密和解密时,我不断收到错误“数据不得超过 256 字节”。我的代码有什么问题吗?

共享公钥是这样的: (我无法向您展示全部,所以我将向您展示一些。)

-xml 类型

<RSAKeyValue><Modulus>0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==</Modulus><Exponent>AQAB</Exponent><P>+IvTFBd3ldPsx5MYvxV[...]+kJQ==</D></RSAKeyValue>

-pem型

MIIBIjANBgkqhkiG9w0BAQ[...]5J6HMrj4MQIDAQAB

加密代码

    @RequestMapping(value="/ezConn/getEncryptedAkey1.do")
    @ResponseBody
        public String getEncryptedAkey1(@RequestParam String userId, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("getEncryptedAkey1 started. userId=" + userId);
        
        String encryptedAkey = "";
        
        try {
            //Modulus
            String modulusInBase64 = "0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==";
            //Exponent
            String exponentInBase64 = "AQAB";

            java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
            String modulusInHex = toHexString(decoder.decode(modulusInBase64));
            String exponentInHex = toHexString(decoder.decode(exponentInBase64));

            BigInteger modulus = new BigInteger(modulusInHex, 16);
            BigInteger pubExp = new BigInteger(exponentInHex, 16);

            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(modulus, pubExp);
            RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, key);

            //the current time
            String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

            byte[] cipherData = cipher.doFinal(now.getBytes());
            
            encryptedAkey = toHexString(cipherData);
                                                            
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        
        logger.debug("encryptedAkey=" + encryptedAkey);
        logger.debug("getEncryptedAkey1 ended.");
        
        return encryptedAkey;
    }
    
    private String toHexString(byte[] array) {
        return DatatypeConverter.printHexBinary(array);
    }

解密代码

    @RequestMapping(value = "/ezConn/getDecryptedAkey.do")
    public String getDecryptedAkey(@RequestParam String Akey, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.debug("getDecryptedAkey started");
        
        logger.debug("Akey=" + Akey);
        String decryptedAkey = "";

        try {
            String modulusInBase64 = "0Lvg8dDJA5z0VbYLVfzXT1pC7PJIY[...]AHuFqh/TQSuDENoLlOYfk4MSiNq2P5J6HMrj4MQ==";
            String exponentInBase64 = "AQAB";

            java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();
            String modulusInHex = toHexString(decoder.decode(modulusInBase64));
            String exponentInHex = toHexString(decoder.decode(exponentInBase64));

            BigInteger modulus = new BigInteger(modulusInHex, 16);
            BigInteger pubExp = new BigInteger(exponentInHex, 16);
            
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, pubExp);
            RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, key);

            byte[] cipherData = cipher.doFinal(Akey.getBytes());                        
            descryptedAkey = toHexString(cipherData);
            
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        
        logger.debug("decryptedAkey=" + decryptedAkey);
        logger.debug("getDecryptedAkey ended");
        return decryptedAkey;
    }
java asp.net rsa public-key-encryption private-key
© www.soinside.com 2019 - 2024. All rights reserved.