Java's symmetric encryption techniques
Symmetric Encryption uses
a single key to encrypt and decrypt a message . This type of encryption is classified as either stream ciphers or block ciphers. More details on these algorithms can be found Athttps://en.wikipedia.org/ Wiki/symmetric-key_algorithm symmetric algorithms that is supported by Java include the following ones where The key size in bits are enclosed in parentheses:
AES (+)
DES ()
Desede (168)
HmacSHA1
Hm acSHA256
Varying lengths of data may encrypted. Block cipher algorithms is used to handle large blocks of data. There is several block cipher modes of operations, as listed next. We will not detail how these modes work here, but additional information can be found At https://en.wikipedia.org/wiki/block_cipher_mode_of_operation:
ECB
CBC
CFB
OFB
PCBC
Before we can encrypt or decrypt data, we need a key.
Generating a keya common to generating a key is using the Keygenerator class.
There is no public constructors for the class but an overloaded GetInstance method would return a Keygenerator instance. The following example uses the AES algorithm with the default provider. Other versions of this method allow selection of the provider:
Keygenerator keygenerator = keygenerator.getinstance ("AES");
The GenerateKey method returns an instance of a object that implements the Secretkey interface, is shown next. This is the key, which is used to support symmetric encryption and decryption:
secretkey secretkey
= Keygenerator.generatekey ();
with a key, we can now encrypt data.
encrypting text Using a symmetric key We'll use the following encrypt method in later sections. This method is passed the text
To encrypt and a secret key. The term
plain text was frequently used to refer to theunencrypted data.the Cipher class provides the framework for the encryption process.
The GetInstance method returns an instance of the class where the-the AES algorithm is used. The Cipher instance is initialized for encryption using Cipher.encrypt_mode as the first argument, and the secret key as T He second argument. The Dofinal method encrypts the plain text byte array and returns an encrypted byte array. The Base64 class ' s Getencoder returns an encoder that encodes the encrypted bytes.
Package Com.doctor.ch08;import Java.nio.charset.standardcharsets;import Java.security.invalidkeyexception;import Java.security.nosuchalgorithmexception;import Java.util.base64;import Javax.crypto.badpaddingexception;import Javax.crypto.cipher;import Javax.crypto.illegalblocksizeexception;import Javax.crypto.keygenerator;import Javax.crypto.nosuchpaddingexception;import Javax.crypto.secretkey;import javax.crypto.spec.secretkeyspec;/** * Symmetric encryption Techniques * * @author sdcuike * * Created on April 16, 2016 morning 11:18:29 */public class Symmetri cencryptiontechniques {public static void main (string[] args) throws Throwable {String Base64key = Generatebas E64key (Aes_algorithm); System.out.println (Base64key); String plaintext = "Hello Doctor?"; String encrypttobase64string = encrypttobase64string (plaintext, base64key); System.out.println (encrypttobase64string); System.out.println (Decrpt (encrypttobase64string, Base64key)); } sTatic final String aes_algorithm = "AES"; static string encrypttobase64string (string plaintext, String base64key) throws NoSuchAlgorithmException, Nosuchpaddingexception, InvalidKeyException, Illegalblocksizeexception, badpaddingexception {SecretKey SecretKey = GetKey (Base64key); Cipher Cipher = cipher.getinstance (aes_algorithm); Cipher.init (Cipher.encrypt_mode, Secretkey); byte[] dofinal = cipher.dofinal (Plaintext.getbytes (standardcharsets.utf_8)); Return Base64.getencoder (). encodetostring (dofinal); } static string Decrpt (String encryptedbase64string, String base64key) throws NoSuchAlgorithmException, Invalidkeyexcep tion, illegalblocksizeexception, badpaddingexception, nosuchpaddingexception {secretkey SecretKey = GetKey (Base64K EY); Cipher Cipher = cipher.getinstance (aes_algorithm); Cipher.init (Cipher.decrypt_mode, Secretkey); Byte[] decode = Base64.getdecoder (). Decode (encryptedbase64string); Byte[] DofinAl = Cipher.dofinal (decode); return new String (dofinal, standardcharsets.utf_8); } static string Generatebase64key (String encryptionalgorithm) throws NoSuchAlgorithmException {Keygenerator Gen Erator = Keygenerator.getinstance (encryptionalgorithm); Secretkey Secretkey = Generator.generatekey (); byte[] encoded = secretkey.getencoded (); Return Base64.getencoder (). encodetostring (encoded); } static Secretkey GetKey (String base64key) throws NoSuchAlgorithmException {return new Secretkeyspec (base64.ge Tdecoder (). Decode (Base64key), aes_algorithm); }}
Idiyannh70kdgaw4fjn25g==miz3vpmi07ak37s9ixl2lw==hello doctor?
Reading notes: Learning Network programming with Java
Copyright? Packt Publishing
First Published:december 2015
Production reference:1141215
Published by Packt Publishing Ltd.
Livery Place
Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-547-1
www.packtpub.com
Java's symmetric encryption techniques