我正在使用RSA私有密钥加密我的对称密钥(AES),并使用我的公共密钥解密它。

但是,当我加密数据时,字节长度为16个字节,但是,当我解密数据时,它将引发错误,并且解密时字节数据的长度为344。

有人可以建议我的方法有什么问题吗?

加密码

 public static String encrypt(byte[] data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(data));
    }


解密码

public static String decrypt(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        System.out.println(data.length);
        return new String(Base64.getDecoder().decode(cipher.doFinal(data)));
    }


调用机制

CertificateFactory fact = CertificateFactory.getInstance("X.509");
            InputStream file1 = new FileInputStream("C:\\Users\\imjme1\\Desktop\\Work_backup\\FMS\\EPM_FILE_ENCRYPTION\\NIFT_SOLUTION\\Certificate_KeyStore\\cdcCert.cer");
            Certificate[] chain = { fact.generateCertificate(file1) };
            file1.close();  // or use try-with-resources

            KeyStore keyStore = KeyStore.getInstance( "JKS" );
            FileInputStream is = new FileInputStream("C:\\\\Users\\\\imjme1\\\\Desktop\\\\Work_backup\\\\FMS\\\\EPM_FILE_ENCRYPTION\\\\NIFT_SOLUTION\\\\Certificate_KeyStore\\\\senderKeystore.jks");
            keyStore.load( is, "fms123".toCharArray() );

PrivateKey privateKey = ( PrivateKey ) keyStore.getKey( "CDC", "fms123".toCharArray() );
            PublicKey publicKey= null;
            if (privateKey instanceof PrivateKey) {
                // Get certificate of public key
                Certificate cert = keyStore.getCertificate("CDC");
                // Get public key
                publicKey = cert.getPublicKey();


              }


            KeyGenerator generator = KeyGenerator.getInstance("AES");
            generator.init(128); // The AES key size in number of bits
            SecretKey secKey = generator.generateKey();

            String e =  encrypt( secKey.getEncoded() , privateKey);

            PrintWriter w = new PrintWriter( new File("C:\\Users\\imjme1\\Desktop\\Work_backup\\FMS\\EPM_FILE_ENCRYPTION\\NIFT_SOLUTION\\Certificate_KeyStore\\tet.txt") );
         w.write(e);
         w.close();
          //  encrypt(secKey.getEncoded(), privateKey);
            Path fileLocation = Paths.get("C:\\Users\\imjme1\\Desktop\\Work_backup\\FMS\\EPM_FILE_ENCRYPTION\\NIFT_SOLUTION\\Certificate_KeyStore\\tet.txt");
//
//
            byte[] data = Files.readAllBytes(fileLocation);
            String d = decrypt(  data , publicKey);

System.out.println(d);
            System.out.println("done");

最佳答案

我可以通过Base64编码器/解码器解决此问题

   public static String encrypt(byte[] data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(data));
    }

    public static String decrypt(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, publicKey);

        System.out.println(data.length);// it was 344 here

        byte[] decoded = Base64.getDecoder().decode(data);
        byte[] todecrypt = cipher.doFinal(decoded);

        System.out.println(decoded.length);// it become 256

        String finalstring = new String( todecrypt, "UTF-8" );
        return finalstring;
    }

10-04 14:07
查看更多