golang Golang RSA OAEP Decrypt
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang 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
PW9n7aYT7DtJ7FGebw+h8dZP5Q8vWqUeKzRR5p+90hOemtCTcxSEVfucWyKlWoat
Q/oYJOR5t0YHi40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5GpoC151IpKf4guoPbCHX
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))
}
golang Golang RSA-OAEP加密和解密
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
)
func main() {
var publicKeyData = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAygGoUiTD+LjwZIgwFZyj
iibWNQ2LM9xZ2pjKQGP8iUBtAuAW629/Ofw8qxToMyixPrG4A7j8+KOPwYrWPGV6
Og//4zm3cG+1hQvnNUWtMjHHBY8OByUPQ6/T8XHER1DxFBfnWfFLZ1yFX6oNNuvt
LgOreI6ehehJd5IB/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kTX9DArLE4d6IRF/1
WW/AlNPuwVgxl0kcJILFtLqA1WoC5UWMhbYe2YB/Q3rCozmn0AiwyqECgYEA0qxd
KClKAMIsrB0WJ9gZEsJOpFi4q4g6T1BwT40Xj6Ul6o6DHi6hFhPgZAstqmnY0ANz
ezQ2yxtIm7zSy7S+nwDUycjY9riJcomc/YQZNA2QVM16hEv84VLwH1MVV2wkTb41
DWjbcg/ZNofZHl9AQIw7es+R3mmtDN+8BZOZSp8CgYBHtwmaUQm1VQtbswAyHfuz
8KApgklCSvQ5SRBj38UDrw0LTnZ+/k+Ar+MH8ORUskvrblQgG7ZbQD9Z+YYzzX6/
hsBuqe9Vwb4/jsfGqHagdDA3OTegmlRpE9A06xInJKggZfi15gry+UYok7dS2pXq
fsHWk8capOP2oiKYEeHs4QKBgF2KcLaDVrtte/5Tz+GTHtbodZidWCm5jAJpeeSo
hfye3G4AJxHArH+sBacGG5md88mwrpbWwTl/fMbBmWsfbsAU02ZhCozJtSWpGo6q
F7K4DwzIS4zwXHEDrWCLOF+fwaLPQKkalM1ZYh3HRc0ph9LhMQu/nEn/6/laYhar
yZWLAoGASvCrpFKn0qllMKNUetBmYFpgtjmnNuW7l0xT2UftkW6AuFjU19gKgXhe
I+uZciHQ8kIUHfNLYBbhETsF3iqsklKfeoIr23zYHLE5GpoC151IpKf4guoPbCHX
a1oCDuZm//f5HMePb9juJN0WR//d5jWuizAycZf41XoEd8Bqydg=
-----END RSA PRIVATE KEY-----
`
pubKeyBlock, _ := pem.Decode([]byte(publicKeyData))
hash := sha1.New()
random := rand.Reader
msg := []byte("helloworldjemyme")
var pub *rsa.PublicKey
pubInterface, parseErr := x509.ParsePKIXPublicKey(pubKeyBlock.Bytes)
if parseErr != nil {
fmt.Println("Load public key error")
panic(parseErr)
}
pub = pubInterface.(*rsa.PublicKey)
encryptedData, encryptErr := rsa.EncryptOAEP(hash, random, pub, msg, nil)
if encryptErr != nil {
fmt.Println("Encrypt data error")
panic(encryptErr)
}
encodedData := base64.URLEncoding.EncodeToString(encryptedData)
fmt.Println(encodedData)
fmt.Println()
fmt.Println("------I AM JUST A LINE------")
fmt.Println()
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)
}
decryptedData, decryptErr := rsa.DecryptOAEP(hash, random, pri, encryptedData, nil)
if decryptErr != nil {
fmt.Println("Decrypt data error")
panic(decryptErr)
}
fmt.Println(string(decryptedData))
}
以上是关于golang Golang RSA OAEP Decrypt的主要内容,如果未能解决你的问题,请参考以下文章