Skip to content
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

Des encryption #81

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
54 changes: 54 additions & 0 deletions src/main/kotlin/Encryption/DESEncryption.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
val text = "Kotlin is a powerful programming language"
val codedtext = TripleDESDemo().encrypt(text)
val decodedtext = TripleDESDemo().decrypt(codedtext)
println(codedtext)
println(decodedtext)
}
}
}
28 changes: 28 additions & 0 deletions src/test/kotlin/Encryption/DESEncTest.kt
Original file line number Diff line number Diff line change
@@ -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)

}

}