From abb503a88f4440d10691f5a52c5e4e75e60fe663 Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:37:23 +0530 Subject: [PATCH 1/4] Add implementation for 3-DES Encryption --- src/main/kotlin/DESEncryption.kt | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/kotlin/DESEncryption.kt diff --git a/src/main/kotlin/DESEncryption.kt b/src/main/kotlin/DESEncryption.kt new file mode 100644 index 0000000..fc6cbde --- /dev/null +++ b/src/main/kotlin/DESEncryption.kt @@ -0,0 +1,54 @@ +import javax.crypto.Cipher +import javax.crypto.spec.IvParameterSpec +import javax.crypto.spec.SecretKeySpec +import javax.crypto.SecretKey +import java.util.Arrays +import java.security.MessageDigest + + +class TripleDESDemo { + fun encrypt(message: String): ByteArray { + val md = MessageDigest.getInstance("md5") + val digestOfPassword = md.digest("HG58YZ3CR9".toByteArray(charset("utf-8"))) + val keyBytes = Arrays.copyOf(digestOfPassword, 24) + var j = 0 + var k = 16 + while (j < 8) { + keyBytes[k++] = keyBytes[j++] + } + val key: SecretKey = SecretKeySpec(keyBytes, "DESede") + val iv = IvParameterSpec(ByteArray(8)) + val cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding") + cipher.init(Cipher.ENCRYPT_MODE, key, iv) + val plainTextBytes = message.toByteArray(charset("utf-8")) + return cipher.doFinal(plainTextBytes) + } + + fun decrypt(message: ByteArray?): String { + val md = MessageDigest.getInstance("md5") + val digestOfPassword = md.digest("HG58YZ3CR9".toByteArray(charset("utf-8"))) + val keyBytes = Arrays.copyOf(digestOfPassword, 24) + var j = 0 + var k = 16 + while (j < 8) { + keyBytes[k++] = keyBytes[j++] + } + val key: SecretKey = SecretKeySpec(keyBytes, "DESede") + val iv = IvParameterSpec(ByteArray(8)) + val decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding") + decipher.init(Cipher.DECRYPT_MODE, key, iv) + val plainText = decipher.doFinal(message) + return String(plainText) + } + + companion object { + @JvmStatic + fun main(args: Array) { + val text = "Kotlin is a powerful programming language" + val codedtext = TripleDESDemo().encrypt(text) + val decodedtext = TripleDESDemo().decrypt(codedtext) + println(codedtext) + println(decodedtext) + } + } +} \ No newline at end of file From f29058fb2266acfe70be7517634beac292da451d Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:38:15 +0530 Subject: [PATCH 2/4] Add folder for Encryption algorithms --- src/main/kotlin/{ => Encryption}/DESEncryption.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/kotlin/{ => Encryption}/DESEncryption.kt (97%) diff --git a/src/main/kotlin/DESEncryption.kt b/src/main/kotlin/Encryption/DESEncryption.kt similarity index 97% rename from src/main/kotlin/DESEncryption.kt rename to src/main/kotlin/Encryption/DESEncryption.kt index fc6cbde..ce8a215 100644 --- a/src/main/kotlin/DESEncryption.kt +++ b/src/main/kotlin/Encryption/DESEncryption.kt @@ -51,4 +51,4 @@ class TripleDESDemo { println(decodedtext) } } -} \ No newline at end of file +} From 980b08be0017564d30d20f98b3ef31d5e60db9c6 Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:40:45 +0530 Subject: [PATCH 3/4] Add test cases for 3-DES encryption decryption --- src/test/kotlin/DESEncTest.kt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/kotlin/DESEncTest.kt diff --git a/src/test/kotlin/DESEncTest.kt b/src/test/kotlin/DESEncTest.kt new file mode 100644 index 0000000..44dd032 --- /dev/null +++ b/src/test/kotlin/DESEncTest.kt @@ -0,0 +1,28 @@ +import org.junit.Test +import java.util.* + +class DESEncTest { + + @Test + fun testWithKotlinString() { + val secretText = "Kotlin is a powerful programming language" + val encryptedValue = TripleDESDemo().encrypt(secretText) + assert(TripleDESDemo().decrypt(encryptedValue)==secretText) + + } + @Test + fun testWithIntellIjString() { + val secretText = "InteliJ IDEA Community Edition" + val encryptedValue = TripleDESDemo().encrypt(secretText) + assert(TripleDESDemo().decrypt(encryptedValue)==secretText) + + } + @Test + fun testWithAlgorithmjString() { + val secretText = "Algorithm-Repo" + val encryptedValue = TripleDESDemo().encrypt(secretText) + assert(TripleDESDemo().decrypt(encryptedValue)==secretText) + + } + +} \ No newline at end of file From e7cc559f9ff0b402d8a4a109ad7453b515937a7d Mon Sep 17 00:00:00 2001 From: Aishwarya0601 <69068161+Aishwarya0601@users.noreply.github.com> Date: Tue, 16 Nov 2021 00:41:40 +0530 Subject: [PATCH 4/4] Add folder for test cases for encryption algorithms --- src/test/kotlin/{ => Encryption}/DESEncTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/kotlin/{ => Encryption}/DESEncTest.kt (96%) diff --git a/src/test/kotlin/DESEncTest.kt b/src/test/kotlin/Encryption/DESEncTest.kt similarity index 96% rename from src/test/kotlin/DESEncTest.kt rename to src/test/kotlin/Encryption/DESEncTest.kt index 44dd032..fd4202c 100644 --- a/src/test/kotlin/DESEncTest.kt +++ b/src/test/kotlin/Encryption/DESEncTest.kt @@ -25,4 +25,4 @@ class DESEncTest { } -} \ No newline at end of file +}