xcrypto

package module
v0.0.0-...-b223a7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

README

ctypto

Go Reference

RSA

RSA非对称加密公私钥对生成,输出PEM格式的公私钥对,同时支持PKCS#1、PKCS#8密钥格式输出;生成的公私钥对,可拷贝到文本文件,保存为.key文件即可使用。

**PEM格式:**RSA公私钥对常用的编码方式,OPENSSL以PEM格式为主,相对DER可读性更强,以BASE64编码呈现; 开头类似 -----BEGIN PRIVATE KEY----- 结尾类似 -----END PRIVATE KEY-----

PKCS#8密钥格式,多用于JAVA、PHP程序加解密中,为目前用的比较多的密钥、证书格式; PKCS#1密钥格式,多用于JS等其它程序加解密,属于比较老的格式标准。 PKCS#1PKCS#8的主要区别,从本质上说,PKCS#8格式增加验证数据段,保证密钥正确性。

示例

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PKCS8PrivateKey

type PKCS8PrivateKey struct {
	PrivateKey *rsa.PrivateKey
}

func ParsePKCS8PrivateKey

func ParsePKCS8PrivateKey(privateKey []byte) (private PKCS8PrivateKey, err error)

func (PKCS8PrivateKey) RsaDecryptPKCS1v15

func (r PKCS8PrivateKey) RsaDecryptPKCS1v15(ciphertext []byte) (source []byte, err error)

type PKIXPublicKey

type PKIXPublicKey struct {
	PublicKey *rsa.PublicKey
}

func ParsePKIXPublicKey

func ParsePKIXPublicKey(publicKey []byte) (public PKIXPublicKey, err error)

func (PKIXPublicKey) RsaEncryptPKCS1v15

func (r PKIXPublicKey) RsaEncryptPKCS1v15(source []byte) (ciphertext []byte, err error)

type RsaKeyPKCS8

type RsaKeyPKCS8 struct {
	PrivateKeyBytes []byte
	PublicKeyBytes  []byte
}

func GenRsaKeyPKCS8

func GenRsaKeyPKCS8(keySize uint) (rsaKey RsaKeyPKCS8, err error)

GenRsaKey(1024)

Example
package main

import (
	xbase64 "github.com/goclub/base64"
	xcrypto "github.com/goclub/crypto"

	xerr "github.com/goclub/error"
	"log"
)

func main() {
	var err error
	defer func() {
		if err != nil {
			xerr.PrintStack(err)
		}
	}()
	rsaKey, err := xcrypto.GenRsaKeyPKCS8(1024)
	if err != nil {
		return
	}
	log.Print("rsaKey.PublicKeyBytes:\n", string(rsaKey.PublicKeyBytes))
	log.Print("rsaKey.PrivateKey:\n", string(rsaKey.PrivateKeyBytes))
	publicKey, err := xcrypto.ParsePKIXPublicKey(rsaKey.PublicKeyBytes)
	if err != nil {
		return
	}
	cipherText, err := publicKey.RsaEncryptPKCS1v15([]byte("goclub.run"))
	if err != nil {
		return
	}
	log.Print("base64(cipherText):\n", string(xbase64.EncodeRawStd(cipherText)))
	privateKey, err := xcrypto.ParsePKCS8PrivateKey(rsaKey.PrivateKeyBytes)
	if err != nil {
		return
	}
	source, err := privateKey.RsaDecryptPKCS1v15(cipherText)
	if err != nil {
		return
	}
	log.Print("source:\n", string(source)) // goclub.run
}
Output:

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL