0

我正在尝试解密 RSA 格式的密码,但它返回异常

Caused by: javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:308)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:255)
at com.sun.crypto.provider.RSACipher.a(DashoA13*..)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)

依次加解密时,工作正常。

看来我的私钥和公钥有问题,下面的代码中有任何证据吗?

谢谢

客户:

    public class TestarDescripto { 

    public static void main(String args []) throws Exception{ 

        String valorAberto = "pass123";

        System.out.println("open password  - > " + valorAberto );

        String valorCriptoRSA = "B5URvvSpoR7XaWLrYsbd6WQVNZG2haqeijuP8LzxtBigwuCG/5s4XO19wsGhtsFFfmnJ3vvqSS3TQ3DQnoQ3jVM8/sF7FqxW8u/RMJ5H/rJNSGAENDlA4U8WleQtP0S8ES8tONOuCdsqVLsbaYX/ep8naA2cEAmHYTFz30Jw9ylsPmvomlHCay14be9h2HkbxjzH/uuqk3J/xuMsPixITNpTgirh1gJVDZ1is9qPAJmpITmaxgCqwhFDuuYeMyVGYkVx/0e3hOWEStAAKlTy+lCgcc2GbJPwZysd08sDLVOoPwtDxzJ0SP0hyDrazs+cv9gKbBJ4OFxeTuBqf5AhEw==";

        System.out.println("Valor criptografado RSA - > " + valorCriptoRSA);

        String valorDesriptoRSA = TestarDescripto.executarDescriptoRSA(valorCriptoRSA);
        System.out.println("Valor descriptografado RSA - > " + valorDesriptoRSA);

        System.out.println("Senha aberta == Senha descriptografada - > " + valorAberto.equals(valorDesriptoRSA));

    } 

    public static String executarDescriptoRSA(String valorCriptografado ) throws CriptografadorException {

        Criptografador criptografador = CriptografadorFactory.getInstance().getCriptografador(CriptografadorTipo.RSA);

        CriptografadorBeanIn beanDesc = new CriptografadorBeanIn();

        beanDesc.setSenhaCriptografadaStringBytesBase64(valorCriptografado);

        CriptografadorBeanOut beanDescript = criptografador.decrypt(beanDesc);

        String senhaDescriptografada = new String (beanDescript.getSenhaAberta());

        return senhaDescriptografada;
}
}

服务器:

public class CriptografadorRSA extends CriptografadorSimetrico{ 

private static final String UTF_8 = "UTF8"; 
private static final int KEY_SIZE = 2048; 
private static final String RSA = "RSA";

private KeyPairGenerator KEY_PAIR_GENERATOR; 
private Cipher CIPHER;

private static final SecureRandom SECURE_RANDOM = new SecureRandom();

private KeyFactory RSA_KEY_FACTORY; 
private byte[] publicEncode;
private byte[] privateEncode;
private PublicKey rsaPubKey; 
private PrivateKey rsaPriKey; 

CriptografadorRSA () throws CriptografadorException {

    try {

        KEY_PAIR_GENERATOR = KeyPairGenerator.getInstance(RSA);

        KEY_PAIR_GENERATOR.initialize(KEY_SIZE, SECURE_RANDOM);

        RSA_KEY_FACTORY = KeyFactory.getInstance(RSA);

        CIPHER = Cipher.getInstance(RSA);
    } 
    catch (NoSuchAlgorithmException  exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    }
    catch (NoSuchPaddingException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    }

    String PUBLIC_KEY = CritografadorPropertiesUtil.getInstance().getPropriedade("PUBLIC_KEY");

    String PRIVATE_KEY = CritografadorPropertiesUtil.getInstance().getPropriedade("PRIVATE_KEY");

    File publicFile = new File(PUBLIC_KEY); 

    File privateFile = new File(PRIVATE_KEY); 

    try {

        toFile(publicFile, privateFile);

        publicEncode = readFile(publicFile);

        privateEncode = readFile(privateFile);

    } 
    catch (IOException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    } 

    try {

        rsaPubKey = toPublicKey(publicEncode);

        rsaPriKey = toPrivateKey(privateEncode);
    } 
    catch (InvalidKeySpecException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    } 
}

@Override
public CriptografadorBeanOut encrypt(CriptografadorBeanIn bean) throws CriptografadorException {

        byte[] cripto;

        CriptografadorBeanOut out = new CriptografadorBeanOut();

        try {

            cripto = cripto(rsaPubKey, bean.getPassPlainText());

            out.setPassCrypto(cripto);

            String encodeBase64String = Base64.encodeBase64String(cripto);

            out.setPassCryptoStringBytesBase64(encodeBase64String);
        } 
        catch (InvalidKeyException exception) {

            super.errorHandler(CriptografadorRSA.class,exception, null);
        } 
        catch (UnsupportedEncodingException exception) {

            super.errorHandler(CriptografadorRSA.class,exception, null);

        } 
        catch (IllegalBlockSizeException exception) {

            super.errorHandler(CriptografadorRSA.class,exception, null);

        } 
        catch (BadPaddingException exception) {

            super.errorHandler(CriptografadorRSA.class,exception, null);
        } 

        return out;
}

@Override
public CriptografadorBeanOut decrypt(CriptografadorBeanIn bean) throws CriptografadorException {

    byte[] descryptByte;

    CriptografadorBeanOut out = new CriptografadorBeanOut();

    try {

        byte[] byteValue = bean.getCryptoPass();

        if (byteValue == null || byteValue.length ==0){

            if (bean.getCryptoPassStringBytesBase64() != null ) {

                byteValue = Base64.decodeBase64(bean.getCryptoPassStringBytesBase64());
            }   
        }

        descryptByte = passDecrypt(rsaPriKey, byteValue);

        try {

            String descriptoString = new String (descryptByte, UTF_8);

            out.setPassPlainText(descriptoString);

        } 
        catch (UnsupportedEncodingException exception) {

            super.errorHandler(CriptografadorRSA.class,exception, null);
        }
    } 
    catch (InvalidKeyException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    } 
    catch (IllegalBlockSizeException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    } 
    catch (BadPaddingException exception) {

        super.errorHandler(CriptografadorRSA.class,exception, null);
    }

    return out;
}


private PublicKey toPublicKey(byte[] encoded) throws InvalidKeySpecException{ 

    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encoded); 

    return RSA_KEY_FACTORY.generatePublic(publicKeySpec);
}

private PrivateKey toPrivateKey(byte[] encoded) throws InvalidKeySpecException{

    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encoded); 

    return RSA_KEY_FACTORY.generatePrivate(privateKeySpec); 
}

private byte[] passDecrypt(PrivateKey privateKey, byte[] cipherText) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    CIPHER.init(Cipher.DECRYPT_MODE, privateKey); 

    return CIPHER.doFinal(cipherText); 
} 

private byte[] cripto(PublicKey publicKey,String word) throws UnsupportedEncodingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

    CIPHER.init(Cipher.ENCRYPT_MODE, publicKey);

    return CIPHER.doFinal(word.getBytes(UTF_8)); 


}

private KeyPair createKeyPair() {

    return KEY_PAIR_GENERATOR.generateKeyPair(); 
}

private void toFile(File publicFile, File privateFile) throws IOException {

    KeyPair keyPair = createKeyPair();

    FileOutputStream  streamPrivate = new FileOutputStream(privateFile);

    streamPrivate.write(keyPair.getPrivate().getEncoded()); 

    FileOutputStream  streamPublic = new FileOutputStream(publicFile);

    streamPublic.write(keyPair.getPublic().getEncoded()); 
}

private byte[] readFile(File file) throws IOException{

    byte[] datas=new byte[(int)file.length()];

    FileInputStream stream = new FileInputStream(file);

    stream.read(datas); 

    return datas;
}

}

4

0 回答 0