Kotlin - Blowfish Encryption and decryption Example

Blowfish is an encryption method developed by Bruce Schneier in 1993 as an alternative to the DES encryption method. It is significantly faster than DES and provides good encryption speed, although no effective cryptanalysis technique has been found to date. It is one of the first secure block ciphers that is not protected by any patents and is therefore freely available for anyone to use. This is a symmetric block cipher algorithm.

  • Block size: 64 bits
  • Key size: variable size from 32 to 448 bits
  • number of subsections: 18 [P-array]
  • number of rounds: 16
  • number of substitution blocks: 4 [each with 512 records of 32 bits each]develop 

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

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

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete