Feb 8, 2013 - This is a simple encryption using Blowfish Algorithm that i use to encrypt several properties on my application. On this example im using.
Share the post 'Encrypt And Decrypt Using Blowfish In Java'
Java Cryptography classes offer a variety of ways for you to encrypt and decrypt. One of them is using Blowfish. The methods below encrypt and decrypt Strings using a String key as its secret key.
2 4 6 8 10 12 14 16 18 | publicstaticStringencryptBlowfish(Stringto_encrypt,Stringstrkey){ SecretKeySpec key=newSecretKeySpec(strkey.getBytes(),'Blowfish'); cipher.init(Cipher.ENCRYPT_MODE,key); returnnewString(cipher.doFinal(to_encrypt.getBytes())); } publicstaticStringdecryptBlowfish(Stringto_decrypt,Stringstrkey){ SecretKeySpec key=newSecretKeySpec(strkey.getBytes(),'Blowfish'); cipher.init(Cipher.DECRYPT_MODE,key); byte[]decrypted=cipher.doFinal(to_decrypt.getBytes()); }catch(Exceptione){returnnull;} |
![Crypt Blowfish Java Crypt Blowfish Java](/uploads/1/2/5/8/125832092/417798729.png)
Bonjour,
j'ai trouver un code de cryptage Blowfish qui crypte et décrypte un fichier.Le problème c'est que le fichier de cryptage s'affiche avec des caractères illisible.J'ai entendu de la classe Base64 mais j'arrive pas a la manipuler.Pouvez vous m'aider?
Voila le code
package application;
import java.io.BufferedReader;
import java.io.File;
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 DecrypBlowfish {
public final static int KEY_SIZE = 128; // [32..448]
private Key secretKey;
public DecrypBlowfish() {
}
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) throws IOException {
String fichier;
System.out.println('Quel est le nom de votre fichier ?');
fichier = LireString();
File f = new File(fichier);
File fl = new File('crepted.txt');
String path=fl.getCanonicalPath();
String filePath = f.getPath() ;
String p='azfgjkop';
DecrypBlowfish bf = new DecrypBlowfish();
String myText = bf.read(filePath);
byte[] cle = (new String(p)).getBytes(); // 24 caractères
SecretKey key = new SecretKeySpec(cle, 'Blowfish');
String myKey = key.toString();
byte[] crypted = bf.crypt(myText, myKey);
bf.write(path, new String(crypted));
String textDecrypted = bf.decryptInString(crypted, myKey);
File filed = new File('decrepted.txt');
String npath=filed.getCanonicalPath();
bf.write(npath, textDecrypted);
}
private static String LireString() {
String ligne_lue=null;
try{
InputStreamReader lecteur=new InputStreamReader(System.in);
BufferedReader entree=new BufferedReader(lecteur);
ligne_lue=entree.readLine();
}
catch(IOException err){
System.exit(0);
}
return ligne_lue;
}
}
j'ai trouver un code de cryptage Blowfish qui crypte et décrypte un fichier.Le problème c'est que le fichier de cryptage s'affiche avec des caractères illisible.J'ai entendu de la classe Base64 mais j'arrive pas a la manipuler.Pouvez vous m'aider?
Voila le code
package application;
import java.io.BufferedReader;
import java.io.File;
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 DecrypBlowfish {
public final static int KEY_SIZE = 128; // [32..448]
private Key secretKey;
public DecrypBlowfish() {
}
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) throws IOException {
String fichier;
System.out.println('Quel est le nom de votre fichier ?');
fichier = LireString();
File f = new File(fichier);
File fl = new File('crepted.txt');
String path=fl.getCanonicalPath();
String filePath = f.getPath() ;
String p='azfgjkop';
DecrypBlowfish bf = new DecrypBlowfish();
String myText = bf.read(filePath);
byte[] cle = (new String(p)).getBytes(); // 24 caractères
SecretKey key = new SecretKeySpec(cle, 'Blowfish');
String myKey = key.toString();
byte[] crypted = bf.crypt(myText, myKey);
bf.write(path, new String(crypted));
String textDecrypted = bf.decryptInString(crypted, myKey);
File filed = new File('decrepted.txt');
String npath=filed.getCanonicalPath();
bf.write(npath, textDecrypted);
}
private static String LireString() {
String ligne_lue=null;
try{
InputStreamReader lecteur=new InputStreamReader(System.in);
BufferedReader entree=new BufferedReader(lecteur);
ligne_lue=entree.readLine();
}
catch(IOException err){
System.exit(0);
}
return ligne_lue;
}
}