Skip to content

Commit dc487ea

Browse files
committed
Added class for generating a secret key.
1 parent 9382e08 commit dc487ea

3 files changed

Lines changed: 77 additions & 35 deletions

File tree

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

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ public static String encryptString(String data, String base64PublicKey, String t
5555
throws CryptoException, IOException {
5656
String provider = "SUN";
5757

58-
try (InputStream bais = new ByteArrayInputStream(data.getBytes(UTF_8))) {
59-
return encrypt(bais, base64PublicKey, transformationName, provider);
60-
}
58+
return encryptString(data, base64PublicKey, transformationName, provider);
6159
}
6260

6361
public static String encryptString(String data, String base64PublicKey, String transformationName, String provider)
6462
throws CryptoException, IOException {
65-
InputStream dataIs = new ByteArrayInputStream(data.getBytes(UTF_8));
63+
byte[] dataBytes = data.getBytes(UTF_8);
6664
String algorithm = transformationName.split("/")[0];
6765
byte[] resultBytes = null;
6866

@@ -71,13 +69,7 @@ public static String encryptString(String data, String base64PublicKey, String t
7169
cipher = Cipher.getInstance(transformationName);
7270
PublicKey publicKey = loadPublicKey(base64PublicKey, algorithm, provider);
7371
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
74-
75-
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
76-
int read = -1;
77-
while ((read = dataIs.read(buf)) > -1) {
78-
cipher.update(buf, 0, read);
79-
}
80-
resultBytes = cipher.doFinal();
72+
resultBytes = cipher.doFinal(dataBytes);
8173
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
8274
| InvalidKeyException e) {
8375
throw new CryptoException(e);
@@ -88,8 +80,36 @@ public static String encryptString(String data, String base64PublicKey, String t
8880
return Base64.getEncoder().encodeToString(resultBytes);
8981
}
9082

91-
public static String encrypt(InputStream data, String base64PublicKey, String transformationName, String provider)
83+
public static String decryptString(String encryptedData, String base64PrivateKey, String transformationName)
9284
throws CryptoException, IOException {
85+
String provider = "SUN";
86+
87+
return decryptString(encryptedData, base64PrivateKey, transformationName, provider);
88+
}
89+
90+
public static String decryptString(String encryptedData, String base64PrivateKey, String transformationName,
91+
String provider) throws CryptoException, IOException {
92+
byte[] dataBytes = Base64.getDecoder().decode(encryptedData);
93+
String algorithm = transformationName.split("/")[0];
94+
byte[] resultBytes = null;
95+
96+
Cipher cipher;
97+
try {
98+
cipher = Cipher.getInstance(transformationName);
99+
cipher.init(Cipher.DECRYPT_MODE, loadPrivateKey(base64PrivateKey, algorithm, provider));
100+
resultBytes = cipher.doFinal(dataBytes);
101+
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException
102+
| InvalidKeyException e) {
103+
throw new CryptoException(e);
104+
} catch (Exception e) {
105+
e.printStackTrace();
106+
}
107+
108+
return new String(resultBytes, UTF_8);
109+
}
110+
111+
public static String encryptBinary(InputStream data, String base64PublicKey, String transformationName,
112+
String provider) throws CryptoException, IOException {
93113
String algorithm = transformationName.split("/")[0];
94114
byte[] resultBytes = null;
95115

@@ -115,32 +135,16 @@ public static String encrypt(InputStream data, String base64PublicKey, String tr
115135
return getString(resultBytes);
116136
}
117137

118-
public static String decryptString(String encryptedInput, String base64PrivateKey, String transformationName)
119-
throws CryptoException, IOException {
120-
String provider = "SUN";
121-
122-
try (InputStream bais = new ByteArrayInputStream(getBytes(encryptedInput))) {
123-
return decrypt(bais, base64PrivateKey, transformationName, provider);
124-
}
125-
}
126-
127-
public static String decryptString(String data, String base64PrivateKey, String transformationName, String provider)
128-
throws CryptoException, IOException {
129-
try (InputStream bais = new ByteArrayInputStream(getBytes(data))) {
130-
return decrypt(bais, base64PrivateKey, transformationName, provider);
131-
}
132-
}
133-
134-
public static String decrypt(InputStream data, String base64PrivateKey, String transformationName, String provider)
135-
throws CryptoException, IOException {
138+
public static String decryptBinary(InputStream data, String base64PrivateKey, String transformationName,
139+
String provider) throws CryptoException, IOException {
136140
String algorithm = transformationName.split("/")[0];
137141
byte[] resultBytes = null;
138142

139143
Cipher cipher;
140144
try {
141145
cipher = Cipher.getInstance(transformationName);
142-
PrivateKey publicKey = loadPrivateKey(base64PrivateKey, algorithm, provider);
143-
cipher.init(Cipher.DECRYPT_MODE, publicKey);
146+
PrivateKey privateKey = loadPrivateKey(base64PrivateKey, algorithm, provider);
147+
cipher.init(Cipher.DECRYPT_MODE, privateKey);
144148

145149
byte[] buf = new byte[Buffer.TRANSFER_SIZE];
146150
int read = -1;
@@ -199,7 +203,8 @@ private static PrivateKey loadPrivateKey(String base64PrivateKey, String algorit
199203
// !str.isEmpty()).orElse("SunRsaSign");
200204
provider = "SunRsaSign";
201205

202-
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(base64PrivateKey.getBytes(UTF_8)));
206+
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(
207+
Base64.getDecoder().decode(base64PrivateKey.getBytes(UTF_8)));
203208

204209
KeyFactory kf = KeyFactory.getInstance(algorithm, provider);
205210

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ro.kuberam.libs.java.crypto.keyManagement;
2+
3+
import java.security.NoSuchAlgorithmException;
4+
import java.util.Base64;
5+
6+
import javax.crypto.KeyGenerator;
7+
import javax.crypto.SecretKey;
8+
9+
public class GenerateSecretKey {
10+
11+
public static SecretKey run(String algorithm) {
12+
KeyGenerator keyGen = null;
13+
try {
14+
/*
15+
* Get KeyGenerator object that generates secret keys for the specified
16+
* algorithm.
17+
*/
18+
keyGen = KeyGenerator.getInstance(algorithm);
19+
} catch (NoSuchAlgorithmException e) {
20+
e.printStackTrace();
21+
}
22+
23+
/* Initializes this key generator for key size to 256. */
24+
keyGen.init(256);
25+
26+
/* Generates a secret key */
27+
SecretKey secretKey = keyGen.generateKey();
28+
29+
return secretKey;
30+
}
31+
32+
public static void main(String args[]) {
33+
SecretKey secretKey = run("AES");
34+
String encodedKey = Base64.getEncoder().encodeToString(secretKey.getEncoded());
35+
System.out.println(encodedKey);
36+
}
37+
}

src/test/java/ro/kuberam/libs/java/crypto/encrypt/RSAUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ public static String decrypt(byte[] data, PrivateKey privateKey) throws NoSuchPa
6262
}
6363

6464
public static String decrypt(String data, String base64PrivateKey) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
65-
return decrypt(Base64.getDecoder().decode(data.getBytes()), getPrivateKey(base64PrivateKey));
65+
return decrypt(Base64.getDecoder().decode(data), getPrivateKey(base64PrivateKey));
6666
}
6767

6868
public static void main(String[] args) throws IllegalBlockSizeException, InvalidKeyException, NoSuchPaddingException, BadPaddingException {
6969
try {
70-
String encryptedString = Base64.getEncoder().encodeToString(encrypt(veryLongString, publicKey));
70+
String encryptedString = Base64.getEncoder().encodeToString(encrypt(longString, publicKey));
7171
System.out.println(encryptedString);
7272
String decryptedString = RSAUtil.decrypt(encryptedString, privateKey);
7373
System.out.println(decryptedString);

0 commit comments

Comments
 (0)