You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			129 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
			
		
		
	
	
			129 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Go
		
	
| package ncrypt
 | |
| 
 | |
| import (
 | |
| 	"crypto/rand"
 | |
| 	"crypto/rsa"
 | |
| 	"crypto/x509"
 | |
| 	"encoding/pem"
 | |
| 	"os"
 | |
| )
 | |
| 
 | |
| // GenerateRsaKey create rsa private and public pemo file.
 | |
| func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error {
 | |
| 	// private key
 | |
| 	privateKey, err := rsa.GenerateKey(rand.Reader, keySize)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	derText := x509.MarshalPKCS1PrivateKey(privateKey)
 | |
| 
 | |
| 	block := pem.Block{
 | |
| 		Type:  "rsa private key",
 | |
| 		Bytes: derText,
 | |
| 	}
 | |
| 
 | |
| 	file, err := os.Create(priKeyFile)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	err = pem.Encode(file, &block)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	file.Close()
 | |
| 
 | |
| 	// public key
 | |
| 	publicKey := privateKey.PublicKey
 | |
| 
 | |
| 	derpText, err := x509.MarshalPKIXPublicKey(&publicKey)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	block = pem.Block{
 | |
| 		Type:  "rsa public key",
 | |
| 		Bytes: derpText,
 | |
| 	}
 | |
| 
 | |
| 	file, err = os.Create(pubKeyFile)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	err = pem.Encode(file, &block)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	file.Close()
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // RsaEncrypt encrypt data with ras algorithm.
 | |
| func RsaEncrypt(data []byte, pubKeyFileName string) []byte {
 | |
| 	file, err := os.Open(pubKeyFileName)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	fileInfo, err := file.Stat()
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	defer file.Close()
 | |
| 	buf := make([]byte, fileInfo.Size())
 | |
| 
 | |
| 	_, err = file.Read(buf)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	block, _ := pem.Decode(buf)
 | |
| 
 | |
| 	pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	pubKey := pubInterface.(*rsa.PublicKey)
 | |
| 
 | |
| 	cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, data)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	return cipherText
 | |
| }
 | |
| 
 | |
| // RsaDecrypt decrypt data with ras algorithm.
 | |
| func RsaDecrypt(data []byte, privateKeyFileName string) []byte {
 | |
| 	file, err := os.Open(privateKeyFileName)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	fileInfo, err := file.Stat()
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	buf := make([]byte, fileInfo.Size())
 | |
| 	defer file.Close()
 | |
| 
 | |
| 	_, err = file.Read(buf)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	block, _ := pem.Decode(buf)
 | |
| 
 | |
| 	priKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 
 | |
| 	plainText, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, data)
 | |
| 	if err != nil {
 | |
| 		panic(err)
 | |
| 	}
 | |
| 	return plainText
 | |
| }
 |