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.UnsupportedEncodingExceptionimport java.nio.charset.Charsetimport java.security.InvalidKeyExceptionimport java.security.NoSuchAlgorithmExceptionimport java.util.*import javax.crypto.BadPaddingExceptionimport javax.crypto.Cipherimport javax.crypto.IllegalBlockSizeExceptionimport javax.crypto.NoSuchPaddingExceptionimport 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)@JvmStaticfun 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...
- Vaadin + Kotlin CRUD example
- Kotlin + Spring Webflux File Upload (Single/Multiple)- REST API Example
- Kotlin - Spring Boot - WebFlux - Video Streaming example
- Kotlin + Spring Boot + OpenCSV Export Data to CSV Example
- Kotlin + Spring Boot + Apache Commons Export Data to CSV Example
- Kotlin + Spring Boot + iText PDF - Export data to PDF example
- WebSocket - Kotlin - Spring boot web application example
- Kotlin: RSA + AES a double layer security system
- Kotlin hashing - Using MD5, SHA-1, SHA-256, SHA-384, SHA-512, and PBKDF2
- Kotlin- AES, RSA, 3DES Encryption and Decryption with example
- Iterate over the Map, List, and Set in Kotlin! How?
- Kotlin Programming in Visual Studio Code IDE [Ubuntu]