Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed Crypto Suite Logo.png
Binary file not shown.
59 changes: 59 additions & 0 deletions des.go
Original file line number Diff line number Diff line change
@@ -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)]
}
7 changes: 4 additions & 3 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.
Expand Down