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
|
public class DES{
protected static String crypting(String textToCrypting, String keyString ) {
// Objet de cette classe offre des fonctionnalités pour
// Chiffrement et le déchiffrement.
Cipher cipher = null;
try {
// Paramètre "DES" spécifie le type de chiffrement que nous voulons créer
// Par la méthode de l'usine. Il comprend l'algorithme, le mode et
// Remplissage. Vous pouvez définir seul algorithme et, dans ce cas par défaut
// Valeurs seront utilisées pour la mode et de rembourrage.
cipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException ex) {
// Algorithme cryptographique demandé n'est pas disponible
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
// Demande un mécanisme de remplissage n'est pas disponible dans l'environnement.
ex.printStackTrace();
}
// La longueur de keystring est de 8. Il est caractéristique importante
// Pour le chiffrement
byte[] keyData = keyString.getBytes();
// Objet clé spécifie une clé secrète
// On utilise pour construire la clé secrète pour notre moteur de chiffrement à partir d'un octet
// Tableau
SecretKeySpec key = new SecretKeySpec(keyData, 0, keyData.length, "DES");
try {
// Initialisation de la mise en place le type d'opération, c'est pour être
// Utilisé pour. Dans ce cas - pour le chiffrement.
cipher.init(Cipher.ENCRYPT_MODE, key);
} catch (InvalidKeyException ex) {
// Donnée objet clé est inapproprié pour l'initialisation de ce chiffre
ex.printStackTrace();
}
int cypheredBytes = 0;
byte[] inputBytes = textToCrypting.getBytes();
byte[] outputBytes = new byte[100];
try {
// Méthode doFinal crypte les données au tableau outputBytes.
//Chiffres pour inconnu ou grandes quantités de données mise à jour méthode plus recommandé
// Nombre d'octets cryptés sauvegardés dans cypheredBytes variable
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length, outputBytes, 0);
} catch (IllegalStateException ex) {
ex.printStackTrace();
} catch (ShortBufferException ex) {
ex.printStackTrace();
} catch (IllegalBlockSizeException ex) {
ex.printStackTrace();
} catch (BadPaddingException ex) {
ex.printStackTrace();
}
String str = new String(outputBytes, 0, cypheredBytes);
inputBytes = str.getBytes();
return str;
}
// Changement d'état de l'objet de chiffrement pour le déchiffrement
protected static String decrypting(String textCrypting, String keyString ) {
//Object of this class provides the functionality for
//encryption and decryption.
Cipher cipher = null;
try {
//parameter "DES" specifies type of cipher we want to create
//through the factory method. It includes algorithm, mode and
//padding. You can define only algorithm and in that case default
//values will be used for mode and padding.
cipher = Cipher.getInstance("DES");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
} catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
//The lenght of keyString is 8. It's important characteristic
//for encryption
byte[] keyData = keyString.getBytes();
//key object specifies a secret key
//it uses to construct the secret key for our cipher object from a byte
//array
SecretKeySpec key = new SecretKeySpec(keyData, 0, keyData.length, "DES");
try {
//change state of the cipher object for decrypting
cipher.init(Cipher.DECRYPT_MODE, key);
} catch (InvalidKeyException ex) {
ex.printStackTrace();
}
int cypheredBytes = 0;
byte[] inputBytes = textCrypting.getBytes();
byte[] outputBytes = new byte[1000];
try {
//decrypts data
cypheredBytes = cipher.doFinal(inputBytes, 0, inputBytes.length,
outputBytes, 0);
} catch (IllegalStateException ex) {
} catch (ShortBufferException ex) {
} catch (IllegalBlockSizeException ex) {
} catch (BadPaddingException ex) {
}
String str = new String(outputBytes, 0, cypheredBytes);
return str;
}
} |
Partager