Code Samples

This topic demonstrates the use of DYADIC as the JCE-Provider in various crypto-cases.

Introduction

To start using Dyadic-based realization of crypto-primitives, specify “DYADIC” as your default JCE provider. Consult JCE Documentation for the use of KeyStore, KeyGenerator, and Cipher.

The following API of JCE creates Dyadic-based Cipher object that implements all standard RSA, AES, and ECC crypto-methods (transformations).

Cipher getInstance(String transformation, "DYADIC")

For example:

Cipher cipher1 = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING", "DYADIC");
Cipher cipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "DYADIC")
KeyStore keyStore = KeyStore.getInstance("PKCS11", "DYADIC");
KeyPairGenerator gen1 = KeyPairGenerator.getInstance("RSA", "DYADIC");
KeyPairGenerator gen2 = KeyPairGenerator.getInstance("EC", "DYADIC");
Signature sign = Signature.getInstance("SHA256WithRSA", "DYADIC");

Note: Dyadic supports KeyStore of type PKCS11.

RSA - Signing and Encryption

The following code sample demonstrates RSA for signing and encryption:

byte[] testData = new byte[]{1, 2, 3, 4, 5};

KeyStore keyStore = KeyStore.getInstance("PKCS11", "DYADIC");
keyStore.load(null);

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "DYADIC");
gen.initialize(2048);
KeyPair keyPair = gen.generateKeyPair();

keyStore.setEntry("RSA test", new DYCryptoProvider.KeyEntry(keyPair.getPrivate()), null);
PrivateKey prvKey = (PrivateKey) keyStore.getKey("RSA test", null);

Signature sign = Signature.getInstance("SHA256WithRSA", "DYADIC");
sign.initSign(prvKey);
sign.update(testData);
byte[] signature = sign.sign();

sign = Signature.getInstance("SHA256WithRSA");
RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) prvKey;
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(rsa.getModulus(), rsa.getPublicExponent()));
sign.initVerify(pubKey);
sign.update(testData);
boolean ok = sign.verify(signature);

Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encrypted = cipher.doFinal(testData);
cipher = Cipher.getInstance("RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING", "DYADIC");
cipher.init(Cipher.DECRYPT_MODE, prvKey);
byte[] decrypted = cipher.doFinal(encrypted);
ok = Arrays.equals(decrypted, testData);

ECDSA - Key Generation

The following code sample demonstrates ECDSA key generation and signature:

KeyStore keyStore = KeyStore.getInstance("PKCS11", "DYADIC");
keyStore.load(null);

KeyPairGenerator gen = KeyPairGenerator.getInstance("EC", "DYADIC");
gen.initialize(new ECGenParameterSpec("secp256r1"));
KeyPair keyPair = gen.generateKeyPair();

keyStore.setEntry("ECDSA test",
        new DYCryptoProvider.KeyEntry(keyPair.getPrivate()), null);
PrivateKey prvKey = (PrivateKey) keyStore.getKey("ECDSA test", null);

byte[] testData = new byte[] { 1, 2, 3, 4, 5 };

Signature sign = Signature.getInstance("SHA256WithECDSA", "DYADIC");
sign.initSign(prvKey);
sign.update(testData);
byte[] signature = sign.sign();

sign = Signature.getInstance("SHA256WithECDSA");
sign.initVerify(keyPair.getPublic());
sign.update(testData);
boolean ok = sign.verify(signature);

AES - Key Generation and Encryption

The following code sample demonstrates AES key generation and usage:

KeyStore keyStore = KeyStore.getInstance("PKCS11", "DYADIC");
keyStore.load(null);

KeyGenerator gen = KeyGenerator.getInstance("AES", "DYADIC");
gen.init(256);
SecretKey secretKey = (SecretKey) gen.generateKey();
keyStore.setKeyEntry("AES test", secretKey, null, null);

SecureRandom secureRandom = new SecureRandom();
byte[] data = new byte[64];
byte[] iv = new byte[12];
byte[] AD = new byte[12];
secureRandom.nextBytes(data);
secureRandom.nextBytes(data);
secureRandom.nextBytes(iv);
secureRandom.nextBytes(AD);
CcmParameterSpec ccmParameterSpec = new CcmParameterSpec(iv, 12, AD);
Cipher encryptCipherEncrypt = Cipher.getInstance("AES/CCM/NoPadding");
encryptCipherEncrypt.init(Cipher.ENCRYPT_MODE, secretKey, ccmParameterSpec);
encryptCipherEncrypt.update(data, 0, data.length / 2);
encryptCipherEncrypt.update(data, data.length / 2, data.length / 2);
byte[] enc = encryptCipherEncrypt.doFinal();
Cipher encryptCipherDecrypt = Cipher.getInstance("AES/CCM/NoPadding");
encryptCipherDecrypt.init(Cipher.DECRYPT_MODE, secretKey, ccmParameterSpec);
byte[] dec = encryptCipherDecrypt.doFinal(enc);


Complete Test Application

See the complete application in https://github.com/DyadicSec/dyadic-sample.

Create a pull request or raise an issue on the source for this page in GitHub