/cryptolib

CryptoLib is encryption utility that provides symmetric key crypto operations for Block Ciphers like CBC, GCM, ECB, CTR. It provides MAC sign and verify operations. Also, it provides asymmetric key operations for EC signature and RSA signature and encryption operations for different padding modes and digests like OAEP, PSS, PKCS1 etc. It implements bouncy castle and SunJCE crypto providers and supports the FIPS 140-2 standards.

Primary LanguageJava

CryptoLib

The aim of cryptolib is to provide a modularized way of supporting crypto implementation of various crypto algorithms block modes, padding modes, digests, and EcCurves for AES, 3DES, RSA, EC, and HMAC. It provides interfaces and implementation for encryption, signature, and HMAC. It supports both FIPS and non-FIPS standards and provides the way to use SunJCE and Bouncy Castle crypto providers. It will also check if certain padding mode, block mode, EcCurve, or a digest is supported for a cipher operation or not.

Clone

git clone https://github.com/irfanazam1/cryptolib.git

Build

  • mvn install
  • mvn clean package install
  • mvn clean package install -DskipTests
  • gradle clean build
  • ./gradlew clean build or gradlew.bat clean build

Including in the project

  • Maven

       <dependency>
          <groupId>cryptolib</groupId>
          <artifactId>cryptolib</artifactId>
          <version>1.0.0</version>
      </dependency>
    
  • Gradle

    implementation 'cryptolib:cryptolib:1.0.0'

Usage

    //Create key Authorizations
     KeyAuthorizations keyAuthorizations = new KeyAuthorizations (128, Algorithm.AES, BlockMode.CBC, PaddingMode.NO_PADDING, Purpose.ENCRYPT);
     //Create the material (key and IV)
     byte[] key = new byte[16];
     byte[] iv = new byte[16];
     SecureRandom random = new SecureRandom();
     random.nextBytes(key);
     random.nextBytes(iv);
     //Setup the key symmetric key
     SymmetricKey symmetricKey = new SymmetricKey();
     symmetricKey.setEncodedKey(key);
     symmetricKey.setIv(iv);
     keyAuthorizations.setKey(symmetricKey);
     //Set the provider
     keyAuthorizations.setProvider(new BouncyCastleProvider());
     //Grab the cipher suite for the authorized algorithm
     CipherSuite cipherSuite = CipherSuiteFactory.getCipherSuite(keyAuthorizations);
     //Setup the data.
     byte[] plainBytes = "TextToEncryption".getBytes(Charset.defaultCharset());
     //Encrypt
     byte[] encryptedBytes = cipherSuite.encrypt(plainBytes);
     //Decrypt.
     keyAuthorizations.setPurpose(Purpose.DECRYPT);
     cipherSuite = CipherSuiteFactory.getCipherSuite(keyAuthorizations);
     byte[] decryptedBytes = cipherSuite.decrypt(encryptedBytes, plainBytes.length);
     System.out.println(Arrays.equals(decryptedBytes, plainBytes));