This example demonstrates using the ecies compatibility package for ECIES (Elliptic Curve Integrated Encryption Scheme) to encrypt data for oneself (i.e., encrypting with a public key derived from a private key, and then decrypting with that same private key). The encrypted message is base64 encoded and prefixed with "BIE1".
The ecies_single example showcases:
- Defining a private key.
- Encrypting a string message ("hello world") using
ecies.EncryptSingle. This function takes the message and the user's private key. It internally derives the corresponding public key for encryption. The output is a base64 encoded string with the "BIE1" prefix. - Decrypting the base64 encoded ciphertext using
ecies.DecryptSingle, which takes the encrypted string and the same private key used for encryption.
This is useful for encrypting data that only the owner of the private key should be able to decrypt, such as storing sensitive information.
// Define a private key
myPrivateKey, _ := ec.PrivateKeyFromWif("L211enC224G1kV8pyyq7bjVd9SxZebnRYEzzM3i7ZHCc1c5E7dQu")
// Encrypt the message using the private key (public key is derived internally)
encryptedData, _ := ecies.EncryptSingle("hello world", myPrivateKey)
fmt.Println(encryptedData) // Prints BIE1-prefixed base64 string
// Decrypt the message using the same private key
decryptedData, _ := ecies.DecryptSingle(encryptedData, myPrivateKey)
fmt.Printf("Decrypted data: %s\n", decryptedData)This section shows:
ecies.EncryptSingleencrypts the data using the public key associated withmyPrivateKey.ecies.DecryptSingledecrypts the data usingmyPrivateKey.
To run this example:
go run ecies_single.goThe output will be the "BIE1" prefixed base64 encoded encrypted string, followed by the successfully decrypted "hello world" message.
Note:
- The WIF string for the private key is hardcoded for simplicity. In real applications, keys should be managed securely.
- The "BIE1" prefix indicates a specific ECIES message format.
To encrypt data for yourself and later decrypt it:
For Encryption:
- Obtain your
*ec.PrivateKey. - Call
ecies.EncryptSingle(plaintextMessageString, yourPrivateKey). Store the resulting BIE1-prefixed base64 string.
For Decryption:
- Obtain your
*ec.PrivateKey(the same one used for encryption). - Call
ecies.DecryptSingle(bie1EncryptedString, yourPrivateKey).
This pattern is suitable for encrypting data at rest where the same entity controls both encryption and decryption.
For more information, see: