Created
March 1, 2016 14:53
-
-
Save ibrahimlawal/06e10e1cc3f5b4b1600b to your computer and use it in GitHub Desktop.
A java 1.6 ccompatible class that provides RSA encryption using a base64 encoded public key string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A java 1.6 ccompatible class that provides RSA encryption using a | |
* base64 encoded public key string. | |
* | |
* To encrypt, do | |
* Crypto.encrypt("Text to encrypt"); // returns base64 encoded string for encrypted data | |
* | |
* @author Ibrahim Lawal ([email protected]) | |
* @version 2016/02/29 | |
*/ | |
import javax.xml.bind.DatatypeConverter; | |
import java.security.KeyFactory; | |
import java.security.NoSuchAlgorithmException; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.spec.InvalidKeySpecException; | |
import java.security.spec.X509EncodedKeySpec; | |
import javax.crypto.Cipher; | |
// No inheriting this class - hence the final | |
public final class Crypto { | |
// This class should not be instantiated. Only valid action is Crypto.encrypt(String) | |
private Crypto() { | |
throw new RuntimeException("This class should not be instantiated. Only valid action is Crypto.encrypt(String)"); | |
} | |
private static String ALGORITHM = "RSA"; | |
private static String CIPHER = "RSA/ECB/PKCS1Padding"; | |
private static String PUBLIC_KEY = "ENTER//THE//BASE//64//ENCODED//KEY//String" + | |
"HERE///////DO//NOT//INCLude//boundaries=="; | |
private static byte[] encrypt(String text, PublicKey key) { | |
byte[] cipherText = null; | |
try { | |
// get an RSA cipher object | |
final Cipher cipher = Cipher.getInstance(CIPHER); | |
//init cipher and encrypt the plain text using the public key | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
cipherText = cipher.doFinal(text.getBytes()); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return cipherText; | |
} | |
public static String encrypt(String text) throws Exception { | |
return new String(DatatypeConverter.printBase64Binary(encrypt(text, getPublicKeyFromString()))); | |
} | |
private static PublicKey getPublicKeyFromString() throws Exception { | |
PublicKey key; | |
try { | |
//init keyfactory | |
KeyFactory kf = KeyFactory.getInstance(ALGORITHM); | |
//decode the key into a byte array | |
byte[] keyBytes = DatatypeConverter.parseBase64Binary(PUBLIC_KEY); | |
//create spec | |
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); | |
//generate public key | |
key = kf.generatePublic(spec); | |
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) { | |
throw new Exception("Invalid publishable key: " + e.getMessage()); | |
} | |
return key; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment