Passed
Branch master (71558c)
by Armenak
02:56
created

encrypt(String,String)   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
dl 0
loc 13
rs 9.9
c 0
b 0
f 0
1
/*
2
 * Copyright 2014-2021, Armenak Grigoryan, and individual contributors as indicated
3
 * by the @authors tag. See the copyright.txt in the distribution for a
4
 * full listing of individual contributors.
5
 *
6
 * This is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation; either version 2.1 of
9
 * the License, or (at your option) any later version.
10
 *
11
 * This software is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
 * Lesser General Public License for more details.
15
 */
16
package com.strider.datadefender.utils;
17
18
import java.io.UnsupportedEncodingException;
19
20
import java.security.InvalidKeyException;
21
import java.security.MessageDigest;
22
import java.security.NoSuchAlgorithmException;
23
import java.util.Arrays;
24
import java.util.Base64;
25
26
import javax.crypto.BadPaddingException;
27
import javax.crypto.Cipher;
28
import javax.crypto.IllegalBlockSizeException;
29
import javax.crypto.NoSuchPaddingException;
30
import javax.crypto.spec.SecretKeySpec;
31
32
import lombok.extern.log4j.Log4j2;
33
34
/**
35
 * This class implements determenistic data anonymization 
36
 * by encoding the data using "salt" and decoding it.
37
 * 
38
 * @author Armenak Grigoryan
39
 */
40
@Log4j2
41
public final class Encoder {
42
    
43
    private static SecretKeySpec secretKey;
44
    private static byte[] key;
45
    
46
    /**
47
     * Empty constructor 
48
     */
49
    public Encoder() {
50
    }
51
    
52
    public static void setKey(String myKey) {
53
        MessageDigest sha = null;
54
        
55
        try {
56
            key = myKey.getBytes("UTF-8");
57
            sha = MessageDigest.getInstance("SHA-1");
58
            key = sha.digest(key);
59
            key = Arrays.copyOf(key, 16); 
60
            secretKey = new SecretKeySpec(key, "AES");
61
        } 
62
        catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
63
            log.debug(e);
64
        }
65
    }    
66
    
67
    
68
    public String encrypt(String strToEncrypt, String secret) {
69
        
70
      try {
71
            setKey(secret);
72
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
73
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
74
            return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
75
        } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | 
76
                 BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
77
            log.error("Error while encrypting: " + e.toString());
78
        }
79
      
80
        return null;
81
82
    }        
83
84
 
85
    public String decrypt(String strToDecrypt, String secret) {
86
        try {
87
            setKey(secret);
88
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
89
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
90
            return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
91
        } catch (InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | 
92
                 IllegalBlockSizeException | NoSuchPaddingException e) {
93
            log.error("Error while decrypting: " + e.toString());
94
        }
95
        return null;
96
    }
97
    
98
}
99