Lines 37-47
Link Here
|
37 |
import java.security.PrivateKey; |
37 |
import java.security.PrivateKey; |
38 |
import java.security.PublicKey; |
38 |
import java.security.PublicKey; |
39 |
import java.security.spec.X509EncodedKeySpec; |
39 |
import java.security.spec.X509EncodedKeySpec; |
40 |
import java.util.Arrays; |
|
|
41 |
import javax.crypto.Cipher; |
40 |
import javax.crypto.Cipher; |
42 |
|
41 |
|
43 |
/** |
42 |
/** |
44 |
* |
43 |
* Password encryption/decryption utilities. |
|
|
44 |
* |
45 |
* @author Jindrich Sedek |
45 |
* @author Jindrich Sedek |
46 |
*/ |
46 |
*/ |
47 |
public class PasswdEncryption { |
47 |
public class PasswdEncryption { |
Lines 49-62
Link Here
|
49 |
private static final String delimiter = ":"; //NOI18N |
49 |
private static final String delimiter = ":"; //NOI18N |
50 |
public static final int MAX_ENCRYPTION_LENGHT = 100; |
50 |
public static final int MAX_ENCRYPTION_LENGHT = 100; |
51 |
|
51 |
|
|
|
52 |
/** |
53 |
* Encrypt a text with a default public key. |
54 |
* @param text text to encrypt |
55 |
* @return encrypted text |
56 |
* @throws IOException when I/O error occurs |
57 |
* @throws GeneralSecurityException when transformation error occurs |
58 |
*/ |
52 |
public static String encrypt(String text) throws IOException, GeneralSecurityException { |
59 |
public static String encrypt(String text) throws IOException, GeneralSecurityException { |
53 |
return encrypt(text, getPublicKey()); |
60 |
return encrypt(text, getPublicKey()); |
54 |
} |
61 |
} |
55 |
|
62 |
|
|
|
63 |
/** |
64 |
* Encrypt a sequence of bytes a default public key. |
65 |
* @param text byte array to encrypt |
66 |
* @return encrypted bytes |
67 |
* @throws IOException when I/O error occurs |
68 |
* @throws GeneralSecurityException when transformation error occurs |
69 |
*/ |
56 |
public static byte[] encrypt(byte[] text) throws IOException, GeneralSecurityException { |
70 |
public static byte[] encrypt(byte[] text) throws IOException, GeneralSecurityException { |
57 |
return encrypt(text, getPublicKey()); |
71 |
return encrypt(text, getPublicKey()); |
58 |
} |
72 |
} |
59 |
|
73 |
|
|
|
74 |
/** |
75 |
* Encrypt a sequence of bytes. |
76 |
* @param text byte array to encrypt |
77 |
* @param key the public key used for the encryption |
78 |
* @return encrypted bytes |
79 |
* @throws IOException when I/O error occurs |
80 |
* @throws GeneralSecurityException when transformation error occurs |
81 |
*/ |
60 |
public static byte[] encrypt(byte[] text, PublicKey key) throws IOException, GeneralSecurityException { |
82 |
public static byte[] encrypt(byte[] text, PublicKey key) throws IOException, GeneralSecurityException { |
61 |
assert (text.length <= MAX_ENCRYPTION_LENGHT); |
83 |
assert (text.length <= MAX_ENCRYPTION_LENGHT); |
62 |
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N |
84 |
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N |
Lines 66-71
Link Here
|
66 |
return encoded; |
88 |
return encoded; |
67 |
} |
89 |
} |
68 |
|
90 |
|
|
|
91 |
/** |
92 |
* Decrypt a sequence of bytes. |
93 |
* @param text byte array to decrypt |
94 |
* @param key the private key used for the decryption |
95 |
* @return decrypted bytes |
96 |
* @throws IOException when I/O error occurs |
97 |
* @throws GeneralSecurityException when transformation error occurs |
98 |
*/ |
69 |
public static byte[] decrypt(byte[] text, PrivateKey key) throws IOException, GeneralSecurityException { |
99 |
public static byte[] decrypt(byte[] text, PrivateKey key) throws IOException, GeneralSecurityException { |
70 |
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N |
100 |
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N |
71 |
rsaCipher.init(Cipher.DECRYPT_MODE, key); |
101 |
rsaCipher.init(Cipher.DECRYPT_MODE, key); |
Lines 74-84
Link Here
|
74 |
return decoded; |
104 |
return decoded; |
75 |
} |
105 |
} |
76 |
|
106 |
|
|
|
107 |
/** |
108 |
* Encrypt a text. |
109 |
* @param text text to encrypt |
110 |
* @param key the public key used for the encryption |
111 |
* @return encrypted text |
112 |
* @throws IOException when I/O error occurs |
113 |
* @throws GeneralSecurityException when transformation error occurs |
114 |
*/ |
77 |
public static String encrypt(String text, PublicKey key) throws IOException, GeneralSecurityException { |
115 |
public static String encrypt(String text, PublicKey key) throws IOException, GeneralSecurityException { |
78 |
byte[] encrypted = encrypt(text.getBytes(), key); |
116 |
byte[] encrypted = encrypt(text.getBytes(), key); |
79 |
return arrayToString(encrypted); |
117 |
return arrayToString(encrypted); |
80 |
} |
118 |
} |
81 |
|
119 |
|
|
|
120 |
/** |
121 |
* Decrypt a text. |
122 |
* @param text text to decrypt |
123 |
* @param key the private key used for the decryption |
124 |
* @return decrypted text |
125 |
* @throws IOException when I/O error occurs |
126 |
* @throws GeneralSecurityException when transformation error occurs |
127 |
*/ |
82 |
public static String decrypt(String text, PrivateKey key) throws IOException, GeneralSecurityException { |
128 |
public static String decrypt(String text, PrivateKey key) throws IOException, GeneralSecurityException { |
83 |
byte[] decrypted = decrypt(stringToArray(text), key); |
129 |
byte[] decrypted = decrypt(stringToArray(text), key); |
84 |
return new String(decrypted); |
130 |
return new String(decrypted); |