| 0. 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. | import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DesLib {
private DesLib() {}
public static final int ver = 1;
public static byte[] genKey() {
byte[] bytes = null;
try {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
SecretKey key = keyGen.generateKey();
bytes = key.getEncoded(); // get the bytes of the key
} catch (Exception e) {
System.err.println(e); }
return bytes;
}
public static SecretKey makeKey(byte[] key) {
return new SecretKeySpec(key, "DES");
}
public static SealedObject encrypt(SecretKey sk, Serializable s) {
SealedObject so = null;
try {
// Prepare the encrypter
Cipher enc = Cipher.getInstance("DES");
enc.init(Cipher.ENCRYPT_MODE, sk);
// Seal (encrypt) the object
so = new SealedObject(s, enc);
} catch (Exception e) {
System.err.println(e); }
return so;
}
public static Object decrypt(SecretKey sk, SealedObject so) {
Object o = null;
try {
// Get the algorithm used to seal the object
String algoName = so.getAlgorithm(); // DES
// Prepare the decrypter
Cipher dec = Cipher.getInstance("DES");
dec.init(Cipher.DECRYPT_MODE, sk);
// Unseal (decrypt) the class
o = so.getObject(dec);
} catch (Exception e) {
System.err.println(e); }
return o;
}
} |