• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang rsa.EncryptOAEP函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中crypto/rsa.EncryptOAEP函数的典型用法代码示例。如果您正苦于以下问题:Golang EncryptOAEP函数的具体用法?Golang EncryptOAEP怎么用?Golang EncryptOAEP使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了EncryptOAEP函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: encrypt

// Encrypt the given payload. Based on the key encryption algorithm,
// this will either use RSA-PKCS1v1.5 or RSA-OAEP (with SHA-1 or SHA-256).
func (ctx rsaEncrypterVerifier) encrypt(cek []byte, alg KeyAlgorithm) ([]byte, error) {
	switch alg {
	case RSA1_5:
		return rsa.EncryptPKCS1v15(randReader, ctx.publicKey, cek)
	case RSA_OAEP:
		return rsa.EncryptOAEP(sha1.New(), randReader, ctx.publicKey, cek, []byte{})
	case RSA_OAEP_256:
		return rsa.EncryptOAEP(sha256.New(), randReader, ctx.publicKey, cek, []byte{})
	}

	return nil, ErrUnsupportedAlgorithm
}
开发者ID:ossrs,项目名称:go-oryx-lib,代码行数:14,代码来源:asymmetric.go


示例2: EncryptOAEP

func (enc *CryptorOAEP) EncryptOAEP(plaintextBytes []byte, label []byte) ([]byte, error) {
	if enc.RsaPublicKey == nil {
		err := errors.New("402: RSA Public Key Not Set")
		return []byte{}, err
	}
	return rsa.EncryptOAEP(enc.Hash, rand.Reader, enc.RsaPublicKey, plaintextBytes, label)
}
开发者ID:VukDukic,项目名称:gotilla,代码行数:7,代码来源:rsautil.go


示例3: MarshalJSON

func (s secret) MarshalJSON() ([]byte, error) {
	m, err := rsa.EncryptOAEP(crypto.SHA512.New(), rand.Reader, key.Public().(*rsa.PublicKey), []byte(s), nil)
	if err != nil {
		return nil, err
	}
	return json.Marshal(base64.StdEncoding.EncodeToString(m))
}
开发者ID:dylanpoe,项目名称:golang.org,代码行数:7,代码来源:secret.go


示例4: Encrypt

func (s *SignedMessage) Encrypt(publicKey *rsa.PublicKey) (encrypted *EncryptedMessage, err error) {
	encrypted = new(EncryptedMessage)
	hash := sha256.New()

	//Join message and signature
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	err = enc.Encode(s)
	if err != nil {
		return nil, err
	}

	//Split messages to parts acceptable by encrypter
	k := (publicKey.N.BitLen() + 7) / 8
	maxSize := k - 2*hash.Size() - 2 //from rsa.go
	toEncrypt := splitMsg(buf.Bytes(), maxSize)

	//encrypt
	parts := make([][]byte, len(toEncrypt))
	for i, part := range toEncrypt {
		parts[i], err = rsa.EncryptOAEP(hash, rand.Reader, publicKey, part, nil)
		if err != nil {
			return nil, err
		}
	}

	encrypted.Msg = parts
	return encrypted, nil
}
开发者ID:radious,项目名称:enc,代码行数:29,代码来源:encryption.go


示例5: Encrypt

// Encrypt encrypts a given plaintext using the provided public key.
// The encryption process uses random data. Therefore, this function is not deterministic.
func Encrypt(publicKey *rsa.PublicKey, plaintext []byte) (EncryptedMessage, error) {
	if publicKey == nil {
		return EncryptedMessage{}, errors.New("public key must not be nil")
	}

	aesKey, err := generateSymmetricKey()
	if err != nil {
		return EncryptedMessage{}, err
	}

	ciphertext, err := symmetricEncrypt(aesKey, plaintext)
	if err != nil {
		return EncryptedMessage{}, err
	}

	encryptedKey, err := rsa.EncryptOAEP(sha256.New(), rand.Reader, publicKey, aesKey, nil)
	if err != nil {
		return EncryptedMessage{}, err
	}

	return EncryptedMessage{
		Ciphertext:   ciphertext,
		EncryptedKey: encryptedKey,
	}, nil
}
开发者ID:rosenhouse,项目名称:bosh-classroom,代码行数:27,代码来源:bletchley.go


示例6: TestPad

func TestPad(t *testing.T) {
	private, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil || private == nil {
		t.Fatal("Can't gen private key %s\n", err)
	}
	public := &private.PublicKey
	var a [9]byte
	copy(a[0:8], "IDENTITY")

	seed := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
	encrypted_secret, err := rsa.EncryptOAEP(sha1.New(), rand.Reader,
		public, seed, a[0:9])
	if err != nil {
		t.Fatal("Can't encrypt ", err)
	}
	fmt.Printf("encrypted_secret: %x\n", encrypted_secret)
	decrypted_secret, err := rsa.DecryptOAEP(sha1.New(), rand.Reader,
		private, encrypted_secret, a[0:9])
	if err != nil {
		t.Fatal("Can't decrypt ", err)
	}
	fmt.Printf("decrypted_secret: %x\n", decrypted_secret)
	var N *big.Int
	var D *big.Int
	var x *big.Int
	var z *big.Int
	N = public.N
	D = private.D
	x = new(big.Int)
	z = new(big.Int)
	x.SetBytes(encrypted_secret)
	z = z.Exp(x, D, N)
	decrypted_pad := z.Bytes()
	fmt.Printf("decrypted_pad   : %x\n", decrypted_pad)
}
开发者ID:tmroeder,项目名称:cloudproxy,代码行数:35,代码来源:support_test.go


示例7: Encrypt

func (t *rsaTransformer) Encrypt(value []byte) ([]byte, error) {
	encrypted, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, &t.rsaPrivateKey.PublicKey, value, nil)
	if err != nil {
		return nil, err
	}
	return []byte(EncodeToString(encrypted)), nil
}
开发者ID:codeship,项目名称:go-encrypt,代码行数:7,代码来源:rsa.go


示例8: CredulousEncode

// returns a base64 encoded ciphertext.
// OAEP can only encrypt plaintexts that are smaller than the key length; for
// a 1024-bit key, about 117 bytes. So instead, this function:
// * generates a random 32-byte symmetric key (randKey)
// * encrypts the plaintext with AES256 using that random symmetric key -> cipherText
// * encrypts the random symmetric key with the ssh PublicKey -> cipherKey
// * returns the base64-encoded marshalled JSON for the ciphertext and key
func CredulousEncode(plaintext string, pubkey ssh.PublicKey) (ciphertext string, err error) {
	rsaKey := sshPubkeyToRsaPubkey(pubkey)
	randKey := make([]byte, 32)
	_, err = rand.Read(randKey)
	if err != nil {
		return "", err
	}

	encoded, err := encodeAES(randKey, plaintext)
	if err != nil {
		return "", err
	}

	out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &rsaKey, []byte(randKey), []byte("Credulous"))
	if err != nil {
		return "", err
	}
	cipherKey := base64.StdEncoding.EncodeToString(out)

	cipherStruct := AESEncryption{
		EncodedKey: cipherKey,
		Ciphertext: encoded,
	}

	tmp, err := json.Marshal(cipherStruct)
	if err != nil {
		return "", err
	}

	ciphertext = base64.StdEncoding.EncodeToString(tmp)

	return ciphertext, nil
}
开发者ID:realestate-com-au,项目名称:credulous,代码行数:40,代码来源:crypto.go


示例9: ClientEncryptHello

// Create an AES IV and key and an 8-byte salt, then encrypt these and
// the proposed protocol version using the server's comms public key.
func ClientEncryptHello(version1 uint32, ck *rsa.PublicKey, rng *xr.PRNG) (
	cOneShot *AesSession, ciphertext []byte, err error) {

	if rng == nil {
		rng = xr.MakeSystemRNG()
	}
	vBytes := make([]byte, 4)
	vBytes[0] = byte(version1)
	vBytes[1] = byte(version1 >> 8)
	vBytes[2] = byte(version1 >> 16)
	vBytes[3] = byte(version1 >> 24)

	// Generate 32-byte AES key, and 8-byte salt for the Hello
	salty := make([]byte, 2*aes.BlockSize+8+20)
	rng.NextBytes(salty)

	key1 := salty[:2*aes.BlockSize]
	// salt1 := salty[2*aes.BlockSize : 2*aes.BlockSize+8]
	oaep1 := salty[2*aes.BlockSize+8:]
	oaepSalt := bytes.NewBuffer(oaep1)

	sha := sha1.New()
	data := salty[:2*aes.BlockSize+8] // contains key1,salt1
	data = append(data, vBytes...)    // ... plus preferred protocol version

	ciphertext, err = rsa.EncryptOAEP(sha, oaepSalt, ck, data, nil)
	if err == nil {
		cOneShot, err = NewAesSession(key1, rng)
	}
	return
}
开发者ID:jddixon,项目名称:xlProtocol_go,代码行数:33,代码来源:helloAndReply.go


示例10: Encrypt

// Encrypts a symmetric key to this identity
func (this *PublicIdentity) Encrypt(key *SymmetricKey) (ek *EncryptedKey) {
	out, err := rsa.EncryptOAEP(sha256.New224(), rand.Reader, this.key, key.key, nil)
	if err != nil {
		panic(err)
	}
	return &EncryptedKey{impl: out}
}
开发者ID:Javantea,项目名称:h0tb0x,代码行数:8,代码来源:crypto.go


示例11: main

func main() {
	privateKey, err := rsa.GenerateKey(rand.Reader, 512)
	if err != nil {
		fmt.Printf("rsa.GenerateKey: %v\n", err)
	}

	message := "Hello World!"
	messageBytes := bytes.NewBufferString(message)
	sha1 := sha1.New()

	encrypted, err := rsa.EncryptOAEP(sha1, rand.Reader, &privateKey.PublicKey, messageBytes.Bytes(), nil)
	if err != nil {
		fmt.Printf("EncryptOAEP: %s\n", err)
	}

	decrypted, err := rsa.DecryptOAEP(sha1, rand.Reader, privateKey, encrypted, nil)
	if err != nil {
		fmt.Printf("decrypt: %s\n", err)
	}

	decryptedString := bytes.NewBuffer(decrypted).String()
	fmt.Printf("message: %v\n", message)
	fmt.Printf("encrypted: %v\n", encrypted)
	fmt.Printf("decryptedString: %v\n", decryptedString)
}
开发者ID:leepro,项目名称:goplay,代码行数:25,代码来源:x509.go


示例12: RSAEncrypt

// Encrypts a message with the given public key using RSA-OAEP and the sha1 hash function.
func RSAEncrypt(pub *rsa.PublicKey, msg []byte) []byte {
	b, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, pub, msg, nil)
	if err != nil {
		panic(err)
	}
	return b
}
开发者ID:24hours,项目名称:go-steam,代码行数:8,代码来源:rsa.go


示例13: Enc

func (ct *CryptoTool) Enc(in []byte) []byte {
	out, err := rsa.EncryptOAEP(ct.sha1, rand.Reader, ct.publicKey, in, nil)
	if err != nil {
		log.Fatalln("EncryptOAEP fail", err)
	}
	return out
}
开发者ID:weimingtom,项目名称:tank_battle,代码行数:7,代码来源:cryp.go


示例14: encryptWithAPIPublicKey

func encryptWithAPIPublicKey(publicKey *rsa.PublicKey, message []byte) []byte {
	encryptedMessage, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, publicKey, message, nil)
	if err != nil {
		return nil
	}

	return encryptedMessage
}
开发者ID:rakhra,项目名称:launchkey-go,代码行数:8,代码来源:cryptography.go


示例15: encryptRSA

// encrypts a given message with a provided RSA public key.
// the label must be the same for the encryption and decryption for it to work.
func encryptRSA(pub *rsa.PublicKey, message []byte) []byte {
	label := make([]byte, 10)
	encMessage, err := rsa.EncryptOAEP(sha512.New(), rand.Reader, pub, message, label)
	fmt.Println(encMessage)
	if err != nil {
		fmt.Println("encryption failed!: ", err)
	}
	return encMessage
}
开发者ID:postfix,项目名称:secureBox,代码行数:11,代码来源:encryption.go


示例16: EncryptKey

// EncryptKey encrypts a 16-byte key with the RSA or EC key of the record.
func (pr PasswordRecord) EncryptKey(in []byte) (out []byte, err error) {
	if pr.Type == RSARecord {
		return rsa.EncryptOAEP(sha1.New(), rand.Reader, &pr.RSAKey.RSAPublic, in, nil)
	} else if pr.Type == ECCRecord {
		return ecdh.Encrypt(pr.ECKey.ECPublic, in)
	} else {
		return nil, errors.New("Invalid function for record type")
	}
}
开发者ID:johntdyer,项目名称:golang-devops-stuff,代码行数:10,代码来源:passvault.go


示例17: Encrypt

// Encrypt encrypts the message with RSAES-OAEP, checking to ensure that it is
// not longer than the maximum allowed message length. It uses SHA256
// as the underlying hash algorithm.
func Encrypt(pub *rsa.PublicKey, pt []byte) (ct []byte, err error) {
	if len(pt) > MaxMessageLength(pub) {
		err = fmt.Errorf("message is too long")
		return
	}

	hash := sha256.New()
	ct, err = rsa.EncryptOAEP(hash, rand.Reader, pub, pt, defaultLabel)
	return
}
开发者ID:jonathanmarvens,项目名称:gocrypto,代码行数:13,代码来源:crypto.go


示例18: Encrypt

//Encrypt Encrypt the given message with the given public key.
func Encrypt(content []byte, publickey *rsa.PublicKey) ([]byte, error) {
	md5hash := md5.New()
	label := []byte("")

	encryptedmsg, err := rsa.EncryptOAEP(md5hash, rand.Reader, publickey, content, label)
	if err != nil {
		return nil, err
	}
	return encryptedmsg, nil
}
开发者ID:MrGosti,项目名称:rosa,代码行数:11,代码来源:rosa.go


示例19: Encrypt_oaep

//OAEP Encrypt
func Encrypt_oaep(public_key *rsa.PublicKey, plain_text, label []byte) (encrypted []byte) {
	var err error
	var md5_hash hash.Hash

	md5_hash = md5.New()

	if encrypted, err = rsa.EncryptOAEP(md5_hash, rand.Reader, public_key, plain_text, label); err != nil {
		log.Fatal(err)
	}
	return
}
开发者ID:engineerbeard,项目名称:barrens_server,代码行数:12,代码来源:barrenoidrsa.go


示例20: Encrypt

func (rk *rsaPublicKey) Encrypt(msg []byte) ([]byte, error) {
	// FIXME: If msg is too long for keysize, EncryptOAEP returns an error
	// Do we want to return a Keyczar error here, either by checking
	// ourselves for this case or by wrapping the returned error?
	s, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &rk.key, msg, nil)
	if err != nil {
		return nil, err
	}
	h := append(makeHeader(rk), s...)
	return h, nil
}
开发者ID:acasajus,项目名称:dkeyczar,代码行数:11,代码来源:rsa_key.go



注:本文中的crypto/rsa.EncryptOAEP函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang rsa.EncryptPKCS1v15函数代码示例发布时间:2022-05-24
下一篇:
Golang rsa.DecryptPKCS1v15SessionKey函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap