I have the following AES Encryption function written in Golang.
func encrypt(key []byte, text string) string {
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return base64.URLEncoding.EncodeToString(ciphertext)
}
I am struggling to understand the flow to decrypt the generated text using Java. Any help will be highly appreciated!
Here is the Scala code, Not sure what's the problem with it.
def decode(input:String) = {
val keyBytes = Hex.decodeHex("someKey".toCharArray)
val inputWithoutPadding = input.substring(0,input.size - 2)
val inputArr:Seq[Byte] = Hex.decodeHex(inputWithoutPadding.toCharArray)
val skSpec = new SecretKeySpec(keyBytes, "AES")
val iv = new IvParameterSpec(inputArr.slice(0,16).toArray)
val dataToDecrypt = inputArr.slice(16,inputArr.size)
val cipher = Cipher.getInstance("AES/CFB/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv)
cipher.doFinal(dataToDecrypt.toArray)
}