Sunday, September 08, 2024

Installed Perl Crypt Modules

Installed :

Crypt::Random

Crypt::IDEA

Crypt::Twofish

Crypt::TEA

Crypt::Blowfish

Crypt::Vernam


 

Saturday, September 07, 2024

Using One time pads - Step by Step

https://www.ciphermachinesandcryptology.com/en/onetimepad.htm

https://github.com/schizolab/one-time-pad-tutorial

https://www.geeksforgeeks.org/implementation-of-vernam-cipher-or-one-time-pad-algorithm/

https://www.tutorialspoint.com/cryptography/cryptography_one_time_pad_cipher.htm

 

 

 

Thursday, November 03, 2016

Password Based Encryption

Password
Eight or more characters in the ASCII character range 0x32 through 0x126
Salt
A string of bytes added to the password to make it harder to brute force guess.
Iteration Count
The number of times a make-work transformation is applied to the password + salt to slow any automated brute force attacks.
Payload
The information to be securely protected.



PBEParameterSpec Class

For those using the clean room version of JCE 1.2.2 included in Bouncy Castle 155, only the 2 key and 3 key forms of the Triple-DES algorithm will work.

The SHA-1 Secure Hash Algorithm is the only message digest supported by Bouncy Castle 155 for use with the above.



Sunday, October 30, 2016

Symmetric Block Ciphers

So, here is a partial list of symmetric block ciphers available in Bouncy Castle :
  • RC5 has 64 bit block size and 128 bit key size
  • Blowfish has 64 bit block size and 1 to 448 bit key size
  • AES has 128 bit block size and 128, 192, 256 bit key sizes
  • RC6 has 128 bit block size and 128, 192, 256 bit key sizes
  • Serpent has 128 bit block size and 128, 192, 256 bit key sizes
  • Twofish has 128 bit block size and key sizes up to 256 bits



You will certainly need a byte array for a Key, and a SecretKeySpec object to convert this byte array to a form acceptable to the Cipher object that does the actual encryption and decryption of your Input. The Key byte array doesn't need to be the same length as the Cipher block size, but must be a length acceptable to the Cipher. Note the above ciphers all have both a block size and acceptable key sizes.

Any of the above block ciphers without additional processing only securely encrypts a single block. To securely encrypt larger amounts of input, encryption Modes have been developed that use the encrypted results of the current block to additionally secure the following block.

Obviously an initial block must be available to securely encrypt the first block of actual input. This block is called the Initialization Vector (IV). It can be explicitly provided, or derived from the password or from looking ahead in the input.

CBC (Cipher Block Chaining) is a commonly used mode that uses an explicitly provided random byte block as the IV. Nothing has to be communicated to the receiver for decryption, the first block of the decrypted message is discarded and the remainder is the original message. CBC requires the input to be padded if necessary to be modulo 0 block size.



For Symmetric Block Ciphers to work at all, both parties must agree on the Cipher, the Mode, and the Key byte array.

Technically there is no reason that once established any of these need to be changed. However as a matter of policy, since the Key byte array may somehow become compromised, they are often changed on a schedule. Then a secure means must be found to communicate the new key to the other party.



Friday, October 28, 2016

SecretKeySpec Class

The simplest way to generate a secret key is select a block of bytes from an array of bytes.

However you want to do it, generate an array of bytes at least as long as your intended cipher requires. Most ciphers now require 16, 24, or 64 byte blocks as keys.

SecretKeySpec has two constructors which give you a choice of exactly the right sized array of bytes or some block of bytes selected from a larger byte array.
byte[] keyArray1 = new byte[16];
byte[] keyArray2 = new byte[24];
byte[] keyArray3 = new byte[64];
Here are the two SecretKeySpec constructors :
SecretKeySpec(byte[] key, String algorithm)
SecretKeySpec key1 = new SecretKeySpec(keyArray1,"AES");

SecretKeySpec(byte[] key, int offset, int len, String algorithm)
SecretKeySpec key2 = new SecretKeySpec(keyArray3,13,16,"Serpent");


Here is the link to the official Oracle Documentation




Where to Get the JCE and How to Install it

For JDK 1.2 & JDK 1.3, go to the Bouncy Castle web site and download JCE 1.2.2.

For JDK 1.4 and later, the JCE is already included.



For JDK 1.2 the files you want are all named xxx-jdk12-155.jar and xxx-jdk12-155.zip

The Provider pkg is bcprov-jdk12-155.jar

The Open PGP pkg is bcpg-jdk12-155.jar

The Test Class pkg is bctest-jdk12-155.jar



Copy the *.jar files to the jre/lib/ext folder in both the JDK and the JRE



Add Bouncy Castle to the provider list in jre/lib/security for both the JDK and the JRE

Use the next number available.

security.provider.5=org.bouncycastle.jce.provider.BouncyCastleProvider



Look around in the above compressed files for a program called SimpleProviderTest.

Compile and run it to get : BC provider is installed  msg.



Thursday, October 27, 2016

Creating a SecureRandom Object

import java.security.SecureRandom;

SecureRandom random = new SecureRandom();
byte[] bytes = new byte[20];
random.nextBytes(bytes);

If you don't have a hardware random number generator built into your CPU, or if you don't trust it, then the random.nextBytes() method will probably block until it can find enough entropy to generate the bytes you need. In my experience it only blocks on the first request, and later requests return quickly.


Links

Synopsis

Terse Systems

Examples



Wednesday, October 26, 2016

Using the Cipher Class for File Encryption



{snip}

    fileChooser1.setFileFilter(new BBSFilter());
    if(fileChooser1.APPROVE_OPTION != fileChooser1.showOpenDialog(this)) return;
    clearFileName = fileChooser1.getSelectedFile().getPath();
    File clearFile = new File(clearFileName);
    int prefix = clearFileName.length()-4;
    cryptoFileName = (clearFileName.substring(0,prefix)).concat(".XXX");
    File cryptoFile = new File(cryptoFileName);
    plainFileName = (clearFileName.substring(0,prefix)).concat(".TXT");
    File plainFile = new File(plainFileName);

    byte[] keyBytes = new byte[] {
           0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
           0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 };
    byte[] ivBytes = new byte[] {
           0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03,
           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };

    try{

      SecretKeySpec key = new SecretKeySpec(keyBytes,"Twofish");
      IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
      Cipher cipher = Cipher.getInstance("Twofish/CTR/NoPadding","BC");

      // encryption pass
      cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
      FileInputStream fis = new FileInputStream(clearFile);
      BufferedInputStream bis = new BufferedInputStream(fis,DSKBLOK);
      CipherInputStream cis = new CipherInputStream(bis,cipher);
      FileOutputStream fos = new FileOutputStream(cryptoFile);
      BufferedOutputStream bos = new BufferedOutputStream(fos,DSKBLOK);
      int ch; while((ch = cis.read()) >= 0) bos.write(ch);
      bos.flush(); bos.close();

      // decryption pass
      cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
      fis = new FileInputStream(cryptoFileName);
      bis = new BufferedInputStream(fis,DSKBLOK);
      cis = new CipherInputStream(bis,cipher);
      fos = new FileOutputStream(plainFile);
      bos = new BufferedOutputStream(fos,DSKBLOK);
      while ((ch = cis.read()) >= 0) bos.write(ch);
      bos.flush(); bos.close();
    } catch(Exception ee) { statusBar.setText("\n   Oooops!\n"); }


{snip}

Saturday, January 12, 2013

Tiny Encryption Algorithm (TEA)

TEA is an algorithm with a couple weaknesses. Still, the algorithm is a block cipher notable for its simplicity of description and a C implementation of just a few lines of code.

It was designed by David Wheeler and Roger Needham of the Cambridge Computer Laboratory, first presented at the Fast Software Encryption workshop in Leuven in 1994, and first published in the proceedings of that workshop.

It's an ideal algorithm for testing new frameworks and modes of operation. The first caveat is to make sure your installation produces the proper outputs to reference inputs. Then you can build your framework around it confident that if your output breaks, it's the framework at fault.

The authors suggest in its reference notes that anything you can do with or to DES will work just as well with TEA.

TEA operates on two 32-bit unsigned integers, uses a 128-bit key, and chunks out similar 32-bit unsigned integers. Conveniently you can feed it pointers to character strings of this size and it will cast them to longs.

The original reference implementation accepts three pointers (input,output, key) which makes the necessary CBC mode adapters easier to attach.

Standard usage calls for 64 Feistel rounds which is 32 cycles of the algorithm.
Make n = 32.

The effective key size is 126 bits which is plenty for keeping my diary secure.

Here is the ANSI C implementation I use :
void encipher(unsigned long *const v,unsigned long *const w,
   const unsigned long *const k){

   register unsigned long 
     y=v[0],z=v[1],n=32,
     sum=0,delta=0x9E3779B9,
     a=k[0],b=k[1],c=k[2],d=k[3];

   while(n-->0) {
      sum += delta;
      y += (z << 4)+a ^ z+sum ^ (z >> 5)+b;
      z += (y << 4)+c ^ y+sum ^ (y >> 5)+d;
   }
   w[0]=y; w[1]=z;
}

void decipher(unsigned long *const v,unsigned long *const w,
   const unsigned long *const k){

   register unsigned long 
     y=v[0],z=v[1],n=32,
     sum=0xC6EF3720,delta=0x9E3779B9,
     a=k[0],b=k[1], c=k[2],d=k[3];

   while(n-->0) {
      z -= (y << 4)+c ^ y+sum ^ (y >> 5)+d;
      y -= (z << 4)+a ^ z+sum ^ (z >> 5)+b;
      sum -= delta;
   }
   w[0]=y; w[1]=z;
}


Test Vectors :

Plain Text :

Cipher Key :

Cipher Text :


Tuesday, January 08, 2013

Secure Hash Algorithms (SHA-1 and SHA-256)

Secure Hash Algorithms (SHA-1 and SHA-256)

Secure Hash Algorithms, a form of symmetric encryption, is also included in VIA processors based on the ‘Esther' core.

Implementing the SHA-1 and SHA-256 variants of secure hash, the VIA Padlock SHA engine can deliver real-time data encryption peaking at 5 gigabits per second.

This algorithm is again endorsed and used at all levels of the U.S. Government and operates by compressing the data needed and encrypting it into message digests. Each message digest is created so that the information inside is computationally infeasible to be read by an outside party.

Table: An example of the message digests produced by SHA-1:

Original String SHA-1 Hash Digest
Hello World z7R8yBtZz0+eqead7UEYzPvVFjw=
VB L1SHP0uzGbMUpT4z0zlAdEzfPE=
Vb e0cnhoZRmuoC/Ed51RrW71x1CDw=
Vb e3Pa1F6tMmhPGUfGg1nrfdV31+1=
VB gzt6my3YlrzJiTiucvqBTgM6LtM=

By studying the contents of each SHA-1 secure hash digest it becomes immediately evident that a change of character case on the same word produces a completely different encrypted message.

In fact a change of just one bit within a message causes a completely different secure hash message digest to be created. This allows easy detection of message manipulation, as a message cannot be decrypted, altered even slightly and re-encrypted without producing an entirely different message digest. This is why most governments and organizations in the world now use a implementation of the secure hash algorithm.

SHA-256 is a more secure version of SHA-1, using a 256bit hash algorithm (SHA-1 uses a 160 bit algorithm), and is currently considered completely unbreakable.

Despite this advantage, SHA-1 and SHA-256 secure hash algorithms suffer from one drawback: they all require enormous consumption of processor cycles in the computation of digests, especially when a high throughput is required.

However, VIA has solved this problem by integrating the computation of secure hash algorithms SHA-1 and SHA-256 directly onto the processor die.

By incorporating secure hash into the VIA PadLock Security Engine, VIA has effectively shifted the computational process of large-scale number crunching away from the main processor to the PadLock engine.

Thus, a developer can include real-time secure hash encryption in their applications without worrying that the process will significantly affect other applications or processes running concurrently.


RSA Algorithms and VIA Padlock Montgomery Multiplier

RSA Algorithms and VIA Padlock Montgomery Multiplier

VIA processors featuring the ‘Esther' core include a powerful Montgomery Multiplier, supporting key sizes up to 32K in length, used to accelerate the computational throughput of public key cryptography, such as RSA algorithms.

Used the by U.S. Government, RSA Algorithms are an encryption mechanism based on the asymmetric encryption method, or public key encryption. Public key encryption allows transmission of secure information across unsecured mediums.

Encryption under this system requires that the sender and receiver both have two keys; a private key and a public key. To initiate secure communication under a RSA system the sender asks for the receiver's public key. This key can either be attained by a direct request to the receiver or by looking up a central database of keys. Using the receiver's public key the sender encrypts the information to be sent across the unsecured medium.

When the receiver gets the communiqué they use their private key to decrypt the secured information.

To send information back to the original sender the receiver uses the sender's public key to encrypt the response.

This public key-private key process provides ultra-secure one-to-one communication across unsecured mediums.

VIA's PadLock Montgomery Multiplier expedites this process while at the same time reducing significantly the processor load during the key construction and data encryption by eliminating the processor taxing exercise of the large number division required to perform this type of encryption.



Public Key Cryptography

wiki


GNU Privacy Guard

Wiki


Disk Encryption

Wiki Link


AES - Advanced Encryption Standard


Symmetric-Key Algorithm : Vintage - 2001

AES is based on a design principle known as a substitution-permutation network, and is fast in both software and hardware. AES does not use a Feistel network.

High speed and low RAM requirements were criteria of the AES selection process. Thus AES performs well on a wide variety of hardware, from 8-bit smart cards to high-performance computers.

The design and strength of all key lengths of the AES algorithm (i.e., 128, 192 and 256) are sufficient to protect classified information up to the SECRET level. TOP SECRET information will require use of either the 192 or 256 key lengths.

AES has a block size of 128 bits (16 bytes) and three available key lengths of 128, 192, 256 bits.

Give the algorithm a key, and then feed it 16 byte blocks and get back 16 byte blocks.

It works the same way for either encrypt or decrypt.

 

Thursday, February 10, 2011

Javax Hierarchy of Crypto Classes

CipherSpi => Cipher

KeyGeneratorSPi => KeyGenerator

Cipher

CipherInputStream

CipherOutputStream

SecretKey


SecureRandom
Key


MessageDigestSpi => MessageDigest