From bd81737e42cbee0ad4285e9749a73690f64e432b Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:39:44 +0530 Subject: [PATCH 1/4] Adding implementation of RSA Encryption --- src/main/kotlin/RSA.kt | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/main/kotlin/RSA.kt diff --git a/src/main/kotlin/RSA.kt b/src/main/kotlin/RSA.kt new file mode 100644 index 0000000..af7fb92 --- /dev/null +++ b/src/main/kotlin/RSA.kt @@ -0,0 +1,72 @@ +import java.security.spec.PKCS8EncodedKeySpec +import javax.crypto.Cipher +import java.security.spec.X509EncodedKeySpec +import java.io.IOException +import java.security.* +import java.util.* + + +class RSA { + var privateKey: PrivateKey + var publicKey: PublicKey + @Throws(GeneralSecurityException::class, IOException::class) + fun loadPublicKey(stored: String): Key { + val data: ByteArray = Base64.getDecoder().decode(stored.toByteArray()) + val spec = X509EncodedKeySpec(data) + val fact = KeyFactory.getInstance("RSA") + return fact.generatePublic(spec) + } + + @Throws(Exception::class) + fun encryptMessage(plainText: String, publickey: String): String { + val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") + cipher.init(Cipher.ENCRYPT_MODE, loadPublicKey(publickey)) + return Base64.getEncoder().encodeToString(cipher.doFinal + (plainText.toByteArray())) + } + + + @Throws(Exception::class) + fun decryptMessage(encryptedText: String?, privatekey: String): String { + val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding") + cipher.init(Cipher.DECRYPT_MODE, loadPrivateKey(privatekey)) + return String(cipher.doFinal(Base64.getDecoder().decode(encryptedText))) + } + + + @Throws(GeneralSecurityException::class) + fun loadPrivateKey(key64: String): PrivateKey { + val clear: ByteArray = Base64.getDecoder().decode(key64.toByteArray()) + val keySpec = PKCS8EncodedKeySpec(clear) + val fact = KeyFactory.getInstance("RSA") + val priv = fact.generatePrivate(keySpec) + Arrays.fill(clear, 0.toByte()) + return priv + } + + + + init { + val keyGen = KeyPairGenerator.getInstance("RSA") + keyGen.initialize(1024) + val pair = keyGen.generateKeyPair() + privateKey = pair.private + publicKey = pair.public + } +} +fun main(args: Array) { + val secretText = "www.knowledgefactory.net" + val keyPairGenerator = RSA() + + val privateKey: String = Base64.getEncoder(). + encodeToString(keyPairGenerator.privateKey.encoded) + val publicKey: String = Base64.getEncoder(). + encodeToString(keyPairGenerator.publicKey.encoded) + println("Private Key: $privateKey") + println("Public Key: $publicKey") + + val encryptedValue = keyPairGenerator.encryptMessage(secretText, publicKey) + println("Encrypted Value: $encryptedValue") + val decryptedText = keyPairGenerator.decryptMessage(encryptedValue, privateKey) + println("Decrypted output: $decryptedText") +} \ No newline at end of file From b8ff76e06545c23a4d6917b1846a7085c461c57e Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:40:19 +0530 Subject: [PATCH 2/4] Adding folder for encryption --- src/main/kotlin/{ => Encryption}/RSA.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/kotlin/{ => Encryption}/RSA.kt (97%) diff --git a/src/main/kotlin/RSA.kt b/src/main/kotlin/Encryption/RSA.kt similarity index 97% rename from src/main/kotlin/RSA.kt rename to src/main/kotlin/Encryption/RSA.kt index af7fb92..f76c327 100644 --- a/src/main/kotlin/RSA.kt +++ b/src/main/kotlin/Encryption/RSA.kt @@ -69,4 +69,4 @@ fun main(args: Array) { println("Encrypted Value: $encryptedValue") val decryptedText = keyPairGenerator.decryptMessage(encryptedValue, privateKey) println("Decrypted output: $decryptedText") -} \ No newline at end of file +} From 60056652407cc7cb61c932605dd8e63f8d6f2195 Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:42:59 +0530 Subject: [PATCH 3/4] Adding test case for RSA Encryption --- src/test/kotlin/RSAEncTest.kt | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/kotlin/RSAEncTest.kt diff --git a/src/test/kotlin/RSAEncTest.kt b/src/test/kotlin/RSAEncTest.kt new file mode 100644 index 0000000..2f0e975 --- /dev/null +++ b/src/test/kotlin/RSAEncTest.kt @@ -0,0 +1,30 @@ +import encryption.Caesar +import org.junit.Test +import java.util.* + +class RSAEncTest { + val keyPairGenerator = RSA() + val privateKey: String = Base64.getEncoder(). + encodeToString(keyPairGenerator.privateKey.encoded) + val publicKey: String = Base64.getEncoder(). + encodeToString(keyPairGenerator.publicKey.encoded) + @Test + fun testWithKotlinString() { + val secretText = "Kotlin is a powerful programming language" + val encryptedValue = keyPairGenerator.encryptMessage(secretText, publicKey) + assert(keyPairGenerator.decryptMessage(encryptedValue, privateKey)==secretText) + + } + @Test + fun testWithIntellIjString() { + val secretText = "InteliJ IDEA Community Edition" + val encryptedValue = keyPairGenerator.encryptMessage(secretText, publicKey) + assert(keyPairGenerator.decryptMessage(encryptedValue, privateKey)==secretText) + } + @Test + fun testWithAlgorithmjString() { + val secretText = "Algorithm-Repo" + val encryptedValue = keyPairGenerator.encryptMessage(secretText, publicKey) + assert(keyPairGenerator.decryptMessage(encryptedValue, privateKey)==secretText) + } +} \ No newline at end of file From 6c0d249daa5bf87883e109c23470d3fb745cd63f Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Thu, 21 Oct 2021 11:43:35 +0530 Subject: [PATCH 4/4] Adding folder for Encryption --- src/test/kotlin/{ => Encryption}/RSAEncTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/kotlin/{ => Encryption}/RSAEncTest.kt (97%) diff --git a/src/test/kotlin/RSAEncTest.kt b/src/test/kotlin/Encryption/RSAEncTest.kt similarity index 97% rename from src/test/kotlin/RSAEncTest.kt rename to src/test/kotlin/Encryption/RSAEncTest.kt index 2f0e975..f1b9cc2 100644 --- a/src/test/kotlin/RSAEncTest.kt +++ b/src/test/kotlin/Encryption/RSAEncTest.kt @@ -27,4 +27,4 @@ class RSAEncTest { val encryptedValue = keyPairGenerator.encryptMessage(secretText, publicKey) assert(keyPairGenerator.decryptMessage(encryptedValue, privateKey)==secretText) } -} \ No newline at end of file +}