Задача: Шифрование произвольных данных
Исходник: Реализация шифрования текста по алгаритму DESede, язык: java [code #610, hits: 9740]
аноним: skal [добавлен: 24.04.2010]
  1. import java.io.*;
  2. import javax.crypto.*;
  3. import javax.crypto.spec.*;
  4.  
  5. public class Encryptor
  6. {
  7. public static void main(String [] args)
  8. {
  9. FileOutputStream fos = null;
  10. String secret = "Шифруемый текст.";
  11. try
  12. {
  13. String keyfile = "file.txt";
  14. String algorithm = "DESede";
  15.  
  16. KeyGenerator kg = KeyGenerator.getInstance(algorithm);
  17. SecretKey key = kg.generateKey();
  18. Cipher cipher = Cipher.getInstance(algorithm);
  19. cipher.init(Cipher.ENCRYPT_MODE, key);
  20.  
  21. oos.writeObject(secret);
  22. fos = new FileOutputStream(keyfile);
  23. SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
  24. DESedeKeySpec keyspec = (DESedeKeySpec) skf.getKeySpec(key, DESedeKeySpec.class);
  25. fos.write(keyspec.getKey());
  26. fos.close();
  27. oos.close();
  28. }
  29. catch (Exception e)
  30. {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

+добавить реализацию