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

Golang rsa.DecryptOAEP函数代码示例

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

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



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

示例1: authorize

// Function check if private key could decode what public key encode
func authorize(pubkey *rsa.PublicKey, privkey_str []byte) (bool, error) {
	block, _ := pem.Decode(privkey_str)
	if block == nil {
		return false, errors.New("Provided data is not private key")
	}
	if len(block.Bytes) < 100 {
		return false, errors.New("Provided data is not private key")
	}

	privkey := new(rsa.PrivateKey)

	if block.Type == "RSA PRIVATE KEY" {

		privkey, _ = x509.ParsePKCS1PrivateKey(block.Bytes)

	} else {
		return false, errors.New("Provided data is not private key")
	}

	// Encrypt message
	eComm, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, pubkey, []byte(testMessage), nil)
	if err != nil {
		return false, err
	}

	log.Println("Encrypted data " + string(eComm[:]))
	log.Println(eComm[:])
	test := []byte{
		75, 171, 179, 168, 150, 13, 169, 80, 26, 131, 139, 152, 174, 42, 69, 237, 56, 148, 43, 29, 48, 179, 116, 109, 226, 86, 102, 236, 225, 150, 192, 52, 96, 6, 203, 147, 106, 50, 228, 117, 89, 220, 214, 2, 156, 53, 86, 236, 73, 167, 186, 30, 27, 140, 67, 181, 65, 176, 95, 115, 189, 99, 72, 219, 88, 158, 242, 101, 78, 244, 137, 80, 150, 26, 20, 163, 190, 26, 165, 45, 82, 228, 140, 100, 116, 210, 63, 247, 103, 14, 131, 22, 172, 128, 171, 121, 13, 218, 2, 217, 119, 51, 243, 39, 22, 49, 165, 4, 204, 248, 251, 129, 190, 153, 27, 170, 56, 83, 108, 87, 56, 187, 88, 9, 119, 35, 247, 125,
	}

	msg1, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, test, nil)

	log.Println(string(msg1))

	// Decrypt message
	msg, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, eComm, nil)
	if err != nil {
		return false, err
	}

	// If Decrypted message euqal with original - private key is correct and user authorized

	if string(msg) == testMessage {
		return true, nil
	}

	return false, nil
}
开发者ID:dartkron,项目名称:RemoteExecuter,代码行数:50,代码来源:remoteExecutor.go


示例2: Decrypt

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


示例3: ReadWebSocket

func ReadWebSocket(ws *websocket.Conn) []byte {
	total_bytes_bytes_encrypted := make([]byte, key_len/8)
	if _, err := ws.Read(total_bytes_bytes_encrypted); err != nil {
		log.Println("(ReadWebSocket) Read Total Bytes: ", err)
	}

	sha1hash := sha1.New()
	total_bytes_bytes, err := rsa.DecryptOAEP(sha1hash, rand.Reader, pvkey, total_bytes_bytes_encrypted, nil)
	if err != nil {
		log.Println("(ReadWebSocket) Decryption: ", err)
	}

	var total_bytes uint32
	total_bytes = binary.LittleEndian.Uint32(total_bytes_bytes)

	// fmt.Printf("OAEP decrypted [%x] to \n[%d]\n",total_bytes_bytes_encrypted, total_bytes)

	var msg_splice [][]byte
	msg := make([]byte, total_bytes/2-64)
	if _, err := ws.Read(msg); err != nil {
		log.Println("(ReadTCPSocket) Read Msg: ", err)
	}
	msg2 := make([]byte, total_bytes-total_bytes/2+64)
	if _, err := ws.Read(msg2); err != nil {
		log.Println("(ReadTCPSocket) Read Msg2: ", err)
	}
	msg_splice = append(msg_splice, msg)
	msg_splice = append(msg_splice, msg2)
	return bytes.Join(msg_splice, nil)
}
开发者ID:imgemp,项目名称:WeTube,代码行数:30,代码来源:WeTubeClient.go


示例4: Decrypt

func (rk *rsaKey) Decrypt(msg []byte) ([]byte, error) {
	s, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, &rk.key, msg[kzHeaderLength:], nil)
	if err != nil {
		return nil, err
	}
	return s, nil
}
开发者ID:acasajus,项目名称:dkeyczar,代码行数:7,代码来源:rsa_key.go


示例5: CredulousDecodeAES

func CredulousDecodeAES(ciphertext string, privkey *rsa.PrivateKey) (plaintext string, err error) {
	in, err := base64.StdEncoding.DecodeString(ciphertext)
	if err != nil {
		return "", err
	}

	// pull apart the layers of base64-encoded JSON
	var encrypted AESEncryption
	err = json.Unmarshal(in, &encrypted)
	if err != nil {
		return "", err
	}

	encryptedKey, err := base64.StdEncoding.DecodeString(encrypted.EncodedKey)
	if err != nil {
		return "", err
	}

	// decrypt the AES key using the ssh private key
	aesKey, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privkey, encryptedKey, []byte("Credulous"))
	if err != nil {
		return "", err
	}

	plaintext, err = decodeAES(aesKey, encrypted.Ciphertext)

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


示例6: decryptRSA

func decryptRSA(in, out string, key *rsa.PrivateKey, verifykey string, armour bool) (err error) {
	pkg, err := ioutil.ReadFile(in)
	if err != nil {
		fmt.Println("[!]", err.Error())
		return
	}

	lockedKey, box, err := unpackageBox(pkg)
	if err != nil {
		return
	}

	hash := sha256.New()
	boxKey, err := rsa.DecryptOAEP(hash, rand.Reader, key, lockedKey, nil)
	if err != nil {
		fmt.Println("[!] RSA decryption failed:", err.Error())
		return
	}

	message, ok := secretbox.Open(box, boxKey)
	if !ok {
		fmt.Println("[!] failed to open box.")
		err = fmt.Errorf("opening box failed")
		return
	}
	err = ioutil.WriteFile(out, message, 0644)
	return
}
开发者ID:postfix,项目名称:sshbox,代码行数:28,代码来源:sshbox.go


示例7: DecryptOAEP

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


示例8: 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


示例9: decodePassword

func decodePassword(privatekeyfile string, in string) string {
	pemData, err := ioutil.ReadFile(privatekeyfile)
	block, _ := pem.Decode(pemData)
	if block == nil {
		fmt.Errorf("bad key data: %s", "not PEM-encoded")
	}
	if got, want := block.Type, "RSA PRIVATE KEY"; got != want {
		fmt.Errorf("unknown key type %q, want %q", got, want)
	}
	priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)

	if err != nil {
		fmt.Errorf("bad private key: %s", err)
	}
	//encrypt
	//out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &priv.PublicKey, []byte(in), []byte("aaa"))
	//if err != nil {
	//	fmt.Errorf("encrypt: %s", err)
	//}
	//encodedStr := base64.StdEncoding.EncodeToString(out)
	//fmt.Printf("encoded: %s \n", encodedStr)

	// Decrypt the data
	encodedBytes, _ := base64.StdEncoding.DecodeString(in)
	out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, priv, encodedBytes, []byte("aaa"))
	if err != nil {
		fmt.Errorf("decrypt: %s", err)
	}
	outStr := base64.StdEncoding.EncodeToString(out)
	fmt.Printf("decoded result %s\n", outStr)
	return outStr
}
开发者ID:nevermosby,项目名称:whalepirates-docker-hackathon-project,代码行数:32,代码来源:main.go


示例10: 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


示例11: Decrypt

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


示例12: Unwrap

func (alg *RsaOaep) Unwrap(encryptedCek []byte, key interface{}, cekSizeBits int, header map[string]interface{}) (cek []byte, err error) {
	if privKey, ok := key.(*rsa.PrivateKey); ok {
		return rsa.DecryptOAEP(alg.sha(), rand.Reader, privKey, encryptedCek, nil)
	}

	return nil, errors.New("RsaOaep.Unwrap(): expected key to be '*rsa.PrivateKey'")
}
开发者ID:useidel,项目名称:notary,代码行数:7,代码来源:rsa_oaep.go


示例13: Decrypt

func Decrypt() {
    key := Key()
    h, ha := HashAlgorithm()
    encrypted, err := ioutil.ReadFile(EncryptedFile)
    if err != nil {
        log.Fatalf("failed reading encrypted data: %s", err)
    }

    signature, err := ioutil.ReadFile(SignatureFile)
    if err != nil {
        log.Fatalf("failed saving signature data: %s", err)
    }

    if err = rsa.VerifyPKCS1v15(&key.PublicKey, ha, HashMessage(encrypted), signature); err != nil {
        log.Fatalf("message not valid: %s", err)
    } else {
        log.Printf("message is valid!")
    }

    plaintext, err := rsa.DecryptOAEP(h, rand.Reader, key, encrypted, nil)
    if err != nil {
        log.Fatalf("failed decrypting: %s", err)
    }
    log.Printf("decrypted message: %s", plaintext)
}
开发者ID:rrudduck,项目名称:golang-stuff,代码行数:25,代码来源:rsa.go


示例14: ServerDecryptHello

// Decrypt the Hello using the node's private comms key, and decode its
// contents.
func ServerDecryptHello(ciphertext []byte, ckPriv *rsa.PrivateKey, rng *xr.PRNG) (
	sOneShot *AesSession, version1s uint32, err error) {
	if rng == nil {
		rng = xr.MakeSystemRNG()
	}
	sha := sha1.New()
	data, err := rsa.DecryptOAEP(sha, nil, ckPriv, ciphertext, nil)
	// DEBUG
	if err == nil {
		expectedLen := 2*aes.BlockSize + 12
		if len(data) != expectedLen {
			fmt.Printf("expected OAEP packet len %d, actual %d bytes\n",
				expectedLen, len(data))
			err = WrongOAEPSize // XXX BAD NAME
		}
	}
	// END
	if err == nil {
		key1s := data[:2*aes.BlockSize]
		// salt1s = data[2*aes.BlockSize : 2*aes.BlockSize+8]
		vBytes := data[2*aes.BlockSize+8:]
		version1s = uint32(vBytes[0]) |
			uint32(vBytes[1])<<8 |
			uint32(vBytes[2])<<16 |
			uint32(vBytes[3])<<24
		sOneShot, err = NewAesSession(key1s, rng)
	}
	return
}
开发者ID:jddixon,项目名称:xlProtocol_go,代码行数:31,代码来源:helloAndReply.go


示例15: aeskeyFromRsa

// Takes the rsaaeskey message (in the ANNOUNCE body) and decrypts it to an
// aes key.
func aeskeyFromRsa(rsaaeskey64 string) (key []byte, err error) {
	s64 := base64pad(rsaaeskey64)
	s, err := base64.StdEncoding.DecodeString(s64)
	if err != nil {
		return
	}
	return rsa.DecryptOAEP(sha1.New(), nil, rsaPrivKey, s, nil)
}
开发者ID:frodosens,项目名称:go-airplay,代码行数:10,代码来源:auth.go


示例16: decryptRSA

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


示例17: decrypt

// Decrypt 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 rsaDecrypterSigner) decrypt(jek []byte, alg KeyAlgorithm, generator keyGenerator) ([]byte, error) {
	// Note: The random reader on decrypt operations is only used for blinding,
	// so stubbing is meanlingless (hence the direct use of rand.Reader).
	switch alg {
	case RSA1_5:
		defer func() {
			// DecryptPKCS1v15SessionKey sometimes panics on an invalid payload
			// because of an index out of bounds error, which we want to ignore.
			// This has been fixed in Go 1.3.1 (released 2014/08/13), the recover()
			// only exists for preventing crashes with unpatched versions.
			// See: https://groups.google.com/forum/#!topic/golang-dev/7ihX6Y6kx9k
			// See: https://code.google.com/p/go/source/detail?r=58ee390ff31602edb66af41ed10901ec95904d33
			_ = recover()
		}()

		// Perform some input validation.
		keyBytes := ctx.privateKey.PublicKey.N.BitLen() / 8
		if keyBytes != len(jek) {
			// Input size is incorrect, the encrypted payload should always match
			// the size of the public modulus (e.g. using a 2048 bit key will
			// produce 256 bytes of output). Reject this since it's invalid input.
			return nil, ErrCryptoFailure
		}

		cek, _, err := generator.genKey()
		if err != nil {
			return nil, ErrCryptoFailure
		}

		// When decrypting an RSA-PKCS1v1.5 payload, we must take precautions to
		// prevent chosen-ciphertext attacks as described in RFC 3218, "Preventing
		// the Million Message Attack on Cryptographic Message Syntax". We are
		// therefore deliberately ignoring errors here.
		_ = rsa.DecryptPKCS1v15SessionKey(rand.Reader, ctx.privateKey, jek, cek)

		return cek, nil
	case RSA_OAEP:
		// Use rand.Reader for RSA blinding
		return rsa.DecryptOAEP(sha1.New(), rand.Reader, ctx.privateKey, jek, []byte{})
	case RSA_OAEP_256:
		// Use rand.Reader for RSA blinding
		return rsa.DecryptOAEP(sha256.New(), rand.Reader, ctx.privateKey, jek, []byte{})
	}

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


示例18: DecryptWithPrivateKey

func DecryptWithPrivateKey(recipientPvtKey *rsa.PrivateKey, cipher []byte) ([]byte, error) {
	sha_hash := sha256.New()

	decrypted, err := rsa.DecryptOAEP(sha_hash, rand.Reader, recipientPvtKey, cipher, label)
	if err != nil {
		return nil, err
	}

	return decrypted, nil
}
开发者ID:golang-devops,项目名称:go-psexec,代码行数:10,代码来源:encryption_asymmetric.go


示例19: DecryptMessage

func DecryptMessage(message string, privateKey *rsa.PrivateKey) []byte {
	decodedMessage := base64Decode(message)
	decryptedMessage, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privateKey, []byte(decodedMessage), nil)

	if err != nil {
		return nil
	}

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


示例20: Dec

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



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang rsa.DecryptPKCS1v15函数代码示例发布时间:2022-05-24
下一篇:
Golang rc4.Cipher类代码示例发布时间: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