summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/thirdparty/src/main/java/org/openecomp/portalsdk/core/onboarding/crossapi/CipherUtil.java
blob: d355e102c48cecbdb3c30617fddb42b9b7e246d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.core.onboarding.crossapi;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class CipherUtil {
	
	private final static String key = "AGLDdG4D04BKm2IxIWEr8o==!"; 

	/**
	 * @param plainText
	 * @param secretKey
	 * @return encrypted version of plain text.
	 * @throws Exception 
	 */
	public static String encrypt(String plainText, String secretKey) throws Exception{
        byte[] rawKey;
        String encryptedString;
        SecretKeySpec sKeySpec;
        byte[] encryptText = plainText.getBytes("UTF-8");
        Cipher cipher;
        rawKey = Base64.decodeBase64(secretKey);
        sKeySpec = new SecretKeySpec(rawKey, "AES");
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
        encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText));
       
        return encryptedString;
    }

	/**
	 * 
	 * @param plainText
	 * @return Encrypted Text 
	 * @throws Exception
	 */
    public static String encrypt(String plainText) throws Exception
    {
    	return CipherUtil.encrypt(plainText,key);
    }

    /**
     * @param encryptedText
     * @param secretKey
     * @return plain text version of encrypted text
     * @throws Exception
     */
    public static String decrypt(String encryptedText, String secretKey) throws Exception {
        Cipher cipher;
        String encryptedString;
        byte[] encryptText = null;
        byte[] rawKey;
        SecretKeySpec sKeySpec;
        
        rawKey = Base64.decodeBase64(secretKey);          
        sKeySpec = new SecretKeySpec(rawKey, "AES");
        encryptText = Base64.decodeBase64(encryptedText.getBytes("UTF-8"));
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        encryptedString = new String(cipher.doFinal(encryptText));
        
        return encryptedString;
    }
    
    /**
     * @param encryptedText
     * @return Decrypted Text
     * @throws Exception
     */
    public static String decrypt(String encryptedText) throws Exception
    {
    	return CipherUtil.decrypt(encryptedText,key);
    }
    
    
    public static void main(String[] args) throws Exception {

		String password = "Welcome123";
		String encrypted;
		String decrypted;

		if (args.length != 2) {
			System.out.println("Default password testing... ");
			System.out.println("Plain password: " + password);
			encrypted = encrypt(password);
			System.out.println("Encrypted password: " + encrypted);
			decrypted = decrypt(encrypted);
			System.out.println("Decrypted  password: " + decrypted);
		} else {
			String whatToDo = args[0];
			if (whatToDo.equalsIgnoreCase("d")) {
				encrypted = args[1];
				System.out.println("Encrypted Text: " + encrypted);
				decrypted = decrypt(encrypted);
				System.out.println("Decrypted Text: " + decrypted);
			} else {
				decrypted = args[1];
				System.out.println("Plain Text: " + decrypted);				
				encrypted = encrypt(decrypted);
				System.out.println("Encrypted Text" + encrypted);
			}
		}
	}
}