@@ -25,8 +25,9 @@ Licensed to the Apache Software Foundation (ASF) under one
25
25
import javax .crypto .spec .IvParameterSpec ;
26
26
import javax .crypto .spec .PBEKeySpec ;
27
27
import javax .crypto .spec .SecretKeySpec ;
28
+ import javax .inject .Named ;
29
+ import javax .inject .Singleton ;
28
30
29
- import java .nio .charset .Charset ;
30
31
import java .nio .charset .StandardCharsets ;
31
32
import java .security .InvalidAlgorithmParameterException ;
32
33
import java .security .InvalidKeyException ;
@@ -38,107 +39,74 @@ Licensed to the Apache Software Foundation (ASF) under one
38
39
39
40
import org .codehaus .plexus .components .cipher .PlexusCipherException ;
40
41
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 ;
54
52
private static final SecureRandom _secureRandom = new SecureRandom ();
55
53
56
- // ---------------------------------------------------------------
57
- private byte [] getSalt (final int sz ) {
54
+ private byte [] getSalt (int sz ) {
58
55
byte [] res = new byte [sz ];
59
-
60
56
_secureRandom .nextBytes (res );
61
-
62
57
return res ;
63
58
}
64
- // -------------------------------------------------------------------------------
65
- public String encrypt64 (final String clearText , final String password ) throws PlexusCipherException {
66
- try {
67
- byte [] clearBytes = clearText .getBytes (STRING_ENCODING );
68
59
60
+ @ Override
61
+ public String encrypt (String clearText , String password ) throws PlexusCipherException {
62
+ try {
63
+ byte [] clearBytes = clearText .getBytes (StandardCharsets .UTF_8 );
69
64
byte [] salt = getSalt (SALT_SIZE );
70
-
71
65
Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .ENCRYPT_MODE );
72
-
73
66
byte [] encryptedBytes = cipher .doFinal (clearBytes );
74
-
75
67
int len = encryptedBytes .length ;
76
-
77
68
byte padLen = (byte ) (CHUNK_SIZE - (SALT_SIZE + len + 1 ) % CHUNK_SIZE );
78
-
79
69
int totalLen = SALT_SIZE + len + padLen + 1 ;
80
-
81
70
byte [] allEncryptedBytes = getSalt (totalLen );
82
-
83
71
System .arraycopy (salt , 0 , allEncryptedBytes , 0 , SALT_SIZE );
84
-
85
72
allEncryptedBytes [SALT_SIZE ] = padLen ;
86
-
87
73
System .arraycopy (encryptedBytes , 0 , allEncryptedBytes , SALT_SIZE + 1 , len );
88
-
89
74
return Base64 .getEncoder ().encodeToString (allEncryptedBytes );
90
75
} catch (Exception e ) {
91
76
throw new PlexusCipherException (e .getMessage (), e );
92
77
}
93
78
}
94
79
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 {
97
82
try {
98
83
byte [] allEncryptedBytes = Base64 .getDecoder ().decode (encryptedText .getBytes ());
99
-
100
84
int totalLen = allEncryptedBytes .length ;
101
-
102
85
byte [] salt = new byte [SALT_SIZE ];
103
-
104
86
System .arraycopy (allEncryptedBytes , 0 , salt , 0 , SALT_SIZE );
105
-
106
87
byte padLen = allEncryptedBytes [SALT_SIZE ];
107
-
108
88
byte [] encryptedBytes = new byte [totalLen - SALT_SIZE - 1 - padLen ];
109
-
110
89
System .arraycopy (allEncryptedBytes , SALT_SIZE + 1 , encryptedBytes , 0 , encryptedBytes .length );
111
-
112
90
Cipher cipher = createCipher (password .toCharArray (), salt , Cipher .DECRYPT_MODE );
113
-
114
91
byte [] clearBytes = cipher .doFinal (encryptedBytes );
115
-
116
- return new String (clearBytes , STRING_ENCODING );
92
+ return new String (clearBytes , StandardCharsets .UTF_8 );
117
93
} catch (Exception e ) {
118
94
throw new PlexusCipherException (e .getMessage (), e );
119
95
}
120
96
}
121
- // -------------------------------------------------------------------------------
122
- private Cipher createCipher (final char [] pwd , byte [] salt , final int mode )
97
+
98
+ private Cipher createCipher (char [] pwd , byte [] salt , int mode )
123
99
throws NoSuchAlgorithmException , NoSuchPaddingException , InvalidKeyException ,
124
100
InvalidAlgorithmParameterException , InvalidKeySpecException {
125
-
126
101
KeySpec spec = new PBEKeySpec (pwd , salt , PBE_ITERATIONS , SPICE_SIZE * 16 );
127
102
SecretKeyFactory factory = SecretKeyFactory .getInstance ("PBKDF2WithHmacSHA512" );
128
103
byte [] keyAndIv = factory .generateSecret (spec ).getEncoded ();
129
-
130
104
byte [] key = new byte [SPICE_SIZE ];
131
-
132
105
byte [] iv = new byte [SPICE_SIZE ];
133
-
134
106
System .arraycopy (keyAndIv , 0 , key , 0 , key .length );
135
-
136
107
System .arraycopy (keyAndIv , key .length , iv , 0 , iv .length );
137
-
138
108
Cipher cipher = Cipher .getInstance (CIPHER_ALG );
139
-
140
109
cipher .init (mode , new SecretKeySpec (key , KEY_ALG ), new IvParameterSpec (iv ));
141
-
142
110
return cipher ;
143
111
}
144
112
}
0 commit comments