Kotlin- Blowfish Encryption and decryption Example

Blowfish is a symmetric-key block cipher, designed in 1993 by Bruce Schneier. Symmetric encryption uses a single encryption key to both encrypt and decrypt data. The sensitive data and the symmetric encryption key are utilized within the encryption algorithm to turn the sensitive data into ciphertext Blowfish provides a good encryption rate in software. However, the Advanced Encryption Standard (AES) now receives more attention, and Schneier recommends Twofish for modern applications.

Example:

import java.io.UnsupportedEncodingException
import java.nio.charset.Charset
import java.security.InvalidKeyException
import java.security.NoSuchAlgorithmException
import java.util.*
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import javax.crypto.spec.SecretKeySpec

/**
* This program demonstrates how to
* encrypt/decrypt input using the Blowfish
* Cipher with the Java Cryptograhpy.
* */
class BlowfishKnowledgeFactory {
@Throws(
NoSuchAlgorithmException::class,
NoSuchPaddingException::class,
InvalidKeyException::class,
IllegalBlockSizeException::class,
BadPaddingException::class,
UnsupportedEncodingException::class
)
fun encrypt(password: String, key: String): String {
val KeyData = key.toByteArray()
val KS = SecretKeySpec(KeyData, "Blowfish")
val cipher = Cipher.getInstance("Blowfish")
cipher.init(Cipher.ENCRYPT_MODE, KS)
return Base64.getEncoder().encodeToString(cipher.
doFinal(password.toByteArray(charset("UTF-8"))))
}

@Throws(
NoSuchAlgorithmException::class,
NoSuchPaddingException::class,
InvalidKeyException::class,
IllegalBlockSizeException::class,
BadPaddingException::class
)
fun decrypt(encryptedtext: String?, key: String): String {
val KeyData = key.toByteArray()
val KS = SecretKeySpec(KeyData, "Blowfish")
val ecryptedtexttobytes = Base64.getDecoder().
decode(encryptedtext)
val cipher = Cipher.getInstance("Blowfish")
cipher.init(Cipher.DECRYPT_MODE, KS)
val decrypted = cipher.doFinal(ecryptedtexttobytes)
return String(decrypted, Charset.forName("UTF-8"))
}

companion object {
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
val password = "Knf@123"
val key = "knowledgefactory"
println("Password: $password")
val obj = BlowfishKnowledgeFactory()
val enc_output = obj.encrypt(password, key)
println("Encrypted text: $enc_output")
val dec_output = obj.decrypt(enc_output, key)
println("Decrypted text: $dec_output")
}
}
}


Output:

Password: Knf@123
Encrypted text: 4DTHqnctCuk=
Decrypted text: Knf@123
 

More Related Topics...

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example