Posts

RSA Assymmetric Key Encryption

In addition to the standard X509 *.cer certificates there are also certificate files ending with *.PFX or *.P12. The later ones are X509 certs as well, but may in addition contain a private key, too.   PFX was a Microsoft extension, while P12 was the Netscape one. Generating from Scratch using OpenSSL 1. Generate a 2048-bit RSA private key $ openssl genrsa -out private_key.pem 2048 This will generate a key file in traditional key format also called ssLeay private key format. 2. Convert private Key to PKCS#8 format (so Java can read it) $ openssl pkcs8 -topk8 -inform PEM -outform DER -in private_key.pem -out private_key.der -nocrypt 3. Output public key portion in DER format (so Java can read it) $ openssl rsa -in private_key.pem -pubout -outform DER -out public_key.der Generating from the .p12 File Private key: openssl pkcs12 -in   yourP12File.pfx   -nocerts -out   privateKey.pem This will generate the private key alone...

java.security.spec.InvalidKeySpecException: Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys

public PrivateKey getPrivateKey(String fileName) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { byte[] keyFileBytes = readKeyFile(fileName); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyFileBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); } The reason is this line, This is used to read public key spec and not private key. The private key is read via PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec ( keyBytes ); So as the error says it needs to be in PKCS8 spec format for java to understand.

java.lang.annotation.IncompleteAnnotationException: javax.xml.bind.annotation.XmlSchemaType missing element name

Hi The use case happened when i was trying to generate the java files from the wsdl using the CXF codegen maven plugin. The generated code  started failing with the above exception when tried to invoke via Eclipse. Note : the same wsdl worked in Soap UI. The generated code in SOAP UI using the WSIMPORT tool also worked. So the issue i figures out is related to the Jaxb in the project. So what i did is upgraded the Jaxb to this version                  <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.2</version> </dependency> It all started working. So this has something to do with the internal element generation of Jaxb and what happens is when a latest version is not found falls back to the jaxb version embedded in SDK leading to that error. Now i dont have a proof for that just an educated guess. :)

Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - Spring Batch

This Error happens when you are trying to retrieve a Job Execution parameter and inject it into a Bean. The bean can be anything a Task let, or a StatmentSetter the error can happen. E.g Implementation <bean id = "flatFileItemReader" scope="step" class="org.springframework.batch.item.file.FlatFileItemReader"> <property name = "resource" value = "#{jobParameters[input.file.name]}" /> </bean> The error is because of the Scope parameter missing Using a scope of  Step  is required in order to use late binding since the bean cannot actually be instantiated until the  Step  starts, which allows the attributes to be found. Because it is not part of the Spring container by default, the scope must be added explicitly. Check out the Documentation here ..  http://docs.spring.io/spring-batch/reference/html/configureStep.html#step-scope

Installing OpenCv 3.0 and Javacv on Raspberry Pi

Preface --------- I had a free raspberry pi's version B lying around and a spare PS3 eye and a few hours in hand { which later extended to days and week :) }. Decided to use Javacv for a quick IP-Cam setup and a simple face recognition(Dint get time to work till now).  Thanks a lot for Samuel Audet for helping out with a lot of issues. https://groups.google.com/forum/#!topic/javacv/Ql1v5THKIcQ Going through the above link you can read all my interactions with Samuel. This is gonna be a pretty lengthy post. Read through if you need to compile a later version if not simply skip to interesting parts and links. Events --------- Step 1 Look for existing tutorial and I did found one,very well explained with steps  http://salaboy.com/2013/06/14/using-javacv-in-the-raspberry-pi-linux-arm/ Went through the steps, installed the opencv version 2.4.  Opencv installation success (!yaay!)  . Developer in me decided I will take a latest version of javac...

Autowiring a static variable using Spring

Yes, This sounds wrong and should never try to do it in a proper design (According to me). With the above disclaimer, Might run into such a case where we need to do this kind of thing, you are at the right blog. There are 2 ways to do it i guess, i have the simple working way jotted here. Lets get started Consider the class @Component public class Foo { private static ObjectA objectA; // Static variable to be used in static method in class @Autowired ObjectA autowiredObjectA; // Orginal bean that is autowired @postConstruct public void init() {   Foo.objectA=this.autowiredObjectA; // So this method is responsible for handing out the bean after                                                                     the class is constructed and autowired by the spring         ...

\r command not found. Cygwin script

You might have faced this error when creating a cygwin script, Even a simple script can replicate this issue #!/bin/sh date who ls ps ax ./deploy.sh ./deploy.sh: line 2: $'date\r': command not found ./deploy.sh: line 3: $'who\r': command not found https://cygwin.com/ml/cygwin-announce/2010-08/msg00015.html This page has the problem in detail.So the solution is #!/bin/sh # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples (set -o igncr) 2>/dev/null && set -o igncr; # this comment is needed date who ls ps ax This line does the trick. Happy Cygwin scripting :)