Golang Crypto Symmetric Key Generate

A basic example of how to perform symmetric key encryption/decryption using AES and Java's cryptography API.
CryptoHelper.java
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.

Golang generating a 32 byte key. I am using this library for sessions. It says that: It is recommended to use an authentication key with 32 or 64 bytes. The encryption key, if set, must be either 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256 modes. Use the generateKey method of the SubtleCrypto interface to generate a new key (for symmetric algorithms) or key pair (for public-key algorithms). Syntax const result = crypto.subtle.generateKey(algorithm, extractable, keyUsages); Parameters. Algorithm is a dictionary object defining the type of key to generate and providing extra algorithm-specific.

commented Mar 8, 2018

Asymmetric Key

Vsphere 5.1 license key generator. Brilliant tutorial, just what I have been looking for

commented Jun 20, 2018

no brilliant tutorial !!

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!
Kindly reply soon

commented Feb 9, 2019

Asymmetric Key Encryption

how do you generate the encryption key

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment