Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.security.GeneralSecurityException;
  7. import java.security.MessageDigest;
  8. import javax.crypto.Cipher;
  9. import javax.crypto.SecretKey;
  10. import javax.crypto.SecretKeyFactory;
  11. import javax.crypto.spec.DESedeKeySpec;
  12. import javax.crypto.spec.IvParameterSpec;
  13.  
  14. public class TestEncryptor {
  15.  
  16. public static void main(String... args) {
  17. try {
  18. String KEY_STRING = "asdasdasd";
  19. byte[] key = getEnKey(KEY_STRING);
  20. String pFilePath = "D:\fileTest.png";
  21. String pFilePathEncryp = "D:\fileTestEncryp.png";
  22. byte[] archivoDecrypt = encryptFile(pFilePath, key);
  23.  
  24. try (FileOutputStream fos = new FileOutputStream(pFilePathEncryp)) {
  25. fos.write(archivoDecrypt);
  26. }
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30.  
  31. }
  32.  
  33. public static byte[] encryptFile(String pFilePath, byte[] pKey) throws GeneralSecurityException, IOException {
  34. File file = new File(pFilePath);
  35. long length = file.length();
  36. InputStream is = new FileInputStream(file);
  37.  
  38. // You cannot create an array using a long type.
  39. // It needs to be an int type.
  40. // Before converting to an int type, check
  41. // to ensure that file is not larger than Integer.MAX_VALUE.
  42. if (length > Integer.MAX_VALUE) {
  43. // File is too large
  44. }
  45.  
  46. // Create the byte array to hold the data
  47. byte[] bytes = new byte[(int) length];
  48.  
  49. // Read in the bytes
  50. int offset = 0;
  51. int numRead = 0;
  52. while (offset < bytes.length
  53. && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
  54. offset += numRead;
  55. }
  56.  
  57. // Close the input stream and return bytes
  58. is.close();
  59.  
  60. // Ensure all the bytes have been read in
  61. if (offset < bytes.length) {
  62.  
  63. throw new IOException("Could not completely read file " + file.getName());
  64. }
  65.  
  66. SecretKeyFactory lDESedeKeyFactory = SecretKeyFactory.getInstance("DESede");
  67. SecretKey kA = lDESedeKeyFactory.generateSecret(new DESedeKeySpec(pKey));
  68.  
  69. IvParameterSpec lIVSpec = new IvParameterSpec(new byte[8]);
  70. Cipher desedeCBCCipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
  71.  
  72. desedeCBCCipher.init(Cipher.ENCRYPT_MODE, kA, lIVSpec);
  73. // desedeCBCCipher.init(Cipher.DECRYPT_MODE, kA, lIVSpec);
  74. byte[] encrypted = desedeCBCCipher.doFinal(bytes);
  75.  
  76. return encrypted;
  77. }
  78.  
  79. private static byte[] getEnKey(String spKey) {
  80. byte[] desKey = null;
  81. try {
  82. byte[] desKey1 = md5(spKey);
  83. desKey = new byte[24];
  84. int i = 0;
  85. while (i < desKey1.length && i < 24) {
  86. desKey[i] = desKey1[i];
  87. i++;
  88. }
  89. if (i < 24) {
  90. desKey[i] = 0;
  91. i++;
  92. }
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96.  
  97. return desKey;
  98. }
  99.  
  100. private static byte[] md5(String strSrc) {
  101. byte[] returnByte = null;
  102. try {
  103. MessageDigest md5 = MessageDigest.getInstance("MD5");
  104. returnByte = md5.digest(strSrc.getBytes("GBK"));
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. return returnByte;
  109. }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement