[Java学习] java文本文件加密解密类

来源:互联网 发布:spark安装windows 编辑:程序博客网 时间:2024/05/22 17:13
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;http://www.kmnk01.com/hxpfk/2015/jq_1124/96.html
  5. import java.security.*;
  6. import javax.crypto.*;
  7. import javax.crypto.spec.*;
  8. /**
  9. * 文本文件加密解密类
  10. *
  11. * 文件名:FileEncrypter.java JDK:1.40以上 说明:文件加密 加密方法:三重DES加密http://www.kmnk01.com/hxpfk/2015/jq_1124/97.html
  12. * 加密过程:对选中的文件加密后在同文件夹下生成一个增加了".tdes" 扩展名的加密文件
  13. *
  14. * 解密过程:对选中的加密文件(必须有".tdes"扩展名)进行解密
  15. */
  16. public class Test extends JFrame
  17. {
  18. public static final int WIDTH = 550;
  19. public static final int HEIGHT = 200;
  20. public static void main ( String args[] )
  21. {
  22. Test fe = new Test();
  23. fe.show();
  24. }
  25. http://www.kmnk01.com/hxpfk/2015/jc_1124/98.html
  26. Test()
  27. {
  28. this.setSize ( WIDTH, HEIGHT );
  29. this.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
  30. this.setResizable ( false );
  31. Toolkit tk = Toolkit.getDefaultToolkit();
  32. Dimension screenSize = tk.getScreenSize();
  33. this.setLocation ( ( screenSize.width - WIDTH ) / 2,
  34. ( screenSize.height - HEIGHT ) / 2 );
  35. this.setTitle ( "文件加密器(TriDES)" );
  36. Container c = this.getContentPane();
  37. c.setLayout ( new FlowLayout() );
  38. final FilePanel fp = new FilePanel ( "文件选择" );
  39. c.add ( fp );
  40. final KeyPanel pp = new KeyPanel ( "密码" );
  41. c.add ( pp );
  42. JButton jbE = new JButton ( "加密" );
  43. c.add ( jbE );
  44. jbE.addActionListener ( new ActionListener()
  45. {
  46. public void actionPerformed ( ActionEvent event )
  47. {
  48. File file = new File ( fp.getFileName() );
  49. if ( file.exists() )
  50. encrypt ( file.getAbsoluteFile(), pp.getKey() );
  51. else
  52. JOptionPane.showMessageDialog ( null, "请选择文件!", "提示",
  53. JOptionPane.OK_OPTION );http://www.kmnk01.com/hxpfk/2015/jc_1124/98.html
  54. }
  55. } );
  56. JButton jbD = new JButton ( "解密" );
  57. c.add ( jbD );
  58. jbD.addActionListener ( new ActionListener()
  59. {
  60. public void actionPerformed ( ActionEvent event )
  61. {
  62. File file = new File ( fp.getFileName() );
  63. if ( file.exists() )
  64. decrypt ( file.getAbsoluteFile(), pp.getKey() );
  65. else
  66. JOptionPane.showMessageDialog ( null, "请选择文件!", "提示",
  67. JOptionPane.OK_OPTION );
  68. }
  69. } );
  70. }
  71. /**
  72. * 加密函数 输入: 要加密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
  73. * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 其中: AD67EA2F3BE6E5AD
  74. * DES密码一 D368DFE03120B5DF DES密码二 92A8FD8FEC2F0746 DES密码三 输出:
  75. * 对输入的文件加密后,保存到同一文件夹下增加了".tdes"扩展名的文件中。
  76. */
  77. private void encrypt ( File fileIn, String sKey )
  78. {
  79. try
  80. {
  81. if ( sKey.length() == 48 )
  82. {
  83. byte[] bytK1 = getKeyByStr ( sKey.substring ( 0, 16 ) );
  84. byte[] bytK2 = getKeyByStr ( sKey.substring ( 16, 32 ) );
  85. byte[] bytK3 = getKeyByStr ( sKey.substring ( 32, 48 ) );
  86. FileInputStream fis = new FileInputStream ( fileIn );
  87. byte[] bytIn = new byte[ ( int ) fileIn.length() ];
  88. for ( int i = 0; i < fileIn.length(); i++ )
  89. {
  90. bytIn[i] = ( byte ) fis.read();
  91. }
  92. // 加密
  93. byte[] bytOut = encryptByDES (
  94. encryptByDES ( encryptByDES ( bytIn, bytK1 ), bytK2 ), bytK3 );
  95. String fileOut = fileIn.getPath() + ".tdes";
  96. FileOutputStream fos = new FileOutputStream ( fileOut );
  97. for ( int i = 0; i < bytOut.length; i++ )
  98. {
  99. fos.write ( ( int ) bytOut[i] );
  100. }
  101. fos.close();
  102. JOptionPane.showMessageDialog ( this, "加密成功!", "提示",
  103. JOptionPane.OK_OPTION );
  104. }
  105. else
  106. JOptionPane.showMessageDialog ( this, "密码长度必须等于48!", "错误信息",
  107. JOptionPane.ERROR_MESSAGE );
  108. }
  109. catch ( Exception e )
  110. {
  111. e.printStackTrace();
  112. }
  113. }
  114. http://www.kmnk01.com/hxpfk/2015/jq_1124/100.html
  115. /**
  116. * 解密函数 输入: 要解密的文件,密码(由0-F组成,共48个字符,表示3个8位的密码)如:
  117. * AD67EA2F3BE6E5ADD368DFE03120B5DF92A8FD8FEC2F0746 其中: AD67EA2F3BE6E5AD
  118. * DES密码一 D368DFE03120B5DF DES密码二 92A8FD8FEC2F0746 DES密码三 输出:
  119. * 对输入的文件解密后,保存到用户指定的文件中。
  120. */
  121. private void decrypt ( File fileIn, String sKey )
  122. {
  123. try
  124. {
  125. if ( sKey.length() == 48 )
  126. {
  127. String strPath = fileIn.getPath();
  128. if ( strPath.substring ( strPath.length() - 5 ).toLowerCase()
  129. .equals ( ".tdes" ) )
  130. strPath = strPath.substring ( 0, strPath.length() - 5 );
  131. else
  132. {
  133. JOptionPane.showMessageDialog ( this, "不是合法的加密文件!", "提示",
  134. JOptionPane.OK_OPTION );
  135. return;
  136. }
  137. JFileChooser chooser = new JFileChooser();
  138. chooser.setCurrentDirectory ( new File ( "." ) );
  139. chooser.setSelectedFile ( new File ( strPath ) );
  140. // 用户指定要保存的文件
  141. int ret = chooser.showSaveDialog ( this );
  142. if ( ret == JFileChooser.APPROVE_OPTION )
  143. {
  144. byte[] bytK1 = getKeyByStr ( sKey.substring ( 0, 16 ) );
  145. byte[] bytK2 = getKeyByStr ( sKey.substring ( 16, 32 ) );
  146. byte[] bytK3 = getKeyByStr ( sKey.substring ( 32, 48 ) );
  147. FileInputStream fis = new FileInputStream ( fileIn );
  148. byte[] bytIn = new byte[ ( int ) fileIn.length() ];
  149. for ( int i = 0; i < fileIn.length(); i++ )
  150. {
  151. bytIn[i] = ( byte ) fis.read();
  152. }
  153. // 解密
  154. byte[] bytOut = decryptByDES (
  155. decryptByDES ( decryptByDES ( bytIn, bytK3 ), bytK2 ),
  156. bytK1 );
  157. File fileOut = chooser.getSelectedFile();
  158. fileOut.createNewFile();
  159. FileOutputStream fos = new FileOutputStream ( fileOut );
  160. for ( int i = 0; i < bytOut.length; i++ )
  161. {
  162. fos.write ( ( int ) bytOut[i] );
  163. }
  164. fos.close();
  165. JOptionPane.showMessageDialog ( this, "解密成功!", "提示",
  166. JOptionPane.OK_OPTION );
  167. }
  168. }
  169. else
  170. JOptionPane.showMessageDialog ( this, "密码长度必须等于48!", "错误信息",
  171. JOptionPane.ERROR_MESSAGE );
  172. }
  173. catch ( Exception e )
  174. {
  175. JOptionPane.showMessageDialog ( this, "解密失败,请核对密码!", "提示",
  176. JOptionPane.OK_OPTION );
  177. }
  178. }
  179. /**
  180. * 用DES方法加密输入的字节 bytKey需为8字节长,是加密的密码
  181. */
  182. private byte[] encryptByDES ( byte[] bytP, byte[] bytKey ) throws Exception
  183. {
  184. DESKeySpec desKS = new DESKeySpec ( bytKey );
  185. SecretKeyFactory skf = SecretKeyFactory.getInstance ( "DES" );
  186. SecretKey sk = skf.generateSecret ( desKS );
  187. Cipher cip = Cipher.getInstance ( "DES" );
  188. cip.init ( Cipher.ENCRYPT_MODE, sk );
  189. return cip.doFinal ( bytP );http://www.kmnk01.com/hxpfk/2015/bt_1123/92.html
  190. }
  191. /**
  192. * 用DES方法解密输入的字节 bytKey需为8字节长,是解密的密码
  193. */
  194. private byte[] decryptByDES ( byte[] bytE, byte[] bytKey ) throws Exception
  195. {
  196. DESKeySpec desKS = new DESKeySpec ( bytKey );
  197. SecretKeyFactory skf = SecretKeyFactory.getInstance ( "DES" );
  198. SecretKey sk = skf.generateSecret ( desKS );
  199. Cipher cip = Cipher.getInstance ( "DES" );
  200. cip.init ( Cipher.DECRYPT_MODE, sk );
  201. return cip.doFinal ( bytE );
  202. }
  203. /**
  204. * 输入密码的字符形式,返回字节数组形式。 如输入字符串:AD67EA2F3BE6E5AD 返回字节数组:{
  205. * 173,103,234,47,59,230,229,173 }
  206. */
  207. private byte[] getKeyByStr ( String str )
  208. {
  209. byte[] bRet = new byte[str.length() / 2];
  210. for ( int i = 0; i < str.length() / 2; i++ )
  211. {
  212. Integer itg = new Integer ( 16 * getChrInt ( str.charAt ( 2 * i ) )
  213. + getChrInt ( str.charAt ( 2 * i + 1 ) ) );
  214. bRet[i] = itg.byteValue();
  215. }
  216. return bRet;
  217. }
  218. /**
  219. * 计算一个16进制字符的10进制值 输入:0-F
  220. */
  221. private int getChrInt ( char chr )
  222. {
  223. int iRet = 0;
  224. if ( chr == "0".charAt ( 0 ) )
  225. iRet = 0;
  226. if ( chr == "1".charAt ( 0 ) )
  227. iRet = 1;
  228. if ( chr == "2".charAt ( 0 ) )
  229. iRet = 2;
  230. if ( chr == "3".charAt ( 0 ) )
  231. iRet = 3;
  232. if ( chr == "4".charAt ( 0 ) )
  233. iRet = 4;
  234. if ( chr == "5".charAt ( 0 ) )
  235. iRet = 5;
  236. if ( chr == "6".charAt ( 0 ) )
  237. iRet = 6;
  238. if ( chr == "7".charAt ( 0 ) )
  239. iRet = 7;
  240. if ( chr == "8".charAt ( 0 ) )
  241. iRet = 8;
  242. if ( chr == "9".charAt ( 0 ) )
  243. iRet = 9;
  244. if ( chr == "A".charAt ( 0 ) )
  245. iRet = 10;
  246. if ( chr == "B".charAt ( 0 ) )
  247. iRet = 11;
  248. if ( chr == "C".charAt ( 0 ) )
  249. iRet = 12;
  250. if ( chr == "D".charAt ( 0 ) )
  251. iRet = 13;
  252. if ( chr == "E".charAt ( 0 ) )
  253. iRet = 14;
  254. if ( chr == "F".charAt ( 0 ) )
  255. iRet = 15;
  256. return iRet;
  257. }
  258. }
  259. /**
  260. * 文件选择组件。
  261. */
  262. class FilePanel extends JPanel
  263. {
  264. FilePanel ( String str )
  265. {
  266. JLabel label = new JLabel ( str );
  267. JTextField fileText = new JTextField ( 35 );
  268. JButton chooseButton = new JButton ( "浏览..." );
  269. this.add ( label );
  270. this.add ( fileText );
  271. this.add ( chooseButton );
  272. clickAction ca = new clickAction ( this );
  273. chooseButton.addActionListener ( ca );
  274. }
  275. public String getFileName()
  276. {
  277. JTextField jtf = ( JTextField ) this.getComponent ( 1 );
  278. return jtf.getText();
  279. }http://www.kmnk01.com/hxpfk/2015/jq_1124/102.html
  280. private class clickAction implements ActionListener
  281. {
  282. clickAction ( Component c )
  283. {
  284. cmpt = c;
  285. }
  286. public void actionPerformed ( ActionEvent event )
  287. {
  288. JFileChooser chooser = new JFileChooser();
  289. chooser.setCurrentDirectory ( new File ( "." ) );
  290. int ret = chooser.showOpenDialog ( cmpt );
  291. if ( ret == JFileChooser.APPROVE_OPTION )
  292. {
  293. JPanel jp = ( JPanel ) cmpt;
  294. JTextField jtf = ( JTextField ) jp.getComponent ( 1 );
  295. jtf.setText ( chooser.getSelectedFile().getPath() );
  296. }
  297. }
  298. private Component cmpt;
  299. }
  300. }
  301. /**
  302. * 密码生成组件。
  303. */
  304. class KeyPanel extends JPanel
  305. {
  306. KeyPanel ( String str )
  307. {
  308. JLabel label = new JLabel ( str );
  309. JTextField fileText = new JTextField ( 35 );
  310. JButton chooseButton = new JButton ( "随机产生" );
  311. this.add ( label );
  312. this.add ( fileText );
  313. this.add ( chooseButton );
  314. clickAction ca = new clickAction ( this );
  315. chooseButton.addActionListener ( ca );
  316. }
  317. // 返回生成的密码(48个字符长度)
  318. public String getKey()
  319. {
  320. JTextField jtf = ( JTextField ) this.getComponent ( 1 );
  321. return jtf.getText();
  322. }
  323. private class clickAction implements ActionListener
  324. {
  325. clickAction ( Component c )
  326. {
  327. cmpt = c;
  328. }
  329. public void actionPerformed ( ActionEvent event )
  330. {
  331. try
  332. {
  333. KeyGenerator kg = KeyGenerator.getInstance ( "DES" );
  334. kg.init ( 56 );
  335. Key ke = kg.generateKey();
  336. byte[] bytK1 = ke.getEncoded();
  337. ke = kg.generateKey();
  338. byte[] bytK2 = ke.getEncoded();
  339. ke = kg.generateKey();
  340. byte[] bytK3 = ke.getEncoded();
  341. JPanel jp = ( JPanel ) cmpt;
  342. JTextField jtf = ( JTextField ) jp.getComponent ( 1 );
  343. jtf.setText ( getByteStr ( bytK1 ) + getByteStr ( bytK2 )
  344. + getByteStr ( bytK3 ) );
  345. }
  346. catch ( Exception e )
  347. {
  348. e.printStackTrace();
  349. }
  350. }
  351. private String getByteStr ( byte[] byt )
  352. {
  353. String strRet = "";
  354. for ( int i = 0; i < byt.length; i++ )
  355. {
  356. // System.out.println(byt[i]);
  357. strRet += getHexValue ( ( byt[i] & 240 ) / 16 );
  358. strRet += getHexValue ( byt[i] & 15 );
  359. }
  360. return strRet;
  361. }
  362. private String getHexValue ( int s )
  363. {
  364. String sRet = null;
  365. switch ( s )
  366. {
  367. case 0:
  368. sRet = "0";
  369. break;
  370. case 1:
  371. sRet = "1";
  372. break;
  373. case 2:
  374. sRet = "2";
  375. break;
  376. case 3:
  377. sRet = "3";
  378. break;
  379. case 4:
  380. sRet = "4";
  381. break;http://www.kmnk01.com/hxpfk/2015/py_1124/103.html
  382. case 5:
  383. sRet = "5";
  384. break;
  385. case 6:
  386. sRet = "6";
  387. break;
  388. case 7:http://www.kmnk01.com/hxpfk/2015/hzj_1124/99.html
  389. sRet = "7";
  390. break;
  391. case 8:
  392. sRet = "8";
  393. break;
  394. case 9:
  395. sRet = "9";
  396. break;
  397. case 10:
  398. sRet = "A";
  399. break;
  400. case 11:
  401. sRet = "B";
  402. break;
  403. case 12:
  404. sRet = "C";
  405. break;
  406. case 13:
  407. sRet = "D";
  408. break;
  409. case 14:
  410. sRet = "E";
  411. break;
  412. case 15:
  413. sRet = "F";
  414. }
  415. return sRet;
  416. }
  417. private Component cmpt;
  418. }
  419. }
0 0
原创粉丝点击