St Thomas Aquinas enlightens us as follows:
God bless you!
Summa Theologica - The Minister of the Keys
Article 5. Whether wicked priests have the use of the keys?
Objection 1. It would seem that wicked priests have not the use of the keys. For in the passage where the use of the keys is bestowed on the apostles (John 20:22-23), the gift of the Holy Ghost is promised. But wicked men have not the Holy Ghost. Therefore they have not the use of the keys.
Objection 2. Further, no wise king entrusts his enemy with the dispensation of his treasure. Now the use of the keys consists in dispensing the treasure of the King of heaven, Who is Wisdom itself. Therefore the wicked, who are His enemies on account of sin, have not the use of the keys.
Objection 3. Further, Augustine says (De Bapt. v, 21) that God "gives the sacrament of grace even through wicked men, but grace itself only by Himself or through His saints." Hence He forgives sin by Himself, or by those who are members of the Dove. But the remission of sins is the use of the keys. Therefore sinners, who are not "members of the Dove," have not the use of the keys.
Objection 4. Further, the prayer of a wicked priest cannot effect reconciliation, for, as Gregory says (Pastor. i, 11), "if an unacceptable person is sent to intercede, anger is provoked to yet greater severity." But the use of the keys implies a kind of intercession, as appears in the form of absolution. Therefore wicked priests cannot use the keys effectively.
On the contrary, No man can know whether another man is in the state of grace. If, therefore, no one could use the keys in giving absolution unless he were in a state of grace, no one would know that he had been absolved, which would be very unfitting.
Further, the wickedness of the minister cannot void the liberality of his lord. But the priest is no more than a minister. Therefore he cannot by his wickedness take away from us the gift which God has given through him.
I answer that, Just as participation of a form to be induced into an effect does not make a thing to be an instrument, so neither does the loss of that form prevent that thing being used as an instrument. Consequently, since man is merely an instrument in the use of the keys, however much he may through sin be deprived of grace, whereby sins are forgiven, yet he is by no means deprived of the use of the keys.
Reply to Objection 1. The gift of the Holy Ghost is requisite for the use of the keys, not as being indispensable for the purpose, but because it is unbecoming for the user to use them without it, though he that submits to them receives their effect.
Reply to Objection 2. An earthly king can be cheated and deceived in the matter of his treasure, and so he does not entrust his enemy with the dispensation thereof. But the King of heaven cannot be cheated, because all tends to His own glory, even the abuse of the keys by some, for He can make good come out of evil, and produce many good effects through evil men. Hence the comparison fails.
Reply to Objection 3. Augustine speaks of the remission of sins in so far as holy men co-operate therein, not by virtue of the keys, but by merit of congruity. Hence He says that God confers the sacraments even through evil men, and among the other sacraments, absolution which is the use of the keys should be reckoned: but that through "members of the Dove," i.e. holy men, He grants forgiveness of sins, in so far as He remits sins on account of their intercession.
We might also reply that by "members of the Dove" he means all who are not cut off from the Church, for those who receive the sacraments from them, receive grace, whereas those who receive the sacraments from those who are cut off from the Church, do not receive grace, because they sin in so doing, except in the case of Baptism, which, in cases of necessity, may be received even from one who is excommunicate.
Reply to Objection 4. The prayer which the wicked priest proffers on his own account, is not efficacious: but that which he makes as a minister of the Church, is efficacious through the merit of Christ. Yet in both ways the priest's prayer should profit those who are subject to him.