Golang Crypto Symmetric Key Generate
In cryptography, Galois/Counter Mode (GCM) is the mode of operation for symmetric key cryptographic block ciphers widely adopted thanks to its performance. An operation is the authenticated encryption algorithm designed to provide both data integrity and confidentiality. GCM is defined for block ciphers with the block size of 128 bits. Nov 13, 2018 Go Lang RSA Cryptography. Posted on Tuesday November 13, 2018. Of data, In order to encrypt reasonable amounts of data a hybrid scheme is commonly used: RSA is used to encrypt a key for a symmetric primitive like AES. The following example code will generate RSA keys of size 2048 and then perform RSA Sign/verify using the PKCS1. Jul 30, 2016 If the salt is of 0 length, it generates a new salt, and returns the expanded key and salt as byte arrays. A salt should only be provided as part of a decryption or verification process. When using ExpandKey to create a new key, let ExpandKey generate the salt. This is to lessen the risk of a weak or non-unique salt being used.
Jan 23, 2015 Symmetric and Asymmetric Encryption with Javascript and Go. You could encrypt the data using an asymmetric encryption — i.e. A public rsa key to encrypt the data, store the encrypted data. Fernet (symmetric encryption)¶ Fernet guarantees that a message encrypted using it cannot be manipulated or read without the key. Fernet is an implementation of symmetric (also known as “secret key”) authenticated cryptography. Fernet also has support for implementing key rotation via MultiFernet. Class cryptography.fernet.Fernet (key) source ¶. The Go language supports symmetric encryption algorithms in its crypto package. Do not use anything except AES in GCM mode if you don't know what you're doing! Crypto/aes package: AES (Advanced Encryption Standard), also known as Rijndael encryption method, is used by the U.S. Federal government as a block encryption standard. HMAC can work with any existing message digest algorithms (hash functions). It considers the message digest produced by the embedded hash function as a black box. It then uses the shared symmetric key to encrypt the message digest, thus, producing the final output, that is, MAC.
importjava.security.Key; |
importjava.security.SecureRandom; |
importjavax.crypto.Cipher; |
importjavax.crypto.KeyGenerator; |
importjavax.crypto.SecretKey; |
importjavax.crypto.spec.IvParameterSpec; |
importorg.apache.commons.codec.binary.Base64; |
publicclassCryptoHelper { |
publicstaticvoidmain( String [] args ) throwsException { |
CryptoHelper crypto =newCryptoHelper(); |
String plaintext ='This is a good secret.'; |
System.out.println( plaintext ); |
String ciphertext = crypto.encrypt( plaintext ); |
System.out.println( ciphertext ); |
String decrypted = crypto.decrypt( ciphertext ); |
System.out.println( decrypted ); |
} |
publicStringencrypt( Stringplaintext ) throwsException { |
return encrypt( generateIV(), plaintext ); |
} |
publicStringencrypt( byte [] iv, Stringplaintext ) throwsException { |
byte [] decrypted = plaintext.getBytes(); |
byte [] encrypted = encrypt( iv, decrypted ); |
StringBuilder ciphertext =newStringBuilder(); |
ciphertext.append( Base64.encodeBase64String( iv ) ); |
ciphertext.append( ':' ); |
ciphertext.append( Base64.encodeBase64String( encrypted ) ); |
return ciphertext.toString(); |
} |
publicStringdecrypt( Stringciphertext ) throwsException { |
String [] parts = ciphertext.split( ':' ); |
byte [] iv =Base64.decodeBase64( parts[0] ); |
byte [] encrypted =Base64.decodeBase64( parts[1] ); |
byte [] decrypted = decrypt( iv, encrypted ); |
returnnewString( decrypted ); |
} |
privateKey key; |
publicCryptoHelper( Keykey ) { |
this.key = key; |
} |
publicCryptoHelper() throwsException { |
this( generateSymmetricKey() ); |
} |
publicKeygetKey() { |
return key; |
} |
publicvoidsetKey( Keykey ) { |
this.key = key; |
} |
publicstaticbyte [] generateIV() { |
SecureRandom random =newSecureRandom(); |
byte [] iv =newbyte [16]; |
random.nextBytes( iv ); |
return iv; |
} |
publicstaticKeygenerateSymmetricKey() throwsException { |
KeyGenerator generator =KeyGenerator.getInstance( 'AES' ); |
SecretKey key = generator.generateKey(); |
return key; |
} |
publicbyte [] encrypt( byte [] iv, byte [] plaintext ) throwsException { |
Cipher cipher =Cipher.getInstance( key.getAlgorithm() +'/CBC/PKCS5Padding' ); |
cipher.init( Cipher.ENCRYPT_MODE, key, newIvParameterSpec( iv ) ); |
return cipher.doFinal( plaintext ); |
} |
publicbyte [] decrypt( byte [] iv, byte [] ciphertext ) throwsException { |
Cipher cipher =Cipher.getInstance( key.getAlgorithm() +'/CBC/PKCS5Padding' ); |
cipher.init( Cipher.DECRYPT_MODE, key, newIvParameterSpec( iv ) ); |
return cipher.doFinal( ciphertext ); |
} |
} |
commented Dec 25, 2017
Thank you! Thanks to you, I could do this. |
commented Mar 8, 2018
Tekken 7 cd key generator online. Brilliant tutorial, just what I have been looking for |
commented Jun 20, 2018
no brilliant tutorial !! |
Golang Crypto Symmetric Key Generate Code
commented Sep 8, 2018
is this program an example of AES NI implementation? as there are no initialization vectors in NI .according to my understanding! |
commented Feb 9, 2019
how do you generate the encryption key |

Golang Go Generate
// This shows an example of how to generate a SSH RSA Private/Public key pair and save it locally |
package main |
import ( |
'crypto/rand' |
'crypto/rsa' |
'crypto/x509' |
'encoding/pem' |
'golang.org/x/crypto/ssh' |
'io/ioutil' |
'log' |
) |
func main() { |
savePrivateFileTo := './id_rsa_test' |
savePublicFileTo := './id_rsa_test.pub' |
bitSize := 4096 |
privateKey, err := generatePrivateKey(bitSize) |
if err != nil { |
log.Fatal(err.Error()) |
} |
publicKeyBytes, err := generatePublicKey(&privateKey.PublicKey) |
if err != nil { |
log.Fatal(err.Error()) |
} |
privateKeyBytes := encodePrivateKeyToPEM(privateKey) |
err = writeKeyToFile(privateKeyBytes, savePrivateFileTo) |
if err != nil { |
log.Fatal(err.Error()) |
} |
err = writeKeyToFile([]byte(publicKeyBytes), savePublicFileTo) |
if err != nil { |
log.Fatal(err.Error()) |
} |
} |
// generatePrivateKey creates a RSA Private Key of specified byte size |
func generatePrivateKey(bitSize int) (*rsa.PrivateKey, error) { |
// Private Key generation |
privateKey, err := rsa.GenerateKey(rand.Reader, bitSize) |
if err != nil { |
return nil, err |
} |
// Validate Private Key |
err = privateKey.Validate() |
if err != nil { |
return nil, err |
} |
log.Println('Private Key generated') |
return privateKey, nil |
} |
// encodePrivateKeyToPEM encodes Private Key from RSA to PEM format |
func encodePrivateKeyToPEM(privateKey *rsa.PrivateKey) []byte { |
// Get ASN.1 DER format |
privDER := x509.MarshalPKCS1PrivateKey(privateKey) |
// pem.Block |
privBlock := pem.Block{ |
Type: 'RSA PRIVATE KEY', |
Headers: nil, |
Bytes: privDER, |
} |
// Private key in PEM format |
privatePEM := pem.EncodeToMemory(&privBlock) |
return privatePEM |
} |
// generatePublicKey take a rsa.PublicKey and return bytes suitable for writing to .pub file |
// returns in the format 'ssh-rsa ..' |
func generatePublicKey(privatekey *rsa.PublicKey) ([]byte, error) { |
publicRsaKey, err := ssh.NewPublicKey(privatekey) |
if err != nil { |
return nil, err |
} |
pubKeyBytes := ssh.MarshalAuthorizedKey(publicRsaKey) |
log.Println('Public key generated') |
return pubKeyBytes, nil |
} |
// writePemToFile writes keys to a file |
func writeKeyToFile(keyBytes []byte, saveFileTo string) error { |
err := ioutil.WriteFile(saveFileTo, keyBytes, 0600) |
if err != nil { |
return err |
} |
log.Printf('Key saved to: %s', saveFileTo) |
return nil |
} |