forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyStoreEncryptor.java
More file actions
42 lines (37 loc) · 1.47 KB
/
KeyStoreEncryptor.java
File metadata and controls
42 lines (37 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import org.jpos.iso.ISOUtil;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.interfaces.RSAKey;
import java.security.spec.InvalidKeySpecException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
public class KeyStoreEncryptor {
public String rsaEncrypt(String keybyte, PublicKey publicK) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
try {
String encryptedvalue;
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.ENCRYPT_MODE, publicK);
byte[] encrypted;
encrypted = c.doFinal(keybyte.getBytes());
encryptedvalue = ISOUtil.hexString(encrypted);
return encryptedvalue;
} catch (Exception ps) {
throw ps;
}
}
public byte[] rsaDecrypt(byte[] keybyte, PrivateKey privateKey) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException {
try {
byte[] decryptedbyte;
Cipher c = Cipher.getInstance("RSA");
c.init(Cipher.DECRYPT_MODE, privateKey);
decryptedbyte = c.doFinal(keybyte);
return decryptedbyte;
} catch (Exception ps) {
throw ps;
}
}
}