RSA BadPaddingException : les données doivent commencer avec zéro

J'essaie de mettre en œuvre un algorithme RSA dans un programme Java. Je suis confronté au "BadPaddingException : les données doivent commencer à zéro".
Ici sont les méthodes utilisées pour chiffrer et déchiffrer mes données :

public byte[] encrypt(byte[] input)  throws Exception
{
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");//
    cipher.init(Cipher.ENCRYPT_MODE, this.publicKey);
    return cipher.doFinal(input);
}

public byte[] decrypt(byte[] input)  throws Exception
{   
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");///
    cipher.init(Cipher.DECRYPT_MODE, this.privateKey);
    return cipher.doFinal(input);
}

privateKey et publicKey attributs sont lues à partir des fichiers de cette façon :

public PrivateKey readPrivKeyFromFile(String keyFileName) throws IOException {
    PrivateKey key = null;
    try {
        FileInputStream fin = new FileInputStream(keyFileName);
        ObjectInputStream ois = new ObjectInputStream(fin);
        BigInteger m = (BigInteger) ois.readObject();
        BigInteger e = (BigInteger) ois.readObject();
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        key = fact.generatePrivate(keySpec);
        ois.close();
    }
    catch (Exception e) {
       e.printStackTrace();
    }
    return key;
}

Clé privée et la clé Publique sont créés de cette façon :

public void Initialize() throws Exception
{
    KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
    keygen.initialize(2048);
    keyPair = keygen.generateKeyPair();
    KeyFactory fact = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pub = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class);
    RSAPrivateKeySpec priv = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class);

    saveToFile("public.key", pub.getModulus(), pub.getPublicExponent());
    saveToFile("private.key", priv.getModulus(), priv.getPrivateExponent());
}

et puis enregistrés dans des fichiers :

public void saveToFile(String fileName, BigInteger mod, BigInteger exp) throws IOException {
    FileOutputStream f = new FileOutputStream(fileName);
    ObjectOutputStream oos = new ObjectOutputStream(f);
    oos.writeObject(mod);
    oos.writeObject(exp);
    oos.close();
}

Je ne peux pas compris comment le problème viens de. Toute aide serait apprécié !

Merci d'avance.

  • Savez-vous que la ligne est la génération de l'exception?
  • Oui, c'est le retour de chiffrement.doFinal(entrée); l'enseignement dans les décrypter la méthode.
  • Hm. Et vous êtes sûr de la validité des données d'entrée?
InformationsquelleAutor Robin Monjo | 2010-04-26