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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
/**
* Utilities pour crypter/décrypter
*
*@author Joe
*/
public class DESUtilities {
private static DESUtilities instance;
/**
* Encrypte avec la clé générée
*
*@param key Description of Parameter
*@param message Description of Parameter
*@return Description of the Returned Value
*/
public String encrypt(Key key, String message) {
// get a encrypter object
DESEncrypter encrypter = new DESEncrypter();
// set the key
encrypter.setKey(key);
//crypt
String encryptedMessage = encrypter.encrypt(message);
return encryptedMessage;
}
/**
* Envoie la clé pour permmettre la phase de decryptage
*
*@param keyByte Description of Parameter
*/
public void shareKey(byte[] keyByte) {
// e.g. envoie par mail
}
/**
* Décrypte avec la clé générée
*
*@param keyByte Description of Parameter
*@param encryptedMessage Description of Parameter
*@return Description of the Returned Value
*/
public String decrypt(byte[] keyByte, String encryptedMessage) {
// get a decrspter object
DESDecrypter decrypter = new DESDecrypter();
//set key
decrypter.setKey(keyByte);
//decrypt
String message = decrypter.decrypt(encryptedMessage);
return message;
}
/**
* Génère une clé symétrique avec DES
*
*@return Description of the Returned Value
*/
public Key generateKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(56);
// 56 is the keysize. Fixed for DES
Key key = kg.generateKey();
return key;
}
catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
return null;
}
}
/**
* Gets the Instance attribute of the DESUtilities object
*
*@return The Instance value
*/
public static DESUtilities getInstance() {
if (instance == null) {
instance = new DESUtilities();
}
return instance;
}
/**
* Encrypter
*
*@author Joe
*/
class DESEncrypter {
private Cipher ecipher;
/**
* Constructor for the DESEncrypter object
*/
public DESEncrypter() {
try {
ecipher = Cipher.getInstance("DES");
}
catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
}
/**
* Sets the Key attribute of the DESEncrypter object
*
*@param key The new Key value
*/
public void setKey(Key key) {
try {
ecipher.init(Cipher.ENCRYPT_MODE, key);
}
catch (InvalidKeyException ex) {
ex.printStackTrace();
}
}
/**
* Crypte le message
*
*@param str message à crypter
*@return message crypté
*/
public String encrypt(String str) {
try {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (BadPaddingException ex) {
ex.printStackTrace();
return null;
}
catch (IllegalBlockSizeException ex) {
ex.printStackTrace();
return null;
}
catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
return null;
}
}
}
/**
* Decrytpe message
*
*@author Joe
*/
class DESDecrypter {
private Cipher dcipher;
/**
* Constructor for the DESEncrypter object
*/
public DESDecrypter() {
try {
dcipher = Cipher.getInstance("DES");
}
catch (NoSuchPaddingException ex) {
ex.printStackTrace();
}
catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
}
/**
* Sets the Key attribute of the DESEncrypter object
*
*@param keyByte The new Key value
*/
public void setKey(byte[] keyByte) {
try {
//generate key from byte
javax.crypto.spec.DESKeySpec sks = new javax.crypto.spec.DESKeySpec(keyByte);
SecretKey sKey = SecretKeyFactory.getInstance("DES").generateSecret(sks);
//set key
dcipher.init(Cipher.DECRYPT_MODE, sKey);
}
catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
catch (InvalidKeySpecException ex) {
ex.printStackTrace();
}
catch (InvalidKeyException ex) {
ex.printStackTrace();
}
}
/**
* Decrypte le message
*
*@param str message à décrypter
*@return message décrypté
*/
public String decrypt(String str) {
try {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch (javax.crypto.BadPaddingException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
}
} |
Partager