Skip to content

Instantly share code, notes, and snippets.

@jemygraw
Created November 13, 2014 02:31
Show Gist options
  • Save jemygraw/f31c74b0e0e8ec7c7d15 to your computer and use it in GitHub Desktop.
Save jemygraw/f31c74b0e0e8ec7c7d15 to your computer and use it in GitHub Desktop.
Golang RSA OAEP Decrypt
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
)
//c# encrypted data
var encryptedData = `A1bZVqSMn983riamWR1ArGVOlMEllOjkCZVcmlyl9sgUYWr4ui1_g6XAfFi0EJrRsLeI2I2586x76fDN_8thT1jLcjdYcWDrcB24f7bgZ8iyRI2qMEJxYln4pkz3v5R07T7eG0sv1p5YNihObg48PzjIydaey5yM7vl8Cq1Hs-chz77qu6heObkyh6239WQgyRF3cqpLH_OPQ51fjTYI9zDfiptbAOJSE5WKDEArXLZxnb1NissiqOGdkpM9q4F7TRKzJ3RFxH5Gkcpx0A3xq8_HU7Z71O4Dv_emfjIcJyAJLWA4Ye831MVblcGYexcNzVqHQPmkJSfgl5GmQwyKbTevsggIMh9Jzhrbrg9En2zScnGMRsbJDC6zdoupr5EIXLfU`
var privateKeyData = `
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAygGoUiTD+LjwZIgwFZyjiibWNQ2LM9xZ2pjKQGP8iUBtAuAW
629/Ofw8qxToMyixPrG4A7j8+KOPwYrWPGV6Og//4zm3cG+1hQvnNUWtMjHHBY8O
ByUPQ6/T8XHER1DxFBfnWfFLZ1yFX6oNNuvtLgOreI6ehehJd5IB/4mOjMvFEBgO
Eejado2n55VNdcFpdQ3RcvGV+f/rl/lsIM08QvL3lc5gqawj53sW9YZi1DL/uN48
R+ghvAYhtx2jpHDBvlH1NCF1rU6CynYsgV9QIksv0ihwl4T+k5F9ir0uv0WIS6kK
KS1SRpAprRKunos4PlE8l2+jC6LaJUPhDZlj/wIDAQABAoIBAHIcX5YPeLie2AUi
PW9n7aYT7DtJ7FGeb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T2UftkW6AuFjU19gKgXhe
I+uZciHQ8kIUHfNLYBbhETsF3iqsklKfeoIr23zYHLE5GpoC151IpKf4guoPbCHX
a1oCDuZm//f5HMePb9juJN0WR//d5jWuizAycZf41XoEd8Bqydg=
-----END RSA PRIVATE KEY-----
`
func main() {
//golang encrypted data
encryptedData = `eE167RoMG0AgczAMUdaQ0581PRuCQA1_xNNkqiq-zR6y93YXz6hqqP-arAwGCcUySFqxvvgopVyWBABM5MBnhEGhUBvADae6EAwNWSFxm5buUBHFmHqYNrRWQaxO8F7QUZK1llctd_euoWixxAKCVIqnE0UsbcZEveULYgm3EVwbJTPUyfdRbX--lmDbsWHM2YEVuwL6f1jph_XrwKWUt62jbRnp21f7zPj2mWFZEUvCt_s6KfzymZRNky-0-QSKVI4IygZM2ZZ9gjjj7CXnrvpyTYc2_P86SHaYTP9ZhoOwdTixaRE60hnzZ5N1oUXYI99LD_-E6NziZxGoXg6qsA==`
hash := sha1.New()
random := rand.Reader
privateKeyBlock, _ := pem.Decode([]byte(privateKeyData))
var pri *rsa.PrivateKey
pri, parseErr := x509.ParsePKCS1PrivateKey(privateKeyBlock.Bytes)
if parseErr != nil {
fmt.Println("Load private key error")
panic(parseErr)
}
decodedData, _ := base64.URLEncoding.DecodeString(encryptedData)
decryptedData, decryptErr := rsa.DecryptOAEP(hash, random, pri, decodedData, nil)
if decryptErr != nil {
fmt.Println("Decrypt data error")
panic(decryptErr)
}
fmt.Println(string(decryptedData))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment