Cryptography and Encoding
Encoding is a major part of cyber security related operations. Sometimes you may need to actually encode payloads or decode data using base64 or in more extreme cases ROT13. Below I have a few frameworks and programs that can actually help you understand encryption, ciphers and encodings.
Cryptography / Encoding | Encoding's ( base64/base32 )
There are many encodings go has built directly into its standard library and one of the more used encodings is the base set of encodings! The program below takes input text, detects if its a specific base encoding and decodes the text.
For context, bases in this means that it detected, encodes and decodes base64 and base32 strings.
package main
import (
"encoding/base32"
"encoding/base64"
"fmt"
)
type BaseDecoder struct {
Base64decoded []byte
Base32decoded []byte
}
var (
X error
BD BaseDecoder
)
func DetectBase32(input string) bool {
BD.Base32decoded, X = base32.StdEncoding.DecodeString(input)
return X == nil
}
func DetectBase64(input string) bool {
BD.Base64decoded, X = base64.StdEncoding.DecodeString(input)
return X == nil
}
func EncodeB64(input string) string {
return base64.RawStdEncoding.EncodeToString([]byte(input))
}
func EncodeB32(input string) string {
return base32.StdEncoding.EncodeToString([]byte(input))
}
func main() {
base32String := EncodeB32("foobar")
base64String := EncodeB64("Hello World!")
isBase32 := DetectBase32(base32String)
isBase64 := DetectBase64(base64String)
if isBase32 {
fmt.Println("[+] Found encoding! (BASE32)[" + base32String + "]")
fmt.Println(string(BD.Base32decoded))
}
if isBase64 {
fmt.Println("[+] Found encoding! (BASE32)[" + base64String + "]")
fmt.Println(string(BD.Base64decoded))
}
}This program is very easy to understand as we can see we just call base32 an base64 libraries.
Cryptography / Encoding | Ciphers (Vigenere, Beacon Cipher)
Ciphers are another big part of the cyber security realm especially if you may need to make a secret message and transfer it to someone in a file. However, unlike encryption, ciphers despite being cryptographic can become easily reversed. A program below is going to decode both the vigenere and beacon cipher after generating cipher text.
Cryptography / Encodings | Ciphers (Rot13)
Cryptography / Encoding | Encryption (AES) Encrypting Files
Sometimes, ciphers can not always do the job, however this is Why golang has a powerful library for encryption algorithms such as AES. More severe cases may require us to say encrypt a file or the data in a file and move it to a specified output file. Well, given how rich golang's standard library is we can easily do so as shown below.
but now we just end up with a file that is completely encrypted and cant be decrypted without a program to do so. Well, this is where the decryption comes into handy! With the same key we can decrypt the same data. Unlike your standard cipher or encoding we cant just reverse the math and then boom our issue is solved, we need an actual key to decrypt the data. This can be done like so.
This function once added to our program can be called with the same values. This time instead of calling the input file argument to be our origin input file we would use the output file. This means the call would look like this
and of course you can still change this as well.
Last updated