Hi all. I'm creating a class to encrypt and decryp...
# getting-started
r
Hi all. I'm creating a class to encrypt and decrypt strings using bouncy castle. everything works perfect that way:
Copy code
private fun keysCert() = runCatching {
  Security.addProvider(org.bouncycastle.jce.provider.BouncyCastleProvider())
  val certificate: X509Certificate = CertificateFactory
    .getInstance("X.509", "BC")
    .generateCertificate(FileInputStream("/home/rodrigo/Projects/aries/commons/backend/commons_security/src/main/resources/Some.cer")) as X509Certificate

  val keyStore = KeyStore.getInstance("PKCS12")
  keyStore.load(
    FileInputStream("/home/rodrigo/Projects/aries/commons/backend/commons_security/src/main/resources/Some.p12"),
    "password".toCharArray()
  )

  val privateKey = keyStore.getKey("some", "password".toCharArray()) as PrivateKey
  Pair(certificate, privateKey)
}.onFailure { it.message }

fun String.encypt(): Result<ByteArray> = runCatching {
  val cmsEnvelopedDataGenerator = CMSEnvelopedDataGenerator()
  val jceKey = JceKeyTransRecipientInfoGenerator(keysCert().getOrThrow().first)
  cmsEnvelopedDataGenerator.addRecipientInfoGenerator(jceKey)
  val message = CMSProcessableByteArray(this.toByteArray())
  val encryptor = JceCMSContentEncryptorBuilder(CMSAlgorithm.AES256_CBC).setProvider("BC").build()
  cmsEnvelopedDataGenerator.generate(message, encryptor).encoded
}.onFailure { it.message }

fun ByteArray.decrypt(): Result<String> = runCatching {
  val envelopeData = CMSEnvelopedData(this)
  val recip = envelopeData.recipientInfos.getRecipients()
  val recipientInfo: KeyTransRecipientInformation = recip.iterator().next() as KeyTransRecipientInformation
  String(recipientInfo.getContent(JceKeyTransEnvelopedRecipient(keysCert().getOrThrow().second)))
}.onFailure { it.message }
But I would like encrypt() to return a string and decrypt() to be from a string. I tried this way:
fun String.encypt(): Result<String> = runCatching {
// equals
String(cmsEnvelopedDataGenerator.generate(message, encryptor).encoded)
}.onFailure { it.message }
and on decrypt
fun String.decrypt(): Result<String> = runCatching {
val envelopeData = CMSEnvelopedData(this.toByteArray()) -- getting exception here
... What am I doing wrong?
k
If you want encrypt to return a string the natural thing would be to encode it, no?
fun String.encypt(): Result<String> = runCatching {
// equals
Base64.getEncoder().encodeToString(cmsEnvelopedDataGenerator.generate(message, encryptor).encoded)
}.onFailure { it.message }
Copy code
fun String.decrypt(): Result<String> = runCatching {
    val decoded = Base64.getDecoder().decode(this)
    val envelopeData = CMSEnvelopedData(decoded)
    val recip = envelopeData.recipientInfos.getRecipients()
    val recipientInfo: KeyTransRecipientInformation = recip.iterator().next() as KeyTransRecipientInformation
    String(recipientInfo.getContent(JceKeyTransEnvelopedRecipient(keysCert().getOrThrow().second)))
}.onFailure { it.message }
r
Thank you @Kristian Nedrevold
142 Views