The exclusive promotional activities on JCOP J3H145 card and Feitian R301 Smartcard Reader are in full swing. Please check this page for details.

Combine two byte array and encrypt them togethor

Applets Development Guide

Moderator: product

OSSAMA
Posts: 6
Joined: Sun Mar 19, 2017 8:54 am
Points :242
Contact:

Combine two byte array and encrypt them togethor

Post by OSSAMA » Fri Aug 31, 2018 8:09 am

hello everyone,
I need to combine two arrays and then encrypt the combined array with my AES.
the first part ( the first array is the Card_Static_Information) and the second part (the second array is the outcoming response data) which will be stored in the buffer.
I have tried many ways but still, no one worked.
your help is very appreciated.
Ossama

Code: Select all

private void GENERATE_APPLICATION_CRYPTOGRAM_AC (APDU apdu, short len) 
	{
		byte[] Card_Static_Information = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16};
		if (len <= 0 || len % 16 != 0)
		 {
			ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
		}
	        byte [] buffer = new byte [32];
	        buffer = apdu.getBuffer();
		Util.arrayCopyNonAtomic(Card_Static_Information, (short)0, buffer, (short)16, (short)16);
		Key key = getAesKey();
		byte mode = buffer[ISO7816.OFFSET_P1] == (byte) 0x00 ? Cipher.MODE_ENCRYPT : Cipher.MODE_DECRYPT;
		Cipher cipher = buffer[ISO7816.OFFSET_P2] == (byte) 0x00 ? aesEcbCipher : aesCbcCipher;
		if (cipher == aesCbcCipher) 
		{
			cipher.init(key, mode, AESICV, (short) 0, (short) 16);
		} 
		else 
		{
			cipher.init(key, mode);
		}
		cipher.doFinal(buffer, ISO7816.OFFSET_CDATA, len, buffer, (short) 0);
		apdu.setOutgoingAndSend((short) 0, (short) 32);		
	}

owlstead
Posts: 9
Joined: Sun Jan 27, 2019 10:57 am
Points :28
Contact:

Re: Combine two byte array and encrypt them togethor

Post by owlstead » Sun Jan 27, 2019 6:30 pm

I have tried many ways but still, no one worked.
That's not an error description. We cannot tell what you've tried an how it failed.

You should use `cipher.update` instead of concatenating the plaintext together. If you want you can send the ciphertext in one go if the result is put right after each other in the APDU buffer, although also sending the bytes separately using - uh - `sendBytes` would probably make most sense. If it doesn't fit in one APDU then you may need to implement response chaining or extended length APDU's.

tay00000
Posts: 141
Joined: Tue Sep 27, 2016 10:58 am
Points :1884
Contact:

Re: Combine two byte array and encrypt them togethor

Post by tay00000 » Tue Jan 29, 2019 6:16 am

Just adding additional points which @owlstead has done a good job in pointing.

Here's a couple of extra problems I noticed in the OP's source code.
byte [] buffer = new byte [32];
buffer = apdu.getBuffer();
Do you know that by doing that, you will create a persistent memory object with a 32 byte byte array called 'buffer' and then you use it to contain the APDU buffer ? firstly, that will shorten the life of your smart card's persistent storage because everytime you call the GENERATE_APPLICATION_CRYPTOGRAM_AC() command, it will create a buffer object with 32 byte byte array in the persistent memory. Please read up on the impact of creating 'new' objects as this is NOT Java SE. This is Java Card and 'new' will create objects in the persistent memory of a smart card chip and thus shorten it's lifespan as well as writing to EEPROM on card is 1000x slower.

So here's what you should be doing:
byte[] buffer = apdu.getBuffer();
That's it. Short and simple.
Util.arrayCopyNonAtomic(Card_Static_Information, (short)0, buffer, (short)16, (short)16);
Do you know that you are copying into the buffer array 16 bytes from the Card_Static_Information with 16 bytes offset from the buffer which already contains the APDU headers with who knows what data is inside.

You can create a compute buffer and copy data from both the Card_Static_Information and data from the APDU buffer into the compute buffer and then encrypt the compute buffer and send it out or as @owlstead mentioned, just use update() on the Cipher.

We need more context on what your code is trying to do and what you want before a proper response can be created.

In the meantime, read the following documentations on JavaCards and Smart Cards:

- JC 2.2.2 (https://www.oracle.com/technetwork/java ... 38637.html)
- JC 3.0.4 (https://www.oracle.com/technetwork/java ... 36430.html)
- GP 2.1.1 (https://www.win.tue.nl/pinpasjc/docs/Ca ... 0v0303.pdf)
- ISO-7816 (http://cardwerk.com/iso-7816-smart-card-standard/)

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 2 guests

JavaCard OS : Disclaimer