Skip to content

Commit 7ad0e99

Browse files
committed
1 parent 6b92265 commit 7ad0e99

File tree

2 files changed

+110
-145
lines changed

2 files changed

+110
-145
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>ro.kuberam.libs.java</groupId>
88
<artifactId>crypto</artifactId>
9-
<version>1.5</version>
9+
<version>1.6</version>
1010
<name>EXPath Cryptographic Module</name>
1111
<description>Java Library providing an EXPath Cryptographic Module</description>
1212
<url>http://kuberam.ro/specs/expath/crypto/crypto.html</url>

src/main/java/ro/kuberam/libs/java/crypto/encrypt/SymmetricEncryption.java

Lines changed: 109 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
1919
*/
2020
package ro.kuberam.libs.java.crypto.encrypt;
2121

22+
import static java.nio.charset.StandardCharsets.UTF_8;
23+
2224
import java.io.ByteArrayInputStream;
2325
import java.io.ByteArrayOutputStream;
2426
import java.io.IOException;
2527
import java.io.InputStream;
26-
import java.nio.charset.StandardCharsets;
2728
import java.security.InvalidAlgorithmParameterException;
2829
import java.security.InvalidKeyException;
2930
import java.security.NoSuchAlgorithmException;
@@ -42,153 +43,117 @@
4243
import ro.kuberam.libs.java.crypto.CryptoException;
4344
import ro.kuberam.libs.java.crypto.utils.Buffer;
4445

45-
import static java.nio.charset.StandardCharsets.UTF_8;
46-
4746
/**
48-
* @author <a href="mailto:[email protected]">Claudius Teodorescu</a>
47+
* @author <a href="mailto:[email protected]">Claudius
48+
* Teodorescu</a>
4949
*/
5050
public class SymmetricEncryption {
5151

52-
public static String encryptString(final String input, final String plainKey, final String transformationName,
53-
final String iv, final String provider) throws CryptoException, IOException {
54-
try (final InputStream bais = new ByteArrayInputStream(input.getBytes(UTF_8))) {
55-
return encrypt(bais, plainKey, transformationName, iv, provider);
56-
}
57-
}
58-
59-
public static String encrypt(final InputStream input, final String plainKey, final String transformationName,
60-
final String iv, final String provider) throws CryptoException, IOException {
61-
final String algorithm = (transformationName.contains("/")) ? transformationName.substring(0, transformationName.indexOf("/")) : transformationName;
62-
final String actualProvider = Optional.ofNullable(provider)
63-
.filter(str -> !str.isEmpty())
64-
.orElse("SunJCE"); // default to SunJCE
65-
66-
final Cipher cipher;
67-
try {
68-
cipher = Cipher.getInstance(transformationName, actualProvider);
69-
} catch (final NoSuchProviderException e) {
70-
throw new CryptoException(CryptoError.NO_PROVIDER, e);
71-
} catch (final NoSuchAlgorithmException e) {
72-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
73-
} catch (final NoSuchPaddingException e) {
74-
throw new CryptoException(CryptoError.INEXISTENT_PADDING, e);
75-
}
76-
77-
final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(UTF_8), algorithm);
78-
if (transformationName.contains("/")) {
79-
final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(UTF_8), 0, 16);
80-
try {
81-
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec);
82-
} catch (final InvalidAlgorithmParameterException e) {
83-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
84-
} catch (final InvalidKeyException e) {
85-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
86-
}
87-
} else {
88-
try {
89-
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
90-
} catch (final InvalidKeyException e) {
91-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
92-
}
93-
}
94-
95-
try {
96-
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
97-
int read = -1;
98-
while((read = input.read(buf)) > -1) {
99-
cipher.update(buf, 0, read);
100-
}
101-
102-
final byte[] resultBytes = cipher.doFinal();
103-
return getString(resultBytes);
104-
} catch (final IllegalBlockSizeException e) {
105-
throw new CryptoException(CryptoError.BLOCK_SIZE, e);
106-
} catch (final BadPaddingException e) {
107-
throw new CryptoException(CryptoError.INCORRECT_PADDING, e);
108-
}
109-
}
110-
111-
public static String decryptString(final String encryptedInput, final String plainKey,
112-
final String transformationName, final String iv, final String provider) throws CryptoException, IOException {
113-
try (final InputStream bais = new ByteArrayInputStream(getBytes(encryptedInput))) {
114-
return decrypt(bais, plainKey, transformationName, iv, provider);
115-
}
116-
}
117-
118-
public static String decrypt(final InputStream encryptedInput, final String plainKey,
119-
final String transformationName, final String iv, final String provider) throws CryptoException, IOException {
120-
final String algorithm = (transformationName.contains("/")) ? transformationName.substring(0, transformationName.indexOf("/")) : transformationName;
121-
final String actualProvider = Optional.ofNullable(provider)
122-
.filter(str -> !str.isEmpty())
123-
.orElse("SunJCE"); // default to SunJCE
124-
125-
final Cipher cipher;
126-
try {
127-
cipher = Cipher.getInstance(transformationName, actualProvider);
128-
} catch (final NoSuchProviderException e) {
129-
throw new CryptoException(CryptoError.NO_PROVIDER, e);
130-
} catch (final NoSuchAlgorithmException e) {
131-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
132-
} catch (final NoSuchPaddingException e) {
133-
throw new CryptoException(CryptoError.INEXISTENT_PADDING, e);
134-
}
135-
136-
final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(StandardCharsets.UTF_8), algorithm);
137-
if (transformationName.contains("/")) {
138-
final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8), 0, 16);
139-
try {
140-
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec);
141-
} catch (final InvalidAlgorithmParameterException e) {
142-
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
143-
} catch (final InvalidKeyException e) {
144-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
145-
}
146-
} else {
147-
try {
148-
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
149-
} catch (final InvalidKeyException e) {
150-
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
151-
}
152-
}
153-
try {
154-
155-
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
156-
int read = -1;
157-
while((read = encryptedInput.read(buf)) > -1) {
158-
cipher.update(buf, 0, read);
159-
}
160-
161-
final byte[] resultBytes = cipher.doFinal();
162-
return new String(resultBytes, UTF_8);
163-
} catch (final IllegalBlockSizeException e) {
164-
throw new CryptoException(CryptoError.BLOCK_SIZE, e);
165-
} catch (final BadPaddingException e) {
166-
throw new CryptoException(CryptoError.INCORRECT_PADDING, e);
167-
}
168-
}
169-
170-
public static String getString(final byte[] bytes) {
171-
final StringBuilder sb = new StringBuilder();
172-
for (int i = 0; i < bytes.length; i++) {
173-
final byte b = bytes[i];
174-
sb.append((int) (0x00FF & b));
175-
if (i + 1 < bytes.length) {
176-
sb.append("-");
177-
}
178-
}
179-
return sb.toString();
180-
}
181-
182-
public static byte[] getBytes(final String str) throws IOException {
183-
final StringTokenizer st = new StringTokenizer(str, "-", false);
184-
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
185-
while (st.hasMoreTokens()) {
186-
final int i = Integer.parseInt(st.nextToken());
187-
bos.write((byte) i);
188-
}
189-
return bos.toByteArray();
190-
}
191-
}
52+
public static String encryptString(final String input, final String plainKey, final String transformationName,
53+
final String iv, final String provider) throws CryptoException, IOException {
54+
try (final InputStream bais = new ByteArrayInputStream(input.getBytes(UTF_8))) {
55+
return operation(Cipher.ENCRYPT_MODE, bais, plainKey, transformationName, iv, provider);
56+
}
57+
}
58+
59+
public static String encryptBinary(final InputStream input, final String plainKey, final String transformationName,
60+
final String iv, final String provider) throws CryptoException, IOException {
61+
return operation(Cipher.ENCRYPT_MODE, input, plainKey, transformationName, iv, provider);
62+
}
63+
64+
public static String decryptString(final String encryptedInput, final String plainKey,
65+
final String transformationName, final String iv, final String provider)
66+
throws CryptoException, IOException {
67+
try (final InputStream bais = new ByteArrayInputStream(getBytes(encryptedInput))) {
68+
return operation(Cipher.DECRYPT_MODE, bais, plainKey, transformationName, iv, provider);
69+
}
70+
}
71+
72+
public static String decryptBinary(final InputStream encryptedInput, final String plainKey,
73+
final String transformationName, final String iv, final String provider)
74+
throws CryptoException, IOException {
75+
return operation(Cipher.DECRYPT_MODE, encryptedInput, plainKey, transformationName, iv, provider);
76+
}
77+
78+
public static String operation(final int operationType, final InputStream input, final String plainKey,
79+
final String transformationName, final String iv, final String provider)
80+
throws CryptoException, IOException {
81+
final String algorithm = (transformationName.contains("/"))
82+
? transformationName.substring(0, transformationName.indexOf("/"))
83+
: transformationName;
84+
final String actualProvider = Optional.ofNullable(provider).filter(str -> !str.isEmpty()).orElse("SunJCE");
85+
final Cipher cipher;
86+
ByteArrayOutputStream resultBaos = new ByteArrayOutputStream();
87+
88+
try {
89+
cipher = Cipher.getInstance(transformationName, actualProvider);
90+
} catch (NoSuchProviderException e) {
91+
throw new CryptoException(CryptoError.NO_PROVIDER, e);
92+
} catch (NoSuchAlgorithmException e) {
93+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
94+
} catch (NoSuchPaddingException e) {
95+
throw new CryptoException(CryptoError.INEXISTENT_PADDING, e);
96+
}
97+
98+
final SecretKeySpec skeySpec = new SecretKeySpec(plainKey.getBytes(UTF_8), algorithm);
99+
if (transformationName.contains("/")) {
100+
final IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(UTF_8), 0, 16);
101+
try {
102+
cipher.init(operationType, skeySpec, ivSpec);
103+
} catch (InvalidAlgorithmParameterException e) {
104+
throw new CryptoException(CryptoError.UNKNOWN_ALGORITH, e);
105+
} catch (InvalidKeyException e) {
106+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
107+
}
108+
} else {
109+
try {
110+
cipher.init(operationType, skeySpec);
111+
} catch (InvalidKeyException e) {
112+
throw new CryptoException(CryptoError.INVALID_CRYPTO_KEY, e);
113+
}
114+
}
115+
116+
try {
117+
final byte[] buf = new byte[Buffer.TRANSFER_SIZE];
118+
int read = -1;
119+
while ((read = input.read(buf)) > -1) {
120+
byte[] tmpBuffer = cipher.update(buf, 0, read);
121+
resultBaos.write(tmpBuffer);
122+
}
123+
124+
byte[] finalBuffer = cipher.doFinal();
125+
resultBaos.write(finalBuffer);
126+
127+
return getString(resultBaos.toByteArray());
128+
} catch (IllegalBlockSizeException e) {
129+
throw new CryptoException(CryptoError.BLOCK_SIZE, e);
130+
} catch (BadPaddingException e) {
131+
throw new CryptoException(CryptoError.INCORRECT_PADDING, e);
132+
}
133+
}
134+
135+
public static String getString(final byte[] bytes) {
136+
final StringBuilder sb = new StringBuilder();
137+
for (int i = 0; i < bytes.length; i++) {
138+
final byte b = bytes[i];
139+
sb.append((int) (0x00FF & b));
140+
if (i + 1 < bytes.length) {
141+
sb.append("-");
142+
}
143+
}
144+
return sb.toString();
145+
}
146+
147+
public static byte[] getBytes(final String str) throws IOException {
148+
final StringTokenizer st = new StringTokenizer(str, "-", false);
149+
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
150+
while (st.hasMoreTokens()) {
151+
final int i = Integer.parseInt(st.nextToken());
152+
bos.write((byte) i);
153+
}
154+
return bos.toByteArray();
155+
}
156+
}
192157

193158
}
194159

0 commit comments

Comments
 (0)