Golang & Java Examples

Golang Example:

https://go.dev/play/p/luErVYlD5Nd


package main

import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"log"
)

func decryptCFB(encryptedBase64, key string) ([]byte, error) {
// Decode the base64 encrypted string to get raw bytes
encryptedData, err := base64.StdEncoding.DecodeString(encryptedBase64)
if err != nil {
return nil, fmt.Errorf("failed to decode base64 data: %v", err)
}

// Check if the encrypted data is at least as large as the AES block size
if len(encryptedData) < aes.BlockSize {
	return nil, fmt.Errorf("encrypted data is too short")
}

// Take the first 16 bytes as IV (Initialization Vector)
iv := encryptedData[:aes.BlockSize]

// The rest of the data is the actual ciphertext
ciphertext := encryptedData[aes.BlockSize:]

// Create a new AES cipher block with the provided key
block, err := aes.NewCipher([]byte(key))
if err != nil {
	return nil, fmt.Errorf("failed to create AES cipher: %v", err)
}

// Create a CFB decrypter stream using the block and IV
stream := cipher.NewCFBDecrypter(block, iv)

// Allocate a byte slice to store the decrypted data
decryptedData := make([]byte, len(ciphertext))
// Perform the decryption in place
stream.XORKeyStream(decryptedData, ciphertext)

return decryptedData, nil

}

func main() {
encryptedText := "LqkuGRkozbDPQgzeaxH1WwiDVs7ROE1Kq0XubXIHrZQ="

// Example key (must be 16, 24, or 32 bytes for AES-128, AES-192, or AES-256)
key := "123456789012345678904444" // replace with your actual key

// Decrypt the data
decryptedData, err := decryptCFB(encryptedText, key)
if err != nil {
	log.Fatalf("Decryption failed: %v", err)
}

// Print the decrypted data as a string
fmt.Printf("Decrypted text: %s\n", string(decryptedData))

}


Java Example:


import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESCFBDecrypt {

public static byte[] decryptCFB(String encryptedBase64, String key) throws Exception {
    // Decode the base64 encrypted string to get raw bytes
    byte[] encryptedData = Base64.getDecoder().decode(encryptedBase64);

    // Check if the encrypted data is at least as large as the AES block size
    if (encryptedData.length < 16) {
        throw new IllegalArgumentException("Encrypted data is too short");
    }

    // Take the first 16 bytes as IV (Initialization Vector)
    byte[] iv = new byte[16];
    System.arraycopy(encryptedData, 0, iv, 0, 16);

    // The rest of the data is the actual ciphertext
    byte[] ciphertext = new byte[encryptedData.length - 16];
    System.arraycopy(encryptedData, 16, ciphertext, 0, encryptedData.length - 16);

    // Create a new AES cipher block with the provided key
    SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

    // Initialize the cipher for decryption in CFB mode
    Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);

    // Perform the decryption
    return cipher.doFinal(ciphertext);
}

public static void main(String[] args) {
    String encryptedText = "LqkuGRkozbDPQgzeaxH1WwiDVs7ROE1Kq0XubXIHrZQ=";
    String key = "123456789012345678904444"; //your actual key

    try {
        // Decrypt the data
        byte[] decryptedData = decryptCFB(encryptedText, key);

        // Print the decrypted data as a string
        System.out.println("Decrypted text: " + new String(decryptedData));
    } catch (Exception e) {
        System.err.println("Decryption failed: " + e.getMessage());
    }
}

}


What’s Next