Skip to content

Rsa encryption #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/main/kotlin/Encryption/RSA.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
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")
}
30 changes: 30 additions & 0 deletions src/test/kotlin/Encryption/RSAEncTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}