不耽误各位时间,我就开门见山了哈~
明文是一个长度为50000的byte[]数组,RSA算法中用到了bcprov-jdk16-144.jar这个包,具体算法就是常规的那几步,就不在这里列出来了,跟网上别人写的都差不多。
可是奇怪的是加密完再解密后这个数组的长度就变化了。。内容也“不完全”一样,总是少几个byte。而且明文不同,解密后丢失的长度也不尽相同,有些只少了一两个byte,有些甚至少了上百了byte
小弟不才,实在想不通为什么加密完再解密后居然跟原来的数组不一样。。
请问有没有哪位高人遇到过同样的问题?求指点。。

解决方案 »

  1.   

    你用的 RSA 是多少位的?对于 RSA 来说原文长度是有限制的。
      

  2.   

    我用的是1024位的RSA密钥。
    我也在网上看到有说只能加密117字节的分组,但是只要分开不就行了么?这个分组数量难道还有限制么?
      

  3.   

    唉。。又是这样。。现在csdn怎么这么不活跃呢
      

  4.   

      public byte[] privateKeyDecrypt(byte[] aBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
          InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
        ByteBuffer tempBuffer = ByteBuffer.allocate(aBytes.length);
        byte[] result;
        int iOffset = 0;
        int iLength = 0;
        while (iOffset < aBytes.length) {
          iLength = aBytes.length - iOffset;
          if(iLength > 128){
            iLength = 128;
          }
          result = cipher.doFinal(aBytes, iOffset, iLength);
          tempBuffer.put(result);
          iOffset += 128;
        }
        tempBuffer.flip();
        byte[] finalBytes = new byte[tempBuffer.remaining()];
        tempBuffer.get(finalBytes);
        return finalBytes;
      }
      

  5.   

        KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
        keyPairGen.initialize(1024);
        KeyPair keyPair = keyPairGen.generateKeyPair();
        setPrivateKey(keyPair.getPrivate());
        RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();key是这么产生的