How can I convert an object (an instance of PublicKey) to byte[] and vice versa in Java Card applets?
I am using Java Card 2.1.1 and JCDK 2.1.2.
Java Card does not provide any built-in mechanism to serialize a PublicKey object (or objects in general) into a byte array or to deserialize a byte array back into an object.
Consequently you would need to manually perform such serialization/deserialization. For an object that implements the PublicKey interface, this would mean that you would first need to find out which type of key object it is, e.g. RSAPublicKey. For instance, if you know that the public key is a RSAPublicKey, you could then extract the type (getType()), size (getSize()), exponent (getExponent()), and modulus (getModulus()) parameters and store them into a byte array. Later, you could deserialize the byte array by extracting those values and instantiating a new key using the KeyBuilder.buildKey() method.
Related
I can't see the difference between an object dictionary object and an application object.
mapping of application objects into a PDO is determined by a
corresponding default PDO mapping structure within the object
dictionary
But we don't map such thing as "application object", but an object of the object dictionary.
In SDO service, the exact same objects are avaiable, but the specification don't use the term application object.
What is the property of an application object which distinguishes it from any other object?
CANopen application layer and communication profile
You can only map items located in the object dictionary. When adapting the protocol stack to your application, you have to arrange this relation between the object dictionary and variables in your code.
The mapping parameters are stored at for example 0x1A00 for TPDO1. From there you can map objects stored elsewhere in the object dictionary, such as for example objects in the manufacturer-specific area 0x2000-0x5FFF or device profile-specific areas from 0x6000 and upwards.
For example if you implement the device profile CiA 401 for general-purpose I/O module, you can make 8 bytes for TPDO1 available in 0x1A00, which in turn points at 0x6000:1 to 0x6000:8, which is where the device profile states that digital on/off should be stored. Or you could map something specific to your product from 0x2000.
If you do something like the above, it means that the network can either access the data indirectly through what's mapped to the PDO, or directly by doing a SDO access at for example 0x6000:1.
Also, consider if you need to support dynamic mapping = the user can change mapping themselves, or static mapping = mapping parameters are read-only, always the same.
I have a class of two strings
I want to decrypt and encrypt using RC4 , I know how to do that for one string
http://tofuculture.com/blog/post/RC4-Encryption-in-C.aspx
can I apply to whole object
The easiest way to serialize into a complex format is to first serialize into a simple format (in this case string) and then perform the known transformation from the simple to complex.
So in your example you want to serialize two strings, so decide on a separation character or string that won't appear in your data and use it to separate the two strings. This allows you to create a single string, which you already know how to handle. Then when you want to deserialize you do the opposite, finding the separation and splitting the strings again into a new object.
I would like to give two API functions to store files in a DB - one accepting a byte[] (or an InputStream) and another one accepting a String. I wonder how to implement such an API.
I see the following options:
Define two fields - one a BLOB for binary files and another one a CLOB - for text files. Then I could use PreparedStatement.setBytes (or PreparedStatement.setBinaryStream) for BLOBs and PreparedStatement.setString for CLOBs. I do not like having two fields.
Use String.getBytes() to convert the given String to byte[], thus coming back to the binary case. I do not like the need to convert a String to the byte[].
I was wondering whether there is a solution which does not require two fields and avoids the extra byte buffer.
Java String and Clob are Unicode and stored as two-byte characters. Therefore some conversion is necessary if you want to store these values in a byte[], or Blob.
Therefore the best approach is to use two separate fields, one for BLOB and the other for CLOB values.
If I remember correctly even when we pass large string or byte array as a parameter to a method, it only passes the pointer to the heap of the data rather than the full data. So it should not degrade any performance or it should not pile up memory unnecessarily. Just want to confirm if my understanding is correct regarding the above statement?
I know it is better to keep the string or byte array as a private variable in the class and access it in every method it required thus eliminating one additional parameter from the method call.
Thanks
If I remember correctly even when we pass large string or byte array as a parameter to a method, it only passes the pointer to the heap of the data rather than the full data.
Yes, when you pass any reference type argument, just the reference is passed, by value. Note that this is not the same as "pass by reference", which is applicable to parameters of both reference types and value types.
See my articles on reference types and value types and parameter passing for more information.
I know it is better to keep the string or byte array as a private variable in the class and access it in every method it required thus eliminating one additional parameter from the method call.
That entirely depends on the context. Is it logically part of the state of the class or not?
What is the best way to represent a byte array as an XSD schema? I have a byte input which I need to parse and to feed to Java objects generated by JAXB out of an XSD schema for future validation. Every piece of information in my input is defined by offset and the length. I would like to attach this information to my elements so I could then read it from generated classes and use for parsing. The only way to attach such additional information to an Element I can think of is representing every Element as a Complex Type and then adding attributes to the complex Element and restrictions – to the simple Element wrapped by this complex Element. The problem with this approach (in addition to the Schema looking messy) is that JAXB will create separate class for every Element (Complex Type). And I will have a lot of fields in my input. :) The second issue is that I will in fact attach attributes and restrictions (which I need for validation) to different elements (wrapped simple element and the complex wrapper). So I will need to find a way to figure out that they in fact represent the same piece of information. I hope all this make sense. :) Basically my question is: can you think of a more elegant way to attach position and length information to an XSD element that represents the corresponding piece of a byte array? Thanks!
What is the best way to represent a
byte array as an XSD schema? I have a
byte input which I need to parse and
to feed to Java objects generated by
JAXB out of an XSD schema for future
validation.
JAXB will convert a byte[] to/from elements and attributes of type xsd:base64Binary.
For information on starting from XML schema see:
http://bdoughan.blogspot.com/2011/05/schema-to-java-xmlmimetype.html
For information on starting from Java classes see:
http://bdoughan.blogspot.com/2011/03/jaxb-web-services-and-binary-data.html