11package javasabr .rlib .common .util .crypt ;
22
3+ import java .io .Serial ;
34import java .io .UnsupportedEncodingException ;
5+ import java .nio .charset .StandardCharsets ;
46import java .security .InvalidKeyException ;
57import java .security .NoSuchAlgorithmException ;
68import javax .crypto .BadPaddingException ;
911import javax .crypto .NoSuchPaddingException ;
1012import javax .crypto .SecretKey ;
1113import javax .crypto .ShortBufferException ;
12- import org .jspecify .annotations .NullMarked ;
14+ import lombok .AccessLevel ;
15+ import lombok .experimental .FieldDefaults ;
1316
1417/**
15- * The symmetry crypt based on RC4.
16- *
1718 * @author JavaSaBr
1819 */
19- @ NullMarked
20+ @ FieldDefaults ( level = AccessLevel . PRIVATE , makeFinal = true )
2021public class SymmetryCrypt {
2122
22- /**
23- * The crypter.
24- */
25- private final Cipher ecipher ;
26-
27- /**
28- * The encrypter.
29- */
30- private final Cipher dcipher ;
31-
32- /**
33- * THe secret key.
34- */
35- private final SecretKey secretKey ;
36-
37- /**
38- * Instantiates a new Symmetry crypt.
39- *
40- * @param key the key.
41- * @throws NoSuchAlgorithmException the no such algorithm exception
42- * @throws NoSuchPaddingException the no such padding exception
43- * @throws UnsupportedEncodingException the unsupported encoding exception
44- * @throws InvalidKeyException the invalid key exception
45- */
46- public SymmetryCrypt (final String key )
23+ public static final String ALG_RC_4 = "RC4" ;
24+
25+ Cipher ecipher ;
26+ Cipher dcipher ;
27+
28+ public SymmetryCrypt (String key )
4729 throws NoSuchAlgorithmException , NoSuchPaddingException , UnsupportedEncodingException , InvalidKeyException {
30+ this (key , ALG_RC_4 );
31+ }
4832
49- final Cipher ecipher = Cipher . getInstance ( "RC4" );
50- final Cipher dcipher = Cipher . getInstance ( "RC4" );
33+ public SymmetryCrypt ( String key , String algorithm )
34+ throws NoSuchAlgorithmException , NoSuchPaddingException , UnsupportedEncodingException , InvalidKeyException {
5135
52- final byte [] bytes = key .getBytes ("UTF-8" );
36+ Cipher ecipher = Cipher .getInstance (algorithm );
37+ Cipher dcipher = Cipher .getInstance (algorithm );
5338
54- secretKey = new SecretKey () {
39+ byte [] bytes = key .getBytes (StandardCharsets .UTF_8 );
40+ var secretKey = new SecretKey () {
5541
42+ @ Serial
5643 private static final long serialVersionUID = -8907627571317506056L ;
5744
5845 @ Override
5946 public String getAlgorithm () {
60- return "RC4" ;
47+ return algorithm ;
6148 }
6249
6350 @ Override
@@ -78,34 +65,12 @@ public String getFormat() {
7865 this .dcipher = dcipher ;
7966 }
8067
81- /**
82- * Decrypt data.
83- *
84- * @param in the encrypted data.
85- * @param offset the offset.
86- * @param length the length.
87- * @param out the buffer to store decrypted data.
88- * @throws ShortBufferException the short buffer exception
89- * @throws IllegalBlockSizeException the illegal block size exception
90- * @throws BadPaddingException the bad padding exception
91- */
92- public void decrypt (final byte [] in , final int offset , final int length , final byte [] out )
68+ public void decrypt (byte [] in , int offset , int length , byte [] out )
9369 throws ShortBufferException , IllegalBlockSizeException , BadPaddingException {
9470 dcipher .doFinal (in , offset , length , out , offset );
9571 }
9672
97- /**
98- * Encrypt data.
99- *
100- * @param in the decrypted data.
101- * @param offset the offset.
102- * @param length the length.
103- * @param out the buffer to store encrypted data.
104- * @throws ShortBufferException the short buffer exception
105- * @throws IllegalBlockSizeException the illegal block size exception
106- * @throws BadPaddingException the bad padding exception
107- */
108- public void encrypt (final byte [] in , final int offset , final int length , final byte [] out )
73+ public void encrypt (byte [] in , int offset , int length , byte [] out )
10974 throws ShortBufferException , IllegalBlockSizeException , BadPaddingException {
11075 ecipher .doFinal (in , offset , length , out , offset );
11176 }
0 commit comments