Sample Code in Java
Generation of appKey
public static byte[] createAESKey() {
appKey = null;
try {
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(256);
SecretKey secret = gen.generateKey();
appKey = secret.getEncoded();
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
}
return appKey;
}
Asymmetric Encryption (RSA)
The following Java code snippet can be used for encrypting the password and the appkey using the public key given by the e-Invoice System. The encryption method used here is RSA.
public static String encryptAsymmetricKey(String pubkey, String password) throws Exception{
PublicKey publicKeys = convertPubStringToKey(pubkey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, publicKeys);
byte[] encryptedText = cipher.doFinal(password.getBytes());
String encryptedPassword = Base64.encodeBase64String(encryptedText);
return encryptedPassword;
}
private static PublicKey convertPubStringToKey(String publikkey)
{
PublicKey pubKey = null;
byte[] publicBytes = Base64.decodeBase64(publikkey);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
KeyFactory keyFactory;
try {
keyFactory = KeyFactory.getInstance("RSA");
pubKey = keyFactory.generatePublic(keySpec);
} catch (Exception e)
{
e.printStackTrace();
}
return pubKey;
}
public static String encryptAsymmetricKey(String pubkey, byte[] appKey) throws Exception
{
PublicKey publicKeys = covertPubStringToKey(pubkey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, publicKeys);
  byte[] encryptedText = cipher.doFinal(appKey);
String encryptedAppKey = Base64.encodeBase64String(encryptedText);
return encryptedAppKey;
}
Symmetric Key Decryption using Java
public static String decrptyBySyymetricKey(String encryptedSek, byte[] appKey)
{
Key aesKey = new SecretKeySpec(appKey, "AES"); // converts bytes(32 byte random generated) to key
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // encryption type = AES with padding PKCS5
cipher.init(Cipher.DECRYPT_MODE, aesKey); // initiate decryption type with the key
byte[] encryptedSekBytes = Base64.decodeBase64(encryptedSek); // decode the base64 encryptedSek to bytes
byte[] decryptedSekBytes = cipher.doFinal(encryptedSekBytes); // decrypt the encryptedSek with the initialized cipher containing the key(Results in bytes)
String decryptedSek = Base64.encodeBase64String(decryptedSekBytes); // convert the decryptedSek(bytes) to Base64 StriNG
return decryptedSek; // return results in base64 string
}catch(Exception e)
{
return "Exception; "+e;
}
}
Symmetric Key Encryption using Java.
private static String encryptBySymmetricKey(String json, String decryptedSek)
{
byte[] sekByte = Base64.decodeBase64(decryptedSek);
Key aesKey = new SecretKeySpec(sekByte, "AES");
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encryptedjsonbytes = cipher.doFinal(json.getBytes());
String encryptedJson = Base64.encodeBase64String(encryptedjsonbytes);
return encryptedJson;
}
catch(Exception e) {
return "Exception "+e;
}
}
Symmetric Key Decryption using Java (Decrypt using SEK)
public static String decryptBySymmentricKey(String data, String decryptedSek) {
byte[] sekByte = Base64.getDecoder().decode(decryptedSek);
Key aesKey = new SecretKeySpec(sekByte, "AES");
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(data);
byte[] decValue = cipher.doFinal(decordedValue);
return new String(decValue);
} catch (Exception e) {
return "Exception " + e;
}
}
Decoding the Signed eInvoice
private void decodeSignedJWT(String signedText)
{
try {
BASE64Decoder decoder = new BASE64Decoder();
String[] splitSignedText = signedText.split("\\.");
String decodedSigned =new String(decoder.decodeBuffer(splitSignedText[0]));
decodedSigned = decodedSigned +"\n Content:"+(new String(decoder.decodeBuffer(splitSignedText[1])));
decodedSigned.replaceAll("\\\"", "\"");
System.out.println("\nDecoded Text:" + decodedSigned);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}