Java实现DES加密解密代码

本文介绍了一个Java程序,该程序使用DES算法生成密钥并进行加密和解密操作。通过SealedObject实现了对象的加密,展示了如何创建加密对象、获取加密算法以及解密过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. import java.io.IOException;
  2. import java.io.Serializable;
  3. import java.security.InvalidKeyException;
  4. import java.security.NoSuchAlgorithmException;
  5. http://www.kmnk03.com/hxpfk/bdf/264.html
  6. import javax.crypto.BadPaddingException;
  7. import javax.crypto.Cipher;
  8. import javax.crypto.IllegalBlockSizeException;
  9. import javax.crypto.KeyGenerator;
  10. import javax.crypto.NoSuchPaddingException;
  11. import javax.crypto.SealedObject;
  12. import javax.crypto.SecretKey;
  13. http://www.kmnk03.com/hxpfk/qcd/265.html
  14. public class EncryptDecryptObjectWithDES {
  15. private static Cipher ecipher;
  16. private static Cipher dcipher;
  17. private static SecretKey key;
  18. public static void main(String[] args) {
  19. try {http://www.kmnk03.com/hxpfk/qcd/266.html
  20. // generate secret key using DES algorithm
  21. key = KeyGenerator.getInstance("DES").generateKey();
  22. ecipher = Cipher.getInstance("DES");
  23. dcipher = Cipher.getInstance("DES");
  24. // initialize the ciphers with the given key
  25. ecipher.init(Cipher.ENCRYPT_MODE, key);
  26. dcipher.init(Cipher.DECRYPT_MODE, key);
  27. // create a sealed object
  28. SealedObject sealed = new SealedObject(new SecretObject("My secret message"), ecipher);
  29. http://www.kmnk03.com/hxpfk/pfsy/268.html
  30. // get the algorithm with the object has been sealed
  31. String algorithm = sealed.getAlgorithm();
  32. System.out.println("Algorithm " + algorithm);
  33. // unseal (decrypt) the object
  34. SecretObject o = (SecretObject) sealed.getObject(dcipher);
  35. System.out.println("Original Object: " + o);
  36. }http://www.kmnk03.com/hxpfk/xmz/269.html
  37. catch (NoSuchAlgorithmException e) {
  38. System.out.println("No Such Algorithm:" + e.getMessage());
  39. return;
  40. }
  41. catch (NoSuchPaddingException e) {
  42. System.out.println("No Such Padding:" + e.getMessage());
  43. return;
  44. }
  45. catch (BadPaddingException e) {
  46. System.out.println("Bad Padding:" + e.getMessage());
  47. return;
  48. }http://www.kmnk03.com/hxpfk/qcd/267.html
  49. catch (InvalidKeyException e) {
  50. System.out.println("Invalid Key:" + e.getMessage());
  51. return;
  52. }
  53. catch (IllegalBlockSizeException e) {
  54. System.out.println("Illegal Block:" + e.getMessage());
  55. return;
  56. }
  57. catch (ClassNotFoundException e) {
  58. System.out.println("Class Not Found:" + e.getMessage());
  59. return;
  60. }http://www.kmnk03.com/hxpfk/tf/270.html
  61. catch (IOException e) {
  62. System.out.println("I/O Error:" + e.getMessage());
  63. return;
  64. }
  65. http://www.kmnk03.com/hxpfk/npx/273.html
  66. }
  67. public static class SecretObject implements Serializable {
  68. private static final long serialVersionUID = -1335351770906357695L;
  69. private final String message;
  70. http://www.kmnk03.com/hxpfk/ylb/271.html
  71. public SecretObject(String message) {
  72. this.message = message;
  73. }
  74. @Override
  75. public String toString() {
  76. return "SecretObject [message=" + message + "]";
  77. }
  78. }
  79. http://www.kmnk03.com/hxpfk/ylb/272.html
  80. }kmnk03.com
  81. www.kmnk03.com
复制代码

输出:

  1. Algorithm DES
  2. Original Object: SecretObject [message=My secret message]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值