Skip to content

Instantly share code, notes, and snippets.

@jeffotoni
Created March 2, 2017 20:07
Show Gist options
  • Save jeffotoni/669dd7d01382022026a3d6e2c51e1beb to your computer and use it in GitHub Desktop.
Save jeffotoni/669dd7d01382022026a3d6e2c51e1beb to your computer and use it in GitHub Desktop.
/**
..######......#######.....##......##....########
.##....##....##.....##....##..##..##....##......
.##.................##....##..##..##....##......
..######......#######.....##..##..##....######..
.......##...........##....##..##..##....##......
.##....##....##.....##....##..##..##....##......
..######......#######......###..###.....##......
*
* S3WF Framework - golang
*
* Framework desenvolvido por @jeffotoni
*
* @package golang/pkg
* @author @jeffotoni
* @copyright Copyright (c) 2007 - 2017
* @license http://s3wf.com.br/
* @link http://s3wf.com.br
* @since Version 1.0
* @filesource
*/
// ---------------------------------------------------------------------
//
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base32"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func getToken(length int) string {
randomBytes := make([]byte, 256)
_, err := rand.Read(randomBytes)
if err != nil {
panic(err)
}
return base32.StdEncoding.EncodeToString(randomBytes)[:length]
}
func main() {
///gerando chave dinamicamente de 32 bytes
tokenKey := getToken(32)
key := []byte(tokenKey) // 32 bytes
fmt.Println("chave utilizada para criptografar: ", tokenKey)
///abrindo arquivo exemplo...
file, _ := os.Open("jad1.pdf") // For read access.
///pegando o tamanho em bytes do file..
fi, _ := file.Stat()
data := make([]byte, 16*fi.Size())
count, _ := file.Read(data)
file_copy, _ := os.Create("jad1-copy.pdf.crypt")
defer file_copy.Close()
ciphertext, _ := sCryptAes(key, data[:count])
//fmt.Println(err_c)
///gravando arquivo cryptografado
file_copy.Write(ciphertext)
fmt.Println("Arquivo [jad1-copy.pdf.crypt] criado e cryptografado")
file_cry, _ := os.Open("jad1-copy.pdf.crypt") // For read access.
///pegando o tamanho em bytes do file..
ficry, _ := file_cry.Stat()
data_cry := make([]byte, ficry.Size())
count_cry, _ := file_cry.Read(data_cry)
file_copy_cry, _ := os.Create("jad1-copy.pdf")
defer file_copy_cry.Close()
data_descry, _ := sDesCryptAes(key, data_cry[:count_cry])
file_copy_cry.Write(data_descry)
fmt.Println("Arquivo [jad1-copy.pdf] criado e descriptografado")
//
/////////FIM
///
os.Exit(0)
}
// See alternate IV creation from ciphertext below
//var iv = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}
func sCryptAes(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
return ciphertext, nil
}
func sDesCryptAes(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("muito pequena a cifra do texto")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return nil, err
}
return data, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment