· 8 years ago · Dec 14, 2017, 01:20 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package javaapplication3;
7
8import java.io.*;
9import java.security.InvalidKeyException;
10import java.security.*;
11
12import javax.crypto.BadPaddingException;
13import javax.crypto.Cipher;
14import javax.crypto.IllegalBlockSizeException;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.spec.SecretKeySpec;
17
18/**
19 * A utility class that encrypts or decrypts a file.
20 * @author www.codejava.net
21 *
22 */
23public class CryptoUtils {
24
25 public static byte[] encrypt(String ALGORITHM, String key, byte[] inputBytes)
26 throws CryptoException {
27
28 if(ALGORITHM.equals("AES") || ALGORITHM.equals("Blowfish")){
29 if(key.length()>16)
30 key = key.substring(0,16);
31 else if(key.length()<16)
32 key = key + key.substring(0, 16-key.length() );
33 }
34 else if(ALGORITHM.equals("DES")){
35 if(key.length()>8)
36 key = key.substring(0,8);
37 else if(key.length()<8)
38 key = key + key.substring(0, 8-key.length() );
39
40 }
41
42 return doCrypto(ALGORITHM, Cipher.ENCRYPT_MODE, key, inputBytes);
43 }
44
45 public static byte[] decrypt(String ALGORITHM, String key, byte[] inputBytes)
46 throws CryptoException {
47
48 if(ALGORITHM.equals("AES") || ALGORITHM.equals("Blowfish")){
49 if(key.length()>16)
50 key = key.substring(0,16);
51 if(key.length()<16)
52 key = key + key.substring(0, 16-key.length() );
53 }
54 else if(ALGORITHM.equals("DES")){
55 if(key.length()>8)
56 key = key.substring(0,8);
57 else if(key.length()<8)
58 key = key + key.substring(0, 8-key.length() );
59
60 }
61 return doCrypto(ALGORITHM, Cipher.DECRYPT_MODE, key, inputBytes);
62 }
63
64
65 public static byte[] encryptRSA(PublicKey publicKey, String message) throws Exception {
66 Cipher cipher = Cipher.getInstance("RSA");
67 cipher.init(Cipher.ENCRYPT_MODE, publicKey);
68 return cipher.doFinal(message.getBytes());
69 }
70
71
72
73 public static byte[] decryptRSA(PrivateKey privateKey, byte [] encrypted) throws Exception {
74 Cipher cipher = Cipher.getInstance("RSA");
75 cipher.init(Cipher.DECRYPT_MODE, privateKey);
76 return cipher.doFinal(encrypted);
77 }
78
79 private static byte[] doCrypto(String ALGORITHM, int cipherMode, String key, byte[] inputBytes) throws CryptoException {
80 try {
81
82
83 Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
84 Cipher cipher = Cipher.getInstance(ALGORITHM);
85 cipher.init(cipherMode, secretKey);
86
87
88 byte[] outputBytes = cipher.doFinal(inputBytes);
89
90 return outputBytes;
91
92 } catch (NoSuchPaddingException | NoSuchAlgorithmException
93 | InvalidKeyException | BadPaddingException
94 | IllegalBlockSizeException ex) {
95
96 }
97
98
99 return null;
100 }
101
102 public class CryptoException extends Exception {
103
104 public CryptoException() {
105 }
106
107 public CryptoException(String message, Throwable throwable) {
108 super(message, throwable);
109 }
110}
111}