Udemy: Hands-On Cryptography with JavaRussell Bateman |
Erik Costlow, author and presenter.
This whole presentation gravitates toward the development of Java cryptological code instead of what I needed which was a detailed account of keys, certificates, Java key- and trust stores, openssl, keytool and what's inside. This is not unappreciated, but it's not primarily what I needed right now. It's just that it was the only course I could find that even touched upon Java key- and trust stores, but it did not really even mention the trust store let alone its relationship with the keystore. And, you have to buy before you can learn at the detailed level what's in a course.
Hence, I have added in here emerging notes in the appendices about what I really needed to learn instead of or in addition to what was taught.
...because I skipped over a lot of the early bits of the course, not needing the elementary introduction nor wishing to spend time conveying them in here.
SecureRandom sr = new SecureRandom(); byte[] key = new byte[ 16 ]; // 128-bit key (AES) byte[] initializationVector = new byte[ 16 ]; sr.nextBytes( key ); System.out.println( " Random key=" + bytesToHex( key ) ); System.out.println( "Initialization vector=" + bytesToHex( initializationVector ) );
final String password = "12345";
final String SALT = "[email protected]";
final int keylength = 512;
final int iterations = 32; // (number of times through the SALT)
PBEKeySpec keyspec = new PBEKeySpec( password.toCharArray(), SALT.getBytes(), iterations, keylength );
SecretKeyFactory factory = SecretKeyFactory.getInstance( "PBKDF2WithHmacSHA256" );
byte hashed = factory.generateSecret( keyspec ).getEncoded();
System.out.println( "The SHA-256 value salted with PBKDF2 is " + hashed );
logger( "Decrypting text with password: " + password );
final KeyPairGenerator generator = KeyPairGenerator.getInstance( "RSA" ).initialize( 2048 ); final KeyPair keypair = generator.generateKeyPair(); final PublicKey publickey = keypair.getPublic(); final PrivateKey privatekey = keypair.getPrivate(); System.out.println( " Public key is " + publickey ); System.out.println( "Hex encoding is " + Util.bytesToHex( publickey.getEncoded() ) ); System.out.println( " Private key is " + privatekey );
/* Bouncy Castle Java demo */
/* Java keytool demo */
/* Java key generator demo */
/* Basic asymmetric encryption demo */
$ keytool -importcert
keytool will perform duplicate-certificate avoidance
for you.
/* Java demo certificate chains */
SecureRandom random = new SecureRandom(); byte[] key = new byte[ 16 ]; // 128-bit byte[] vector = new byte[ 16 ]; random.nextBytes( key ); System.out.println( " Random key=" + Util.bytesToHex( key ) ); System.out.println( "Initialization vector=" + Util.bytesToHex( vector ) ); IvParameterSpec initializationVector = new IvParameterSpec( vector ); SecretKeySpec keyspec = new SecretKeySpec( key, "AES" ); Cipher cipher = Cipher.getInstance( "AES/CBC/PKCS5PADDING" ); cipher.init( Cipher.ENCRYPT_MODE, keyspec, initializationVector ); final String TEMPORARY_DIRECTORY = "packt-crypto"; final String RESOURCE_DIRECTORY = "1 - Encrypting and Decrypting files.pptx"; final String ENCRYPTED_DIRECTORY = RESOURCE_DIRECTORY + ".encrypted"; final Path directory = Files.createTempDirectory( TEMPORARY_DIRECTORY ); final Path path = directory.resolve( ENCRYPTED_DIRECTORY ); try( InputStream inputStream = FileEncryptor.class.getResourcesAsStream( DIRECTORY ), OutputStream __encrypted__ = Files.newOutputStream( path ) CipherOutputStream cipherOutputStream = new CipherOutputStream( __encrypted__, cipher ) ) { final byte[] bytes = new byte[ 1024 ]; for( int length = inputStream.read( bytes ); length != EOF; length = inputStream.read( bytes ) ) cipherOutputStream.write( bytes, 0, length ); } catch( IOException e ) { logger.warn( "Unable to encrypt", e ); } logger.info( "Encryption finished; saved at ", path ); ...decrypt, etc.
See Appendices here.