@@ -25,8 +25,9 @@ Licensed to the Apache Software Foundation (ASF) under one
2525import javax .crypto .spec .IvParameterSpec ;
2626import javax .crypto .spec .PBEKeySpec ;
2727import javax .crypto .spec .SecretKeySpec ;
28+ import javax .inject .Named ;
29+ import javax .inject .Singleton ;
2830
29- import java .nio .charset .Charset ;
3031import java .nio .charset .StandardCharsets ;
3132import java .security .InvalidAlgorithmParameterException ;
3233import java .security .InvalidKeyException ;
@@ -38,107 +39,74 @@ Licensed to the Apache Software Foundation (ASF) under one
3839
3940import org .codehaus .plexus .components .cipher .PlexusCipherException ;
4041
41- /**
42- * This class is thread-safe.
43- *
44- * @author Oleg Gusakov
45- */
46- public class PBECipher {
47- protected static final Charset STRING_ENCODING = StandardCharsets .UTF_8 ;
48- protected static final int SPICE_SIZE = 16 ;
49- protected static final int SALT_SIZE = 8 ;
50- protected static final int CHUNK_SIZE = 16 ;
51- protected static final String KEY_ALG = "AES" ;
52- protected static final String CIPHER_ALG = "AES/CBC/PKCS5Padding" ;
53- protected static final int PBE_ITERATIONS = 310000 ;
42+ @ Singleton
43+ @ Named (AESCBCPKCS5Padding .CIPHER_ALG )
44+ public class AESCBCPKCS5Padding implements org .codehaus .plexus .components .cipher .internal .Cipher {
45+ public static final String CIPHER_ALG = "AES/CBC/PKCS5Padding" ;
46+
47+ private static final int SPICE_SIZE = 16 ;
48+ private static final int SALT_SIZE = 8 ;
49+ private static final int CHUNK_SIZE = 16 ;
50+ private static final String KEY_ALG = "AES" ;
51+ private static final int PBE_ITERATIONS = 310000 ;
5452 private static final SecureRandom _secureRandom = new SecureRandom ();
5553
56- // ---------------------------------------------------------------
57- private byte [] getSalt (final int sz ) {
54+ private byte [] getSalt (int sz ) {
5855 byte [] res = new byte [sz ];
59-
6056 _secureRandom .nextBytes (res );
61-
6257 return res ;
6358 }
64- // -------------------------------------------------------------------------------
65- public String encrypt64 (final String clearText , final String password ) throws PlexusCipherException {
66- try {
67- byte [] clearBytes = clearText .getBytes (STRING_ENCODING );
6859
60+ @ Override
61+ public String encrypt (String clearText , String password ) throws PlexusCipherException {
62+ try {
63+ byte [] clearBytes = clearText .getBytes (StandardCharsets .UTF_8 );
6964 byte [] salt = getSalt (SALT_SIZE );
70-
7165 Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .ENCRYPT_MODE );
72-
7366 byte [] encryptedBytes = cipher .doFinal (clearBytes );
74-
7567 int len = encryptedBytes .length ;
76-
7768 byte padLen = (byte ) (CHUNK_SIZE - (SALT_SIZE + len + 1 ) % CHUNK_SIZE );
78-
7969 int totalLen = SALT_SIZE + len + padLen + 1 ;
80-
8170 byte [] allEncryptedBytes = getSalt (totalLen );
82-
8371 System .arraycopy (salt , 0 , allEncryptedBytes , 0 , SALT_SIZE );
84-
8572 allEncryptedBytes [SALT_SIZE ] = padLen ;
86-
8773 System .arraycopy (encryptedBytes , 0 , allEncryptedBytes , SALT_SIZE + 1 , len );
88-
8974 return Base64 .getEncoder ().encodeToString (allEncryptedBytes );
9075 } catch (Exception e ) {
9176 throw new PlexusCipherException (e .getMessage (), e );
9277 }
9378 }
9479
95- // -------------------------------------------------------------------------------
96- public String decrypt64 ( final String encryptedText , final String password ) throws PlexusCipherException {
80+ @ Override
81+ public String decrypt ( String encryptedText , String password ) throws PlexusCipherException {
9782 try {
9883 byte [] allEncryptedBytes = Base64 .getDecoder ().decode (encryptedText .getBytes ());
99-
10084 int totalLen = allEncryptedBytes .length ;
101-
10285 byte [] salt = new byte [SALT_SIZE ];
103-
10486 System .arraycopy (allEncryptedBytes , 0 , salt , 0 , SALT_SIZE );
105-
10687 byte padLen = allEncryptedBytes [SALT_SIZE ];
107-
10888 byte [] encryptedBytes = new byte [totalLen - SALT_SIZE - 1 - padLen ];
109-
11089 System .arraycopy (allEncryptedBytes , SALT_SIZE + 1 , encryptedBytes , 0 , encryptedBytes .length );
111-
11290 Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .DECRYPT_MODE );
113-
11491 byte [] clearBytes = cipher .doFinal (encryptedBytes );
115-
116- return new String (clearBytes , STRING_ENCODING );
92+ return new String (clearBytes , StandardCharsets .UTF_8 );
11793 } catch (Exception e ) {
11894 throw new PlexusCipherException (e .getMessage (), e );
11995 }
12096 }
121- // -------------------------------------------------------------------------------
122- private Cipher createCipher (final char [] pwd , byte [] salt , final int mode )
97+
98+ private Cipher createCipher (char [] pwd , byte [] salt , int mode )
12399 throws NoSuchAlgorithmException , NoSuchPaddingException , InvalidKeyException ,
124100 InvalidAlgorithmParameterException , InvalidKeySpecException {
125-
126101 KeySpec spec = new PBEKeySpec (pwd , salt , PBE_ITERATIONS , SPICE_SIZE * 16 );
127102 SecretKeyFactory factory = SecretKeyFactory .getInstance ("PBKDF2WithHmacSHA512" );
128103 byte [] keyAndIv = factory .generateSecret (spec ).getEncoded ();
129-
130104 byte [] key = new byte [SPICE_SIZE ];
131-
132105 byte [] iv = new byte [SPICE_SIZE ];
133-
134106 System .arraycopy (keyAndIv , 0 , key , 0 , key .length );
135-
136107 System .arraycopy (keyAndIv , key .length , iv , 0 , iv .length );
137-
138108 Cipher cipher = Cipher .getInstance (CIPHER_ALG );
139-
140109 cipher .init (mode , new SecretKeySpec (key , KEY_ALG ), new IvParameterSpec (iv ));
141-
142110 return cipher ;
143111 }
144112}
0 commit comments