Skip to content

Commit

Permalink
Update: encrypt and decrypt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fidays committed Jun 9, 2023
1 parent be41601 commit e84f1a7
Showing 1 changed file with 17 additions and 59 deletions.
76 changes: 17 additions & 59 deletions binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,33 +176,20 @@ func (b Binary) Encrypt(key []byte) Binary {
return nil
}

// Generate a random initialization vector (IV)
iv := make([]byte, aes.BlockSize)
_, err = io.ReadFull(rand.Reader, iv)
if err != nil {
// Create a new byte slice to store the ciphertext
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]

// Generate a random IV (initialization vector)
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil
}

// Pad the data to the nearest multiple of the block size
paddedData := padData(b, aes.BlockSize)

// Create a new CBC mode encrypter using the AES block cipher
mode := cipher.NewCBCEncrypter(block, iv)

// Create a buffer for the encrypted data
encrypted := make([]byte, len(paddedData))
// Use cipher.NewCTR to create a stream cipher for encryption
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], b)

// Encrypt the data
mode.CryptBlocks(encrypted, paddedData)

// Prepend the IV to the encrypted data
encrypted = append(iv, encrypted...)

// Encode the encrypted data as base64 for readability
encoded := make([]byte, base64.StdEncoding.EncodedLen(len(encrypted)))
base64.StdEncoding.Encode(encoded, encrypted)

return encoded
return ciphertext
}

func (b Binary) Decrypt(key []byte) Binary {
Expand All @@ -211,45 +198,16 @@ func (b Binary) Decrypt(key []byte) Binary {
return nil
}

// Decode the base64-encoded encrypted data
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(b)))
n, err := base64.StdEncoding.Decode(decoded, b)
if err != nil {
if len(b) < aes.BlockSize {
return nil
}
decoded = decoded[:n]

// Extract the IV from the encrypted data
iv := decoded[:aes.BlockSize]
encryptedData := decoded[aes.BlockSize:]
iv := b[:aes.BlockSize]
ciphertext := b[aes.BlockSize:]

// Create a new CBC mode decrypter using the AES block cipher
mode := cipher.NewCBCDecrypter(block, iv)

// Create a buffer for the decrypted data
decrypted := make([]byte, len(encryptedData))

// Decrypt the data
mode.CryptBlocks(decrypted, encryptedData)

// Remove the padding from the decrypted data
unpadded := unpadData(decrypted)

return unpadded
}

//----------------------------- helper -----------------------------

// Pad the data to the nearest multiple of blockSize using PKCS7 padding
func padData(data []byte, blockSize int) []byte {
padding := blockSize - (len(data) % blockSize)
padded := append(data, bytes.Repeat([]byte{byte(padding)}, padding)...)
return padded
}
// Use cipher.NewCTR to create a stream cipher for decryption
stream := cipher.NewCTR(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)

// Remove the PKCS7 padding from the data
func unpadData(data []byte) []byte {
padding := int(data[len(data)-1])
unpadded := data[:len(data)-padding]
return unpadded
return ciphertext
}

0 comments on commit e84f1a7

Please sign in to comment.