rsa

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

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

Go to latest
Published: May 11, 2020 License: MIT Imports: 5 Imported by: 0

README

rsa

rsa非对称加密解密,分段加密解密

package rsa

import (
	"testing"
)

const tLongStr = "333333333333333333333333333333333333333333333333333####################################################################################33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333"

func TestRsa(t *testing.T) {
	// 生成密钥对,保存到文件
	pub, pvt, err := GenerateRSAKeyPairs()
	if err != nil {
		t.Error(err)
	}
	tStr := "hello world你好,rsa"
	message := []byte(tStr)
	// 加密
	cipherText, err := Encrypt(message, pub)
	if err != nil {
		t.Error(err)
	}
	if tStr == string(cipherText) {
		t.Error("加密失败")
	}
	// 解密
	plainText, err := Decrypt(cipherText, pvt)
	if err != nil {
		t.Error(err)
	}
	if string(plainText) != tStr {
		t.Error("加解密失败")
	}
	longMsg := []byte(tLongStr)
	cipherTextLong, err := EncryptPadding(longMsg, pub)
	if err != nil {
		t.Error(err)
		return
	}
	if tLongStr == string(cipherTextLong) {
		t.Error("加密失败")
		return
	}
	// 解密
	plainTextLong, err := DecryptPadding(cipherTextLong, pvt)
	if err != nil {
		t.Error(err)
		return
	}
	if string(plainTextLong) != tLongStr {
		t.Error("加解密失败")
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decrypt

func Decrypt(cipherText []byte, privatePEM []byte) ([]byte, error)

RSA解密

func DecryptPadding

func DecryptPadding(cipherText, privatePEM []byte) (res []byte, err error)

DecryptPadding 分段解密

func Encrypt

func Encrypt(plainText []byte, publicPEM []byte) ([]byte, error)

Encrypt RSA加密

func EncryptPadding

func EncryptPadding(plainText, publicPEM []byte) (res []byte, err error)

EncryptPadding 分段加密

func GenerateRSAKeyPairs

func GenerateRSAKeyPairs() (publicKey []byte, privateKey []byte, err error)

GenerateRSAKeyPairs 生成RSA私钥和公钥

Types

This section is empty.

Jump to

Keyboard shortcuts

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