J'ai une pièce jointe au format .p7m et un fichier .pem contenant des clés privées et des certificats. OpenSSL je peux déchiffrer le fichier avec cette commande:Pièce jointe de courrier électronique décryptée Java (fichier .p7m)
openssl smime -decrypt -inform DER -in fileToDecrypt.p7m -inkey privateKey.pem -out destinationFile
Mais en utilisant bouncycastle en Java, je ne pouvais pas le déchiffrer. J'ai lu la clé privée avec ce code:
PEMReader pemReader = new PEMReader(new InputStreamReader(new FileInputStream(privateKeyName)));
Object obj;
PrivateKey key = null;
X509Certificate cert1 = null;
X509Certificate cert2 = null;
obj = pemReader.readObject();
if (obj instanceof PrivateKey) {
key = (PrivateKey) obj;
System.out.println("Private Key found");
}
obj = pemReader.readObject();
if(obj instanceof X509Certificate){
cert1 = (X509Certificate) obj;
System.out.println("cert found");
}
obj = pemReader.readObject();
if(obj instanceof X509Certificate){
cert2 = (X509Certificate) obj;
System.out.println("cert found");
}
Ceci affiche:
Private Key Found
cert found
cert found
Le type des clés sont:
System.out.println(key.getAlgorithm());
System.out.println(cert1.getSigAlgName());
System.out.println(cert2.getSigAlgName());
RSA
SHA256WithRSAEncryption
SHA256WithRSAEncryption
Si je tente de déchiffrer comme ceci:
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
Path path = Paths.get("fileToDecrypt.p7m");
byte[] data = Files.readAllBytes(path);
byte[] decryptedData = cipher.doFinal(data);
J'obtenir:
javax.crypto.IllegalBlockSizeException: Data must not be longer than 256 bytes
Je ces deux fichiers:
- fileToDecrypt.p7m
- privateKey.pem: contenant la clé privée RSA et deux certificats X508
Et I Je ne sais pas par où commencer quoi décrypter avec quoi, et comment?
vous pouvez vous répondre à la question propre;) –