Wednesday, October 20, 2010

Encrypting and decrypting portlet properties in Oracle WCI Portal

A useful feature from Oracle WCI portal is the possibility to save crypted portlet properties. Using an private key by the portlet administrator, enables him to save the encrypted settings and then by using an public key to read the information into the portlet view page. 
To do this first we have to generate private and public key. You can generate public key using some of the tools in internet or just use java by doing the following:
1 // RSA by default 1024bit 2 KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 3 4 KeyPair generatedKeyPair = keyGen.genKeyPair(); 5 6 // save the keys 7 PrivateKey privateKey = keyPair.getPrivate(); 8 PublicKey publicKey = keyPair.getPublic(); 9 10 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); 11 12 // save the public key 13 FileOutputStream fos = new FileOutputStream("public.key"); 14 fos.write(x509EncodedKeySpec.getEncoded()); 15 fos.close(); 16 17 // save the private key 18 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec( 19 privateKey.getEncoded()); 20 fos = new FileOutputStream("private.key"); 21 fos.write(pkcs8EncodedKeySpec.getEncoded()); 22 fos.close(); 23 24 25 26




We do have now our RSA public and private keys which we will use to decrypt and encrypt the portlet setting. In our portlet application we have to read those keys and generate KeyPair which then we use inside the Chiper to decrypt or encrypt the properties. The code bellow shows you how to read the keys and generate the key pairs.




1 // get the cipher and load the private key 2 Cipher cipher = Cipher.getInstance("RSA"); 3 4 // Read Public Key. 5 File filePublicKey = new File("public.key"); 6 FileInputStream fis = new FileInputStream("public.key"); 7 byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; 8 fis.read(encodedPublicKey); 9 fis.close(); 10 11 // Read Private Key. 12 File filePrivateKey = new File("private.key"); 13 fis = new FileInputStream("private.key"); 14 byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()]; 15 fis.read(encodedPrivateKey); 16 fis.close(); 17 18 // Generate KeyPair. 19 KeyFactory keyFactory = KeyFactory.getInstance(algorithm); 20 X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); 21 22 PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); 23 PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); 24 PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); 25 26 KeyPair keyPari = new KeyPair(publicKey, privateKey); 27




Having the KeyPair we can do the decrypting and encrypting process and then save this information inside the WCI portlet settings.


1 // code follows 2 3 // to encrypt property and then save 4 cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); 5 6 portletContext.getResponse().setEncryptedSettingValue(SettingType.Portlet, "YourPropertyName", propertyValue, cipher); 7 8 9 // to read a encrypted property 10 cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); 11 String prop = portletContext.getRequest().getEncryptedSettingValue(SettingType.Portlet, "YourPropertyName", cipher); 12 13 14 15 16 17

1 comment: