· 7 years ago · Nov 26, 2018, 05:22 PM
1package fintech;
2import java.security.InvalidAlgorithmParameterException;
3import java.security.InvalidKeyException;
4import java.security.NoSuchAlgorithmException;
5import java.util.Arrays;
6
7import javax.crypto.BadPaddingException;
8import javax.crypto.Cipher;
9import javax.crypto.IllegalBlockSizeException;
10import javax.crypto.NoSuchPaddingException;
11import javax.crypto.SecretKey;
12import javax.crypto.spec.SecretKeySpec;
13
14public class Util {
15 public static byte[] encrypt3DES(byte[] key1, byte[] key2, byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
16 Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
17 //create key
18 byte[] newKey = new byte[24];
19 // fill new key
20 for (int i = 0; i < newKey.length; i++) {
21 //fill one key of 24 with 2 keys of 8
22 }
23
24 SecretKey sKey = (SecretKey) new SecretKeySpec(newKey,"DESede");
25 cipher.init(Cipher.ENCRYPT_MODE, sKey);
26 return cipher.doFinal(data);
27 }
28 public static byte[] encryptDES(byte[] key, byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
29 Cipher cipher = Cipher.getInstance("DES/CBC/NoPadding");
30 //create key
31 SecretKey sKey = (SecretKey) new SecretKeySpec(key, "DES");
32 cipher.init(Cipher.ENCRYPT_MODE, sKey);
33 return cipher.doFinal(data);
34 }
35}