简体   繁体   English

JAVA rsa解密cipher.dofinal返回0大小数组

[英]JAVA rsa decryption cipher.dofinal returns 0 size array

Here is my encryption code...这是我的加密代码...

private static boolean RsaEncrypt() throws CertificateException, FileNotFoundException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
    // TODO encrypt input file ans write output file
    String plainText="";
    Scanner sc=new Scanner(mTargetFile);
    while(sc.hasNextLine()){
        plainText+=sc.nextLine();
    }

    FileInputStream fin=new FileInputStream(mKeyFile);
    CertificateFactory factory=CertificateFactory.getInstance("X.509");
    X509Certificate cert=(X509Certificate) factory.generateCertificate(fin);
    PublicKey pk=cert.getPublicKey();

    Cipher cipher=Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, pk);

    byte[] bytePlain= cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
    String encrypted=Base64.getEncoder().encodeToString(bytePlain);
    writeFile(encrypted, mTargetFile.getName()+"_enc.txt");

    return false;
}

and here is my decryption code.这是我的解密代码。 It is weird cause it works well when plaintext only contains 'English'.这很奇怪,因为当纯文本仅包含“英语”时它运行良好。 If I put Korean in plain text, it returns 0 size array.如果我将韩语放在纯文本中,它会返回 0 大小的数组。

I have no idea what happens...我不知道会发生什么...

private static <privateKey> boolean RsaDecrypt() throws KeyStoreException, IOException, UnrecoverableEntryException, NoSuchAlgorithmException, CertificateException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException {
    char[] password = getPassword();
    String alias="alice";

    byte[] cipherText=Files.readAllBytes(mTargetFile.toPath());
    String cipherString=new String(cipherText, StandardCharsets.UTF_8);
    System.err.println(cipherString.length());
    // TODO decrypt input file and write output file
    KeyStore keystore=KeyStore.getInstance("PKCS12");
    FileInputStream fin=new FileInputStream(mKeyFile);
    keystore.load(fin, password);
    PrivateKey privateKey=(PrivateKey)keystore.getKey(alias, password);
    //KeyStore.PrivateKeyEntry privateKeyEntry= (KeyStore.PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(password));

    Cipher cipher=Cipher.getInstance("RSA");
    byte[] encryptedByte= Base64.getDecoder().decode(cipherString.getBytes(StandardCharsets.UTF_8));
    System.err.println(encryptedByte.length);
    cipher.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] decryptedByte=cipher.doFinal(encryptedByte);
    System.err.println(decryptedByte.length);

    writeFile(decryptedByte, mTargetFile.getName()+"_dec.txt");
    return false;
}

Your encryption/decryption code looks good.您的加密/解密代码看起来不错。 Not sure about KeyStore or file handling, but Cipher usage is ok.不确定KeyStore或文件处理,但Cipher使用没问题。 I did test it, and got proper result even with Korean letters.我确实对其进行了测试,即使使用韩文字母也得到了正确的结果。 Are you sure you didn't hide some crucial stack trace errors?你确定你没有隐藏一些关键的堆栈跟踪错误吗?

I could guess you are trying to encrypt too long plain message.我猜你正在尝试加密太长的普通消息。 Using raw RSA you can encrypt only limited short messages.使用原始 RSA,您只能加密有限的短消息。 With 1024 bits RSA key you can encrypt only 117 bytes of data.使用 1024 位 RSA 密钥,您只能加密 117 字节的数据。 If you need to encrypt longer messages you should use symmetric encryption like AES.如果您需要加密较长的消息,则应使用 AES 等对称加密。 Usually you encrypt message with AES, and then encrypt AES key with RSA key.通常你用 AES 加密消息,然后用 RSA 密钥加密 AES 密钥。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM