Soucis décryptage Blowfish Java
Bonjour,
J'espère avoir posté cela dans la bonne section ^^.
Voila je suis un train de faire un programme qui permet de crypter le contenue d'un fichier texte en blowfish et d’écrire le résultat dans un nouveau fichier.
Pour le cryptage j'ai de pas de soucis, cependant pour le décryptage il me met l'erreur suivante :
Code:
1 2 3 4 5
| javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at MyBlowfish.decryptInString(MyBlowfish.java:65)
at MyBlowfish.main(MyBlowfish.java:121) |
D’après les recherches que j'ai effectué le problème viendrai des byte [].
Je vous joint mon code entier en espérant que quelqu’un puisse m'éclairer :)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.nio.charset.Charset;
import javax.crypto.*;
import java.security.*;
import javax.crypto.spec.*;
public class MyBlowfish {
public final static int KEY_SIZE = 128; // [32..448]
private Key secretKey;
public MyBlowfish() {
}
public Key getSecretKey() {
return secretKey;
}
public byte[] crypt(byte[] plaintext, String key) {
Charset UTF8 = Charset.forName("UTF-8");
if (key.getBytes(UTF8).length > 8) {
key = key.substring(0, 8);
}
try {
secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(plaintext);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public byte[] crypt(String plaintext, String key) {
return crypt(plaintext.getBytes(Charset.forName("UTF-8")), key);
}
public byte[] decryptInBytes(byte[] ciphertext, String key) {
Charset UTF8 = Charset.forName("UTF-8");
if (key.getBytes(UTF8).length > 8) {
key = key.substring(0, 8);
}
try {
secretKey = new SecretKeySpec(key.getBytes(Charset.forName("UTF-8")), "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(ciphertext);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public String decryptInString(byte[] ciphertext, String key) {
return new String(decryptInBytes(ciphertext, key));
}
public String read(String path) {
String myText = "";
try {
InputStream ips = new FileInputStream(path);
InputStreamReader ipsr = new InputStreamReader(ips);
BufferedReader br = new BufferedReader(ipsr);
String ligne;
while ((ligne = br.readLine()) != null) {
myText += ligne + "\n";
}
br.close();
} catch (Exception e) {
System.out.println(e.toString());
}
return myText;
}
public void write(String path, String text) {
PrintWriter ecri;
try {
ecri = new PrintWriter(new FileWriter(path));
ecri.print(text);
ecri.flush();
ecri.close();
}// try
catch (NullPointerException a) {
System.out.println("Erreur : pointeur null");
} catch (IOException a) {
System.out.println("Problème d'IO");
}
}
public static void main(String[] args) {
if (args[0].equals("crypt")) {
MyBlowfish bf = new MyBlowfish();
String myText = bf.read(args[2]);
System.out.println("plaintext = " + myText);
String myKey = args[1];
byte[] crypted = bf.crypt(myText, myKey);
System.out.println("Your key: " + myKey);
System.out.println("text Crypted: " + new BigInteger(crypted));
System.out.println("text Crypted: " + new String(crypted));
bf.write(args[3], new String(crypted));
String textDecrypted = bf.decryptInString(crypted, myKey);
System.out.println("Text Decrypted: " + textDecrypted);
}else if (args[0].equals("decrypt")){
MyBlowfish bf = new MyBlowfish();
String myKey = args[1];
String myText = bf.read(args[2]);
byte[] myTextByte = myText.getBytes(Charset.forName("UTF-8"));
System.out.println("plaintext = " + myText);
System.out.println("plaintext = " + new BigInteger(myTextByte));
String textDecrypted = bf.decryptInString(myTextByte, myKey);
System.out.println("Your key: " + myKey);
System.out.println("Text Decrypted: " + textDecrypted);
bf.write(args[3], textDecrypted);
}
}
} |
Merci d'avance.