Last active
May 12, 2021 11:52
-
-
Save iambenkay/ba781e2c6d8daaaa7a49b6fc0ba9e314 to your computer and use it in GitHub Desktop.
Perform RSA encryption and decryption in Java
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
import java.security.KeyFactory | |
import java.security.spec.PKCS8EncodedKeySpec | |
import java.security.spec.X509EncodedKeySpec | |
import java.util.* | |
import javax.crypto.Cipher | |
object RSA { | |
private val encoder = Base64.getEncoder() | |
private val decoder = Base64.getDecoder() | |
fun encrypt(data: String, publicKey: String): String { | |
val strippedPublicKey = stripKey(publicKey) | |
val publicKeySpec = X509EncodedKeySpec(decoder.decode(strippedPublicKey)) | |
val kf = KeyFactory.getInstance("RSA") | |
val key = kf.generatePublic(publicKeySpec) | |
val c = Cipher.getInstance("RSA") | |
c.init(Cipher.ENCRYPT_MODE, key) | |
val encrypted = c.doFinal(data.toByteArray()) | |
return encoder.encodeToString(encrypted) | |
} | |
fun decrypt(data: String, privateKey: String): String { | |
val strippedPrivateKey = stripKey(privateKey) | |
val privateKeySpec = PKCS8EncodedKeySpec(decoder.decode(strippedPrivateKey)) | |
val kf = KeyFactory.getInstance("RSA") | |
val key = kf.generatePrivate(privateKeySpec) | |
val c = Cipher.getInstance("RSA") | |
c.init(Cipher.DECRYPT_MODE, key) | |
return String(c.doFinal(decoder.decode(data))) | |
} | |
private fun stripKey(key: String): String { | |
return key | |
.replace("\n", "") | |
.replace(Regex("-----BEGIN ([A-Z]*\\s)?PRIVATE KEY-----"), "") | |
.replace(Regex("-----END ([A-Z]*\\s)?PRIVATE KEY-----"), "") | |
.replace(Regex("-----BEGIN ([A-Z]*\\s)?PUBLIC KEY-----"), "") | |
.replace(Regex("-----END ([A-Z]*\\s)?PUBLIC KEY-----"), "") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment