1616// under the License.
1717package com .cloud .consoleproxy ;
1818
19- import java .security .InvalidAlgorithmParameterException ;
20- import java .security .InvalidKeyException ;
21- import java .security .NoSuchAlgorithmException ;
22-
23- import javax .crypto .BadPaddingException ;
24- import javax .crypto .Cipher ;
25- import javax .crypto .IllegalBlockSizeException ;
26- import javax .crypto .NoSuchPaddingException ;
27- import javax .crypto .spec .IvParameterSpec ;
28- import javax .crypto .spec .SecretKeySpec ;
29-
3019import org .apache .commons .codec .binary .Base64 ;
3120import org .apache .log4j .Logger ;
3221
3322import com .google .gson .Gson ;
3423import com .google .gson .GsonBuilder ;
3524
36- /**
37- *
38- * @author Kelven Yang
39- * A simple password based encyrptor based on AES/CBC. It can serialize simple POJO object into URL safe string
40- * and deserialize it back.
41- *
42- */
25+ import com .cloud .utils .crypt .AeadBase64Encryptor ;
26+ import com .cloud .utils .crypt .Base64Encryptor ;
27+
4328public class ConsoleProxyPasswordBasedEncryptor {
4429 private static final Logger s_logger = Logger .getLogger (ConsoleProxyPasswordBasedEncryptor .class );
4530
@@ -57,65 +42,16 @@ public String encryptText(String text) {
5742 if (text == null || text .isEmpty ())
5843 return text ;
5944
60- try {
61- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
62- SecretKeySpec keySpec = new SecretKeySpec (keyIvPair .getKeyBytes (), "AES" );
63-
64- cipher .init (Cipher .ENCRYPT_MODE , keySpec , new IvParameterSpec (keyIvPair .getIvBytes ()));
65-
66- byte [] encryptedBytes = cipher .doFinal (text .getBytes ());
67- return Base64 .encodeBase64URLSafeString (encryptedBytes );
68- } catch (NoSuchAlgorithmException e ) {
69- s_logger .error ("Unexpected exception " , e );
70- return null ;
71- } catch (NoSuchPaddingException e ) {
72- s_logger .error ("Unexpected exception " , e );
73- return null ;
74- } catch (IllegalBlockSizeException e ) {
75- s_logger .error ("Unexpected exception " , e );
76- return null ;
77- } catch (BadPaddingException e ) {
78- s_logger .error ("Unexpected exception " , e );
79- return null ;
80- } catch (InvalidKeyException e ) {
81- s_logger .error ("Unexpected exception " , e );
82- return null ;
83- } catch (InvalidAlgorithmParameterException e ) {
84- s_logger .error ("Unexpected exception " , e );
85- return null ;
86- }
45+ Base64Encryptor encryptor = new AeadBase64Encryptor (keyIvPair .getKeyBytes (), keyIvPair .getIvBytes ());
46+ return encryptor .encrypt (text );
8747 }
8848
8949 public String decryptText (String encryptedText ) {
9050 if (encryptedText == null || encryptedText .isEmpty ())
9151 return encryptedText ;
9252
93- try {
94- Cipher cipher = Cipher .getInstance ("AES/CBC/PKCS5Padding" );
95- SecretKeySpec keySpec = new SecretKeySpec (keyIvPair .getKeyBytes (), "AES" );
96- cipher .init (Cipher .DECRYPT_MODE , keySpec , new IvParameterSpec (keyIvPair .getIvBytes ()));
97-
98- byte [] encryptedBytes = Base64 .decodeBase64 (encryptedText );
99- return new String (cipher .doFinal (encryptedBytes ));
100- } catch (NoSuchAlgorithmException e ) {
101- s_logger .error ("Unexpected exception " , e );
102- return null ;
103- } catch (NoSuchPaddingException e ) {
104- s_logger .error ("Unexpected exception " , e );
105- return null ;
106- } catch (IllegalBlockSizeException e ) {
107- s_logger .error ("Unexpected exception " , e );
108- return null ;
109- } catch (BadPaddingException e ) {
110- s_logger .error ("Unexpected exception " , e );
111- return null ;
112- } catch (InvalidKeyException e ) {
113- s_logger .error ("Unexpected exception " , e );
114- return null ;
115- } catch (InvalidAlgorithmParameterException e ) {
116- s_logger .error ("Unexpected exception " , e );
117- return null ;
118- }
53+ Base64Encryptor encryptor = new AeadBase64Encryptor (keyIvPair .getKeyBytes (), keyIvPair .getIvBytes ());
54+ return encryptor .decrypt (encryptedText );
11955 }
12056
12157 public <T > String encryptObject (Class <?> clz , T obj ) {
0 commit comments