Weird "illegal start of expression" error in Netbeans - java

In the following programs, command is a byte array field in Apdu class. The problem is that my IDE (Netbeans) mark the line of
apdu.command = {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};
As error, with the message illegal start of expression, while it is okay to use the following instead:
byte[] bytes = {(byte) 0x00, (byte) 0xa4, (byte) 0x00, (byte) 0x00};
apdu.command = bytes;
What's wrong with the first programs?
First program:
Second Program:

You need to initialize the array variable with :
apdu.command = new byte[] {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};
The initialization you tried is only valid in array declaration expressions.

array syntax {} can only be used for initialization not for assignment.
you need to use apdu.command =new byte[] {(byte) 0x00, (byte)0xa4, (byte) 0x00, (byte) 0x00};.

Related

How to have a constant initialization vector and secret key in java?

I'm trying to decrypt an access token (it's a String), which is used to default access an Dropbox account and uploading files into it. So right now, I always need that access token to make file uploadings.
Until now, I've been generating a new initialization vector (IV) and a new secret key to encrypt and decrypt the access token. However, I want to store these two in the source code, as constant variables/attributes. The reason why I want them to remain the same ? Because I will give a crypted access token (always the same encoded one) to the users, and the app should keep the IV and the secret key inside the source code.
How can I store them in my source code ?
I tried to write the string values of the IV and of the secret key in files. I use the string from the files, and I assign the string values to string constants in my code. Then i use my constants to create byte arrays for converting into the IV and into the secret key. I'm not sure if this will work yet, it's still in development.
You'd better heed the advice. Storing the key is bad but can sometimes be defended if no other options are available. There is however generally no reason to use a static IV. You can just prefix the IV (which is 16 bytes for most modes of operation) to the ciphertext instead.
Anyway, to store them as static values, just take a look at the following code; note that you should generate them as random values in advance, not the static values you're seeing here:
private static final byte[] KEY_DATA = {
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
(byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
(byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
};
private static final byte[] IV_DATA = {
(byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
(byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
(byte) 0x08, (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
(byte) 0x0C, (byte) 0x0D, (byte) 0x0E, (byte) 0x0F,
};
public static void main(String[] args) throws Exception {
Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKey key = new SecretKeySpec(KEY_DATA, "aes");
IvParameterSpec iv = new IvParameterSpec(IV_DATA);
aes.init(Cipher.ENCRYPT_MODE, key, iv);
...
}
Note that SecretKeySpec implements the interface SecretKey for easy usage.

Java int to hex with 0x

I'm trying to convert int to hex with format 0x12 0x2B and so on. Is there anything similar like python: https://docs.python.org/2/library/functions.html#hex
to accomplish this or i will need to work around this with many unnecessary steps?
I need to get something like this below:
int []hexInt={0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01};
You could declare a String which will be equal to "0x" + Integer.toHexString(int)
public static String intToHexStr(int i){
return "0x"+String.format("%2s", Integer.toHexString(i)).replace(' ', '0');
}
Maybe this is very late but here is the answer to your question:
Integer.parseInt(String.format("%X", intValue),16)

can not select file on ISO-7816-4 smartcard

I am trying to read data from ISO-7816-4 eVCR (electronic vehicle registration card) using
javax.smartcardio. Whenever I try to select file from card I get SW code 6A86 which
translates to 'Incorrect P1 or P2 parameter'. I tried many combinations of values for
P1 and P2 and got same result.
Card itself works fine with other programs and sample code works fine with other cards.
Card I have problem with is same card as in this question.
This is code I use:
Card card = terminal.connect("*");
System.out.println("ATR: " + Utils.bytes2HexString(card.getATR().getBytes()));
byte aid[] = {(byte)0xA0, 0x00, 0x00, 0x00, 0x77, 0x01, 0x08, 0x00, 0x07,
0x00, 0x00, (byte) 0xFE, 0x00, 0x00, (byte) 0xAD, (byte) 0xF2};
ResponseAPDU response = null;
CardChannel channel = card.getBasicChannel();
response = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x04, 0x0C, aid));
System.out.println("AID: " + response);
response = channel.transmit(new CommandAPDU(0x00, 0xA4, 0x02, 0x00, new byte[]{(byte)0xD0, 0x01}));
System.out.println("SELECT: " + response);
And output is:
ATR: 3B:DB:96:00:80:B1:FE:45:1F:83:00:31:C0:64:1A:18:01:00:0F:90:00:52
AID: ResponseAPDU: 2 bytes, SW=9000
SELECT: ResponseAPDU: 2 bytes, SW=6a86
I can't see what am I doing wrong. Do some cards require extra initialization steps or some extra parameters for select?
You could use 0x0C as P2 (instead of 0x00)? Maybe the file ID is correct, but it cannot give back any file information (0x0C means: don't give back file information).
It depends on the card operating system and/or application if this would influence the returned status word.

Mifare Classic 1k APDU command for retrieving tag UID

As the title says; is there a APDU command for retrieving the UID of a tag? I am using Java, with an ACR122-u cardreader and the javax.smartcardio.* package and I want to get the UID from a tag on the scanner. The smartcardio library can send CommandAPDU's but I need to figure out what APDU to send. Google has not been very friendly to me on this one, providing me with thousands of unhelpful datasheets of some sort...
Any help would be great :)
Better late than never but there is actually an APDU to JUST retrieve the UID: (byte) 0xFF, (byte) 0xCA, (byte) 0x00, (byte) 0x00, (byte) 0x00
FF CA 00 00 00
In Java: byte[] getuid = new byte[] { (byte) 0xFF, (byte) 0xCA, (byte) 0x00,
(byte) 0x00, (byte) 0x00 };
If you send this APDU, the response data will be just the UID of the card :) (Much easier than having more info and having to set an offset to get just the info you need...)
The APDU Command for Read UID is
byte[] baReadUID = new byte[5];
baReadUID = new byte[] { (byte) 0xFF, (byte) 0xCA, (byte) 0x00,
(byte) 0x00, (byte) 0x00 };
All Complete code is here....

How to convert a "binary string" to base64?

I've followed this: https://dev.twitter.com/docs/auth/creating-signature
to the end but I can't find how to encode a "binary string" to base64(at the end). I wanted to try online converters first but they don't give the string the show
"tnnArxj06cWHq44gCs1OSKk/jLY="
Tried this: http://www.motobit.com/util/base64-decoder-encoder.asp
and
http://www.hash-cracker.com/base64.php#anchor
and
http://www.opinionatedgeek.com/dotnet/tools/Base64Encode/
and none give that string.
I'm going to be using java. But I think all those java tools I search for will give the same result as the online converters. What has to be done to encode a "binary string" to base64?
The problem of using those online tools isn't going to be in the base64 conversion - it's going to be parsing the hex string into a byte array to start with. In your real code that won't be a problem, as it'll be the output of another stage. Just to prove that, here's some sample Java code, using a public domain base64 encoder:
public class Test {
public static void main(String[] args) throws Exception {
byte[] data = { (byte) 0xB6, (byte) 0x79, (byte) 0xC0, (byte) 0xAF,
(byte) 0x18, (byte) 0xF4, (byte) 0xE9, (byte) 0xC5,
(byte) 0x87, (byte) 0xAB, (byte) 0x8E, (byte) 0x20,
(byte) 0x0A, (byte) 0xCD, (byte) 0x4E, (byte) 0x48,
(byte) 0xA9, (byte) 0x3F, (byte) 0x8C, (byte) 0xB6 };
String text = Base64.encodeBytes(data);
System.out.println(text);
}
}
Output: tnnArxj06cWHq44gCs1OSKk/jLY=
You can use this link to convert Binary String to Base64
https://cryptii.com/pipes/binary-to-text
Here is an example of converting Binary String to Base64 String:
Here is another example of converting Normal String to Base64 String

Categories