Skyla  Feeney

Skyla Feeney

1604910449

How to Implement RSA Encryption with Go

Asymmetric Encryption, or Public Key Encryption, is a fundamental part of security on modern systems. It assures the authentication and non-repudiation principles of cryptography.

In this tutorial, we are going to see how to implement it using Go.

Note: the version of Go used in this article is 1.15.3.

TL;DR: you can find the complete code here.

Introduction

RSA is a public-key algorithm. It is named after its creators (_Rivest-Shamir-Adleman). _It was made public in 1977 and it is one of the most used algorithms today.

The public-key cryptography, also known as asymmetric cryptography, uses two different keys, one to encrypt and another one to decrypt:

  • A public key is used in the process whenever there is no secret involved, such as encryption or verification.
  • A private key is used when there is a secret involved, such as decryption or signing.

Image for post

Image by author.

This means we can share the public key with anyone we want, so they can encrypt any information they want to send us. The only way to access this information is to use the private key to decrypt. This is why it must be kept secret.

The public and private keys are generated together (the public key is derived from the private) and form a key pair.

Image for post

Key pair generation process.

Note: if you want to know more about how the keys are generated or how the process of encryption works, you can check this video.

Let’s see how to implement it using Go.

Key Pair Generation

The first thing we have to do is generate the public and private keys. If you already a pair, you can skip this process and read it from the file.

The package crypto/rsagenerates the keys, with the packagecrypto/rand generating the random numbers.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"fmt"
)

func generateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
	// This method requires a random number of bits.
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		fmt.Println("Error: ", err)
	}

	// The public key is part of the PrivateKey struct
	return privateKey, &privateKey.PublicKey
}

func main() {
	// Generate a 2048-bits key
	privateKey, publicKey := generateKeyPair(2048)

	fmt.Printf("Private key: %v\n", privateKey)
	fmt.Printf("Public Key: %v", publicKey)
}

In order to use the key with another program, or at another time, you can save the keys into a file. The usual way is to save as a PEM format, using the package encoding/pem.

The public key is part of the private key struct, so you don’t need to save them both. But remember to never share this file with anyone.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
)

// ommited function for simplicity

// Export public key as a string in PEM format
func exportPubKeyAsPEMStr(pubkey *rsa.PublicKey) string {
	pubKeyPem := string(pem.EncodeToMemory(
		&pem.Block{
			Type:  "RSA PUBLIC KEY",
			Bytes: x509.MarshalPKCS1PublicKey(pubkey),
		},
	))
	return pubKeyPem
}

// Export private key as a string in PEM format
func exportPrivKeyAsPEMStr(privkey *rsa.PrivateKey) string {
	privKeyPem := string(pem.EncodeToMemory(
		&pem.Block{
			Type:  "RSA PRIVATE KEY",
			Bytes: x509.MarshalPKCS1PrivateKey(privkey),
		},
	))
	return privKeyPem

}

// Save string to a file
func saveKeyToFile(keyPem, filename string) {
	pemBytes := []byte(keyPem)
	ioutil.WriteFile(filename, pemBytes, 0400)
}

func main() {
	// Generate a 2048-bits key
	privateKey, publicKey := generateKeyPair(2048)

	fmt.Printf("Private key: %v\n", privateKey)
	fmt.Printf("Public Key: %v", publicKey)

  // Create PEM string 
	privKeyStr := exportPrivKeyAsPEMStr(privateKey)
	pubKeyStr := exportPubKeyAsPEMStr(publicKey)

	fmt.Println(privKeyStr)
	fmt.Println(pubKeyStr)

	saveKeyToFile(privKeyStr, "privkey.pem")
	saveKeyToFile(pubKeyStr, "pubkey.pem")
}

Note: the error handling was omitted for simplicity. You should check them in real applications.

#go #security #progamming #developer

What is GEEK

Buddha Community

How to Implement RSA Encryption with Go
Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Skyla  Feeney

Skyla Feeney

1604910449

How to Implement RSA Encryption with Go

Asymmetric Encryption, or Public Key Encryption, is a fundamental part of security on modern systems. It assures the authentication and non-repudiation principles of cryptography.

In this tutorial, we are going to see how to implement it using Go.

Note: the version of Go used in this article is 1.15.3.

TL;DR: you can find the complete code here.

Introduction

RSA is a public-key algorithm. It is named after its creators (_Rivest-Shamir-Adleman). _It was made public in 1977 and it is one of the most used algorithms today.

The public-key cryptography, also known as asymmetric cryptography, uses two different keys, one to encrypt and another one to decrypt:

  • A public key is used in the process whenever there is no secret involved, such as encryption or verification.
  • A private key is used when there is a secret involved, such as decryption or signing.

Image for post

Image by author.

This means we can share the public key with anyone we want, so they can encrypt any information they want to send us. The only way to access this information is to use the private key to decrypt. This is why it must be kept secret.

The public and private keys are generated together (the public key is derived from the private) and form a key pair.

Image for post

Key pair generation process.

Note: if you want to know more about how the keys are generated or how the process of encryption works, you can check this video.

Let’s see how to implement it using Go.

Key Pair Generation

The first thing we have to do is generate the public and private keys. If you already a pair, you can skip this process and read it from the file.

The package crypto/rsagenerates the keys, with the packagecrypto/rand generating the random numbers.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"fmt"
)

func generateKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey) {
	// This method requires a random number of bits.
	privateKey, err := rsa.GenerateKey(rand.Reader, bits)
	if err != nil {
		fmt.Println("Error: ", err)
	}

	// The public key is part of the PrivateKey struct
	return privateKey, &privateKey.PublicKey
}

func main() {
	// Generate a 2048-bits key
	privateKey, publicKey := generateKeyPair(2048)

	fmt.Printf("Private key: %v\n", privateKey)
	fmt.Printf("Public Key: %v", publicKey)
}

In order to use the key with another program, or at another time, you can save the keys into a file. The usual way is to save as a PEM format, using the package encoding/pem.

The public key is part of the private key struct, so you don’t need to save them both. But remember to never share this file with anyone.

package main

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"io/ioutil"
)

// ommited function for simplicity

// Export public key as a string in PEM format
func exportPubKeyAsPEMStr(pubkey *rsa.PublicKey) string {
	pubKeyPem := string(pem.EncodeToMemory(
		&pem.Block{
			Type:  "RSA PUBLIC KEY",
			Bytes: x509.MarshalPKCS1PublicKey(pubkey),
		},
	))
	return pubKeyPem
}

// Export private key as a string in PEM format
func exportPrivKeyAsPEMStr(privkey *rsa.PrivateKey) string {
	privKeyPem := string(pem.EncodeToMemory(
		&pem.Block{
			Type:  "RSA PRIVATE KEY",
			Bytes: x509.MarshalPKCS1PrivateKey(privkey),
		},
	))
	return privKeyPem

}

// Save string to a file
func saveKeyToFile(keyPem, filename string) {
	pemBytes := []byte(keyPem)
	ioutil.WriteFile(filename, pemBytes, 0400)
}

func main() {
	// Generate a 2048-bits key
	privateKey, publicKey := generateKeyPair(2048)

	fmt.Printf("Private key: %v\n", privateKey)
	fmt.Printf("Public Key: %v", publicKey)

  // Create PEM string 
	privKeyStr := exportPrivKeyAsPEMStr(privateKey)
	pubKeyStr := exportPubKeyAsPEMStr(publicKey)

	fmt.Println(privKeyStr)
	fmt.Println(pubKeyStr)

	saveKeyToFile(privKeyStr, "privkey.pem")
	saveKeyToFile(pubKeyStr, "pubkey.pem")
}

Note: the error handling was omitted for simplicity. You should check them in real applications.

#go #security #progamming #developer

Hertha  Walsh

Hertha Walsh

1598050632

A Short Guide to Encryption using Go

Cryptography is mandatory in the current modern world. In this era of big data and data science, it is important to make sure your data is protected from a malicious attack.

There are many algorithms and techniques to ensure that your system is secure and no unauthorized access will be done to your data.

Symmetric encryption is one of these techniques, and in this tutorial, I will show you how to do it in Go.

Note: The version of Go used in this tutorial is 1.14.7.

Introduction

The symmetric-key encryption is a technique used to encrypt a message where there is only one secret key. This key is used on both parts of the process, that is, to encrypt and decrypt the message.

Using any kind of symmetric-key algorithm, a message is converted into a form that cannot be understood. Unless someone has the secret key. In this case, both the sender and the receiver have the same key (exchanged in some secure way) so they can send messages and understand each other.

The algorithm used determines the size of the key and the mode that the process of encryption or decryption is done. And, usually, longer the key, harder is to hack it and more secure is your encryption. For example, a 128-bits key can take up to billions of years to be broken by a normal computer.

There are two types of algorithms:

  • Block: the message is encrypted using fixed-size blocks. Example: DESAES, etc.
  • Stream: the message is encrypted taking individual characters. Example: RC4Salsa20, etc.

#encryption #decryption #programming #go #golang

Zander  Herzog

Zander Herzog

1596793260

Secure HTTPS servers in Go

In this article, we are going to look at some of the basic APIs of the http package to create and initialize HTTPS servers in Go.

Image for post

(source: unsplash.com)

In the “Simple Hello World Server” lesson, we learned about net/http package, how to create routes and how [ServeMux](https://golang.org/pkg/net/http/#ServeMux) works. In the “Running multiple HTTP servers” lesson, we learned about [Server](https://golang.org/pkg/net/http/#Server) structure and how to run multiple HTTP servers concurrently.

In this lesson, we are going to create an HTTPS server using both Go’s standard server configuration and custom configuration (using [_Server_](https://golang.org/pkg/net/http/#Server) structure). But before this, we need to know what HTTPS really is?

HTTPS is a big topic of discussion in itself. Hence while writing this lesson, I published an article just on “How HTTPS works?”. I advise you to read this lesson first before continuing this article. In this article, I’ve also described the encryption paradigm and SSL certificates generation process.


If we recall the simplest HTTP server example from previous lessons, we only need http.``[ListenAndServe](https://golang.org/pkg/net/http/#ListenAndServe) function to start an HTTP server and http.``[HandleFunc](https://golang.org/pkg/net/http/#HandleFunc) to register a response handler for a particular endpoint.

Image for post

(https://play.golang.org/p/t3sOenOYAzS)

In the example above, when we run the command go run server.go , it will start an HTTP server on port 9000. By visiting http://localhost:9000 URL in a browser, you will be able to see a Hello World! message on the screen.

Image for post

(http://localhost:9000)

As we know, the nil argument to ListenAndServe() call invokes Go to use the [DefaultServeMux](https://golang.org/pkg/net/http/#DefaultServeMux) response multiplexer, which is the default instance of ServeMux structure provided globally by the Go. The HandleFunc() call adds a response handler for a specific route on the multiplexer instance.

The http.ListenAndServe() call uses the Go’s standard HTTP server configuration, however, in the previous lesson, how we can customize a server using [Server](https://golang.org/pkg/net/http/#Server) structure type.

To start an HTTPS server, all we need do is to call ServerAndListenTLS method with some configuration. Just like ServeAndListen method, this method is available on both the http package and the Server structure.

The http.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#ListenAndServeTLS) method uses the Go’s standard server implementation, however, both [Server](https://golang.org/pkg/net/http/#Server) instance and Server.``[ServeAndListenTLS](https://golang.org/pkg/net/http/#Server.ListenAndServeTLS) method can be configured for our needs.

#go-programming-language #go #golang-tutorial #go-programming #golang

Trystan  Doyle

Trystan Doyle

1598640120

Symmetric and Asymmetric Encryption in .NET Core

This post looks at symmetric and asymmetric encryption and how this could be implemented in .NET Core. Symmetric encryption is fast and can encrypt or decrypt large amounts of text, streams or file

#.net #.net core #asp.net core #asymmetric encryption #encryption #symmetric encryption