0% found this document useful (0 votes)
345 views5 pages

Package Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import

The document contains code for encrypting and decrypting data using elliptic curve cryptography (ECC) and the Bouncy Castle cryptography library. It defines an ASymCrypto class with methods to encrypt and decrypt byte arrays using EC192 encryption with AES. The constructor initializes the asymmetric block cipher, domain parameters, and public/private key parameters from EC keys. The encrypt and decrypt methods pass the input byte array through the cipher to encrypt or decrypt it using the public or private key respectively.

Uploaded by

Tayoub Walid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
345 views5 pages

Package Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import Import

The document contains code for encrypting and decrypting data using elliptic curve cryptography (ECC) and the Bouncy Castle cryptography library. It defines an ASymCrypto class with methods to encrypt and decrypt byte arrays using EC192 encryption with AES. The constructor initializes the asymmetric block cipher, domain parameters, and public/private key parameters from EC keys. The encrypt and decrypt methods pass the input byte array through the cipher to encrypt or decrypt it using the public or private key respectively.

Uploaded by

Tayoub Walid
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

package com.acc; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.

Provider; import java.security.PublicKey; import java.security.SecureRandom; import java.security.Security; import java.security.Signature; import java.security.spec.ECGenParameterSpec; import java.security.spec.ECParameterSpec; import java.security.spec.EllipticCurve; import import import import javax.crypto.Cipher; javax.crypto.CipherInputStream; javax.crypto.CipherOutputStream; javax.crypto.spec.DESKeySpec;

public class TestECC { public static void main(String args[]) { try { Provider p[] = Security.getProviders(); Provider p1 = Security.getProvider("SunEC"); System.out.println(p1.getName()); KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", "SunEC"); System.out.println(kpg.getAlgorithm()); Cipher cipher = Cipher.getInstance("DES"); System.out.println("provider=" + cipher.getProvider()); ECGenParameterSpec ecsp = new ECGenParameterSpec("sect163r2"); kpg.initialize(ecsp); KeyPair kyp = kpg.genKeyPair(); PublicKey pubKey = kyp.getPublic(); PrivateKey privKey = kyp.getPrivate(); System.out.println (cipher.getProvider()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); String cleartextFile = "cleartext.txt"; String ciphertextFile = "ciphertextECIES.txt"; byte[] block = new byte[64]; FileInputStream fis = new FileInputStream(cleartextFile);

FileOutputStream fos = new FileOutputStream(ciphertextFile); CipherOutputStream cos = new CipherOutputStream(fos, cipher); int i; while ((i = fis.read(block)) != -1) { cos.write(block, 0, i); } cos.close(); // Decrypt String cleartextAgainFile = "cleartextAgainECIES.txt"; cipher.init(Cipher.DECRYPT_MODE, privKey, ecsp); fis = new FileInputStream(ciphertextFile); CipherInputStream cis = new CipherInputStream(fis, cipher); fos = new FileOutputStream(cleartextAgainFile); while ((i = cis.read(block)) != -1) { fos.write(block, 0, i); } fos.close(); } catch (Exception e) { System.out.println(e); } } }

import import import import import import import import import import import import

org.bouncycastle.jce.interfaces.ECPublicKey; org.bouncycastle.jce.interfaces.ECPrivateKey; org.bouncycastle.crypto.AsymmetricBlockCipher; org.bouncycastle.crypto.InvalidCipherTextException; org.bouncycastle.crypto.engines.AESEngine; org.bouncycastle.crypto.modes.CBCBlockCipher; org.bouncycastle.crypto.params.ECDomainParameters; org.bouncycastle.jce.ECNamedCurveTable; org.bouncycastle.jce.spec.ECParameterSpec; org.bouncycastle.crypto.params.ECPrivateKeyParameters; org.bouncycastle.crypto.params.ECPublicKeyParameters; javax.crypto.Cipher;

public class ASymCrypto {

//

//cipher init private static AsymmetricBlockCipher bc = null; private static PaddedBufferedBlockCipher cipher = null;

//keys and info parameter private static ECPublicKeyParameters publicParam = null; private static ECPrivateKeyParameters privParam = null;

/** * Constructor */ ASymCrypto(ECPublicKey pubKey, ECPrivateKey privKey) {

// //

//default paddedBufferedBlockCipher with PKCS5/7 padding cipher = new PaddedBufferedBlockCipher(bc); System.out.println( "remotePrivateKey: " + privKey + " -(format): "+ privKey.getFormat() + " algo: " + privKey.getAlgorithm()); System.out.println( "remotePrivateKey: " + pubKey + " -(format): "+ pubKey.getFormat() + " algo: " + pubKey.getAlgorithm()); //get the key and the EC parameters ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime192v1"); ECDomainParameters domainParam = new ECDomainParameters( ecSpec.getCurve() , ecSpec.getG(), ecSpec.getN()); //ECPublicKeyParameters(ECPoint Q, ECDomainParameters params) publicParam = new ECPublicKeyParameters( pubKey.getQ() , domainParam ); if(publicParam == null) System.out.println("ERROR: Initializing ASymCrpto failed at ECPublicKeyParam."); //ECPrivateKeyParameters(java.math.BigInteger d, ECDomainParameters params) privParam = new ECPrivateKeyParameters( privKey.getD(), domainParam ); if(privParam == null) System.out.println("ERROR: Initializing ASymCrpto failed at ECPrivateKeyParam."); bc = new AsymmetricBlockCipher(new AESEngine()); } /** * encryptEC192 function * @param input: byte array with the message to encrypt

* @param output: byte array with the encrypted message using the public key of the partner * @return bool true if successfully encrypted * @throws InvalidCipherTextException */ public boolean encryptEC192(byte[] input, byte[] output) throws InvalidCipherTextException{ if(publicParam == null) System.out.println("ERROR2: Initializing ASymCrpto failed at ECPublicKeyParam."); bc.init( true, publicParam); System.out.println("InputBS: " + bc.getInputBlockSize() + " OutputBS: " + bc.getOutputBlockSize() + "\n"); output = bc.processBlock(input, 0, input.length ); return true; }

/** * encryptEC192 function * @param input: byte array with the message to encrypt * @param output: byte array with the encrypted message using the public key of the partner * @return bool true if successfully encrypted * @throws InvalidCipherTextException */ public boolean decryptEC192(byte[] input, byte[] output) throws InvalidCipherTextException{ if(privParam == null) System.out.println("ERROR2: Initializing ASymCrpto failed at ECPrivateKeyParam."); bc.init( false, privParam); System.out.println("InputBS: " + bc.getInputBlockSize() + " OutputBS: " + bc.getOutputBlockSize() + "\n"); output = bc.processBlock(input, 0, input.length ); return true; }

// // // // // // //

INFORMATION PURPOSE ONLY: public byte[] processBlock(byte[] in, int inOff, int len) throws InvalidCipherTextException process the block of len bytes stored in in from offset inOff. Parameters:

// // // // // // // //

in - the input data inOff - offset into the in array where the data starts len - the length of the block to be processed. Returns: the resulting byte array of the encryption/decryption process. Throws: InvalidCipherTextException - data decrypts improperly. DataLengthException - the input data is too large for the cipher.

You might also like