zoukankan      html  css  js  c++  java
  • golang Rsa

    package models                                                                                                                                                               
    
    import (
        "crypto/rand"
        "crypto/rsa"
        "crypto/x509"
        "encoding/pem"
        "errors"
    )
    
    func RsaDecrypt(ciphertext []byte, pri_key []byte) ([]byte, error) {
    
        block, _ := pem.Decode(pri_key)
        if block == nil {
            return nil, errors.New("invalid rsa private key")
        }   
    
        priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
        if err != nil {
            return nil, err 
        }   
    
        return rsa.DecryptPKCS1v15(rand.Reader, priv, ciphertext)
    }
    
    func RsaEncrypt(plaintext []byte, pub_key []byte) ([]byte, error) {
    
        block, _ := pem.Decode(pub_key)
        if block == nil {
            return nil, errors.New("invalid rsa public key")
        }   
    
        pubInf, err := x509.ParsePKIXPublicKey(block.Bytes)
        if err != nil {
            return nil, err 
        }   
        pub := pubInf.(*rsa.PublicKey)
        return rsa.EncryptPKCS1v15(rand.Reader, pub, plaintext)
    }
    

      

  • 相关阅读:
    CF258D
    CF662C
    CF1295F
    CF1406E
    CF1270F
    CF1278F
    CF1523E
    CF1554E
    算法第四章上机实践报告
    LCA RMQ+ST表学习笔记
  • 原文地址:https://www.cnblogs.com/allenhaozi/p/5762879.html
Copyright © 2011-2022 走看看