Documentation ¶
Index ¶
- Constants
- Variables
- func DecryptAESCBC(ciphertext, key, iv []byte) ([]byte, error)
- func DecryptAESCBCUnknownButConsistentKey(ciphertext []byte) ([]byte, error)
- func DecryptAESECB(ciphertext, key []byte) ([]byte, error)
- func DecryptAESECBSuffix(encryptionFn EncryptionFunc) ([]byte, error)
- func DecryptAESECBUnknownButConsistentKey(ciphertext []byte) ([]byte, error)
- func DetermineBlockSize(fn EncryptionFunc) (int, error)
- func DeterminePrefixSize(fn EncryptionFunc) (n, blocks int, err error)
- func EncryptAESCBC(plaintext, key, iv []byte) ([]byte, error)
- func EncryptAESCBCUnknownButConsistentKey(plaintext []byte) ([]byte, error)
- func EncryptAESECB(plaintext, key []byte) ([]byte, error)
- func EncryptAESECBUnknownButConsistentKey(plaintext []byte) ([]byte, error)
- func EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix(plaintext []byte) ([]byte, error)
- func EncryptAESECBUnknownButConsistentKeyWithSuffix(plaintext []byte) ([]byte, error)
- func EncryptAESWithRandomKey(plaintext []byte) ([]byte, error)
- func HammingDistance(a, b []byte) (int, error)
- func HammingDistances(in []byte, blocksize int) ([]int, error)
- func HammingDistancesMap(in []byte, blocksize int) (map[int][]int, error)
- func Hex2Base64(in []byte) ([]byte, error)
- func LetterFrequenciesFromString(s string, n int) ([]rune, error)
- func MinHammingDistance(in []byte, blocksize int) (int, error)
- func NLetters(s string) int
- func NTopEnglish(s string) int
- func NumMatchingBlocks(in []byte, blocksize int) (int, error)
- func NumRepeatingBlocks(in []byte, blocksize int) (int, int, error)
- func PKCS7Padding(in []byte, blockSize int) []byte
- func PercentCommonEnglish(s string) float64
- func PercentLetters(s string) float64
- func RandomAESKey() []byte
- func RandomNBytes(n int) []byte
- func RepeatingXOR(plaintext, key []byte) ([]byte, error)
- func SolveRepeatingXOR(ciphertext []byte, keysize int) ([]byte, error)
- func SolveSingleByteXOR(in []byte) (key byte, plaintext string, err error)
- func StripPKCS7Padding(in []byte, blockSize int) ([]byte, error)
- func XORHexSlices(a, b []byte) ([]byte, error)
- type BlockMode
- type EncryptionFunc
- type LetterFrequencies
- type RuneFreq
Examples ¶
Constants ¶
const EnglishLetterFrequenciesTop13 = "ETAOIN SHRDLU"
const MaxPrefix = 1024 // only check for up to this size of fixed prefix in unknown ECB encryption function.
Variables ¶
var ( // ErrMismatchedLength is returned when parameters violate a length-matching invariant. ErrMismatchedLength = errors.New("mismatched lengths") // ErrEmpty is returned when a necessary value has not been provided. ErrEmpty = errors.New("empty") // ErrNotFound is returned when a searched-for value cannot be determined. ErrNotFound = errors.New("not found") // ErrInvalidPadding is returned when a value is improperly padded. ErrInvalidPadding = errors.New("invalid padding") )
Functions ¶
func DecryptAESCBC ¶
DecryptAESCBC decrypts the given ciphertext with the given key in CBC mode.
Example ¶
encoded, err := ioutil.ReadFile("testdata/set2/10.txt") if err != nil { panic(err) } contents := make([]byte, base64.StdEncoding.DecodedLen(len(encoded))) if _, err := base64.StdEncoding.Decode(contents, encoded); err != nil { panic(err) } d, err := DecryptAESCBC(contents, []byte("YELLOW SUBMARINE"), bytes.Repeat([]byte{byte(0x0)}, aes.BlockSize)) fmt.Printf("%v %q\n", err, d[:aes.BlockSize*3])
Output: <nil> "I'm back and I'm ringin' the bell \nA rockin' on "
func DecryptAESCBCUnknownButConsistentKey ¶
DecryptAESCBCUnknownButConsistentKey
func DecryptAESECB ¶
DecryptAESECB decrypts the given ciphertext with the given key in ECB mode.
func DecryptAESECBSuffix ¶
func DecryptAESECBSuffix(encryptionFn EncryptionFunc) ([]byte, error)
func DecryptAESECBUnknownButConsistentKey ¶
DecryptAESECBUnknownButConsistentKey
func DetermineBlockSize ¶
func DetermineBlockSize(fn EncryptionFunc) (int, error)
DetermineBlockSize returns the block size of the given encryption function in bytes.
func DeterminePrefixSize ¶
func DeterminePrefixSize(fn EncryptionFunc) (n, blocks int, err error)
DeterminePrefixSize returns the size necessary to include in a plaintext prefix to have the next bytes at the start of a new ECB block.
func EncryptAESCBC ¶
EncryptAESCBC encrypts the given plaintext with the given key in CBC mode.
func EncryptAESCBCUnknownButConsistentKey ¶
EncryptAESCBCUnknownButConsistentKey
func EncryptAESECB ¶
EncryptAESECB decrypts the given ciphertext with the given key in ECB mode.
func EncryptAESECBUnknownButConsistentKey ¶
EncryptAESECBUnknownButConsistentKey
func EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix ¶
EncryptAESECBUnknownButConsistentKeyWithPrefixAndSuffix
func EncryptAESECBUnknownButConsistentKeyWithSuffix ¶
EncryptAESECBUnknownButConsistentKeyWithSuffix
func EncryptAESWithRandomKey ¶
EncryptAESWithRandomKey
Example ¶
// 41 characters which will guarantee two repeated blocks. plaintext := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" for i := 0; i < 10; i++ { b, err := EncryptAESWithRandomKey([]byte(plaintext)) if err != nil { fmt.Println(err) } h := hex.EncodeToString(b) fmt.Printf("%#v %q\n", DetectECBorCBC(b), h) }
Output:
func HammingDistance ¶
HammingDistance computes the number of bits that differ between a and b.
func HammingDistances ¶
HammingDistances returns a list of hamming distances between the blocks in the provided byte slice.
func HammingDistancesMap ¶
HammingDistancesMap returns a map of hamming distances between the blocks in the provided byte slice.
func Hex2Base64 ¶
Hex2Base64 takes a byte slice of hex-encoded data and produces base64-encoded output.
func MinHammingDistance ¶
MinHammingDistance returns the minimum hamming distance between two blocks.
func NTopEnglish ¶
NTopEnglish returns the number of ocurrencies of common english letters in a string.
func NumMatchingBlocks ¶
NumMatchingBlocks computes the number of blocks that match for the given block size.
func NumRepeatingBlocks ¶
NumRepeatingBlocks computes the longest run of repeated blocks and the position of the beginning of the run in the given input.
func PKCS7Padding ¶
PKCS7Padding returns the provided input padded to the given block size.
Example ¶
fmt.Printf("%q", PKCS7Padding([]byte("YELLOW SUBMARINE"), 20))
Output: "YELLOW SUBMARINE\x04\x04\x04\x04"
func PercentCommonEnglish ¶
PercentCommonEnglish returns the percentage of characters that are appear to be common english letters.
func RepeatingXOR ¶
RepeatingXOR returns the hex-encoded result of repeated XOR application of key to plaintext.
func SolveRepeatingXOR ¶
SolveRepeatingXOR attempts to recover plaintext from a given repeated-xor encrypted ciphertext and given key size.
func SolveSingleByteXOR ¶
SolveSingleByteXOR attempts to recover plaintext from a single-byte xor encrypted ciphertext.
func StripPKCS7Padding ¶
StripPKCS7Padding returns an unpadded version of the input or a non-nil error if the PKCS7 padding is invalid.
func XORHexSlices ¶
XORHexSlices takes a two byte slices of hex-encoded data and produces the hex-encoded XOR result.
Types ¶
type BlockMode ¶
type BlockMode int
func DetectECBorCBC ¶
DetectECBorCBC is our oracle for detecting ECB vs CBC.
type EncryptionFunc ¶
type LetterFrequencies ¶
LetterFrequencies allows computation of rune frequencies.