diff --git a/src/main/kotlin/Encryption/DESEncryption.kt b/src/main/kotlin/Encryption/DESEncryption.kt new file mode 100644 index 0000000..ce8a215 --- /dev/null +++ b/src/main/kotlin/Encryption/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) + } + } +} diff --git a/src/test/kotlin/Encryption/DESEncTest.kt b/src/test/kotlin/Encryption/DESEncTest.kt new file mode 100644 index 0000000..fd4202c --- /dev/null +++ b/src/test/kotlin/Encryption/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) + + } + +}