decrypt

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DecryptEnv     = `DECRYPT_ENV`
	DecryptDataVar = `decrypted-data`
	DefaultQRPath  = `/tmp/extracted_qr.png`
)

Variables

View Source
var DecryptCmd = &bonzai.Cmd{
	Name:  "decrypt",
	Alias: "d",
	Short: "decrypt embedded QR Code from an image",
	Comp:  comp.Cmds,
	Cmds: []*bonzai.Cmd{
		ImageCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
}

**🔹 Main Decrypt Command**

View Source
var ExtractCmd = &bonzai.Cmd{
	Name:  "extract",
	Alias: "x",
	Short: "decrypt text from extracted QR code",
	Comp:  comp.Cmds,
	Cmds: []*bonzai.Cmd{
		TextCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Do: func(x *bonzai.Cmd, args ...string) error {

		if len(args) < 2 {
			fmt.Println("error: provide a password (key) to be used to decrypt the message.")
			return fmt.Errorf("usage: decrypt image <input> [<output>] extract text <key>")
		}

		if args[0] == TextCmd.Name {
			return TextCmd.Do(TextCmd, args[1:]...)
		}
		return nil
	},
}

**🔹 Extract Command for Decrypting Text**

View Source
var ImageCmd = &bonzai.Cmd{
	Name:  "image",
	Alias: "i",
	Short: "extract QR Code from an image",
	Comp:  comp.Cmds,
	Cmds: []*bonzai.Cmd{
		ExtractCmd,
		vars.Cmd.AsHidden(),
		help.Cmd.AsHidden(),
	},
	Vars: bonzai.Vars{
		{
			K: DecryptDataVar,
			V: `foo`,
			E: DecryptEnv,
			S: `decrypted data extracted from an image`,
			P: true,
		},
	},
	Do: func(x *bonzai.Cmd, args ...string) error {
		if len(args) < 1 {
			return fmt.Errorf("usage: decrypt image <input-image> [<output-qrcode>, defaults to /tmp/extracted_qr.png]")
		}

		inputImage := args[0]
		outputQR := DefaultQRPath
		if len(args) > 1 && args[1] != ExtractCmd.Name {
			outputQR = args[1]
		}

		fmt.Println("Extracting QR code from image:", inputImage)

		err := ExtractQRCodeFromJPEG(inputImage, outputQR)
		if err != nil {
			return fmt.Errorf("failed to extract QR code from image: %w", err)
		}

		fmt.Println("Extracted QR Code saved to:", outputQR)

		if args[1] == ExtractCmd.Name {

			return ExtractCmd.Do(ExtractCmd, args[2:]...)
		}

		if args[2] == ExtractCmd.Name {

			return ExtractCmd.Do(ExtractCmd, args[3:]...)
		}

		return nil
	},
}

**🔹 Decrypt Image Command**

View Source
var TextCmd = &bonzai.Cmd{
	Name:  "text",
	Alias: "t",
	Short: "decrypt text using AES",
	Do: func(x *bonzai.Cmd, args ...string) error {
		if len(args) < 1 {
			return fmt.Errorf("usage: decrypt image <input> [<output>] extract text <key>")
		}

		key := args[0]
		fmt.Println("key", key)
		outputQR := DefaultQRPath

		qrText, err := ReadQRCode(outputQR)
		if err != nil {
			return fmt.Errorf("failed to read QR code: %w", err)
		}

		fmt.Println("🛠️ Extracted QR Code (Base64):", qrText)

		decryptedText, err := DecryptAES(qrText, key)
		if err != nil {
			return fmt.Errorf("failed to decrypt text: %w", err)
		}

		vars.Data.Set(DecryptDataVar, decryptedText)
		fmt.Println("Decrypted Text:", decryptedText)

		return nil
	},
}

**🔹 Extract Text Command**

Functions

func ConvertBitstreamToQRImage

func ConvertBitstreamToQRImage(bitstream []byte, size int) (image.Image, error)

ConvertBitstreamToQRImage reconstructs a QR code image from bitstream

func DecryptAES

func DecryptAES(encryptedBase64, key string) (string, error)

**🔹 Decrypt AES (Reversible from Encrypt)**

func ExtractQRCodeFromJPEG

func ExtractQRCodeFromJPEG(inputPath string, outputQRPath string) error

ExtractQRCodeFromJPEG extracts QR code bits from a JPEG's DCT coefficients

func ReadQRCode

func ReadQRCode(filePath string) (string, error)

**🔹 Read QR Code from Image (Fix Base64 Formatting)**

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL