diff --git a/Crypto Suite Logo.png b/Crypto Suite Logo.png deleted file mode 100644 index 3738292..0000000 Binary files a/Crypto Suite Logo.png and /dev/null differ diff --git a/des.go b/des.go new file mode 100644 index 0000000..ef8b69a --- /dev/null +++ b/des.go @@ -0,0 +1,59 @@ +package crsuite + +import ( + "bytes" + "crypto/cipher" + "crypto/des" +) + +// DESEncrypt performs DES encryption on plaintext using key and iv, and returns +// the ciphertext. +func DESEncrypt(plaintext []byte, key []byte, iv []byte) ([]byte, error) { + + block, blkErr := des.NewCipher(key) + if blkErr != nil { + return nil, blkErr + } + + blockSize := block.BlockSize() + originalData := PKCS5Padding(plaintext, blockSize) + blockMode := cipher.NewCBCEncrypter(block, iv) + ciphertext := make([]byte, len(originalData)) + blockMode.CryptBlocks(ciphertext, originalData) + return ciphertext, nil +} + +// DESDecrypt performs DES decryption on ciphertext using key and iv, and +// returns the decrypted plaintext thus obtained. +func DESDecrypt(ciphertext []byte, key []byte, iv []byte) ([]byte, error) { + block, err := des.NewCipher(key) + if err != nil { + return nil, err + } + + blockMode := cipher.NewCBCDecrypter(block, iv) + originalData := make([]byte, len(ciphertext)) + blockMode.CryptBlocks(originalData, ciphertext) + originalData = PKCS5UnPadding(originalData) + return originalData, nil +} + +// PKCS5Padding adds padding to src using the PKCS5 padding schema described in +// RFC 2898. +// +// Reference: https://www.ietf.org/rfc/rfc2898.txt +func PKCS5Padding(src []byte, blockSize int) []byte { + padding := blockSize - len(src)%blockSize + padtext := bytes.Repeat([]byte{byte(padding)}, padding) + return append(src, padtext...) +} + +// PKCS5Padding removes padding from src according to the PKCS5 padding schema +// described in RFC 2898. +// +// Reference: https://www.ietf.org/rfc/rfc2898.txt +func PKCS5UnPadding(src []byte) []byte { + length := len(src) + unpadding := int(src[length-1]) + return src[:(length - unpadding)] +} diff --git a/rsa.go b/rsa.go index e1d9cee..7ffb1db 100644 --- a/rsa.go +++ b/rsa.go @@ -4,12 +4,13 @@ import ( "crypto" "crypto/rand" "crypto/rsa" - "crypto/sha256" + "crypto/sha256" ) + // RSAEncrypt encrypts plaintext using the RSA (Rivest–Shamir–Adleman) // encryption algorithm. func RSAEncrypt(plaintext []byte, keySize int) (ciphertext []byte, publicKey *rsa.PublicKey, privateKey *rsa.PrivateKey, err error) { - privateKey, err = GenerateKeyPairs(keySize) + privateKey, err = GenerateKeyPairs(keySize) if err != nil { return } @@ -23,7 +24,7 @@ func RSAEncrypt(plaintext []byte, keySize int) (ciphertext []byte, publicKey *rs // RSADecrypt decrypts ciphertext using privateKey and returns the plaintext // thus obtained. func RSADecrypt(ciphertext []byte, privateKey *rsa.PrivateKey) ([]byte, error) { - return privateKey.Decrypt(nil, ciphertext, &rsa.OAEPOptions{Hash: crypto.SHA256}) + return privateKey.Decrypt(nil, ciphertext, &rsa.OAEPOptions{Hash: crypto.SHA256}) } // GenerateKeyPairs generates private and public keys for RSA encryption.