Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypting in NetSuite Suitescript and decrypting in java application

I need to encrypt a string using SuiteScript, send it to a web service written in Java, and decrypt it there.

Using SuiteScript I'm able to encrypt and decrypt without any issue. But when I use the same key in java, I get different errors.

var x = "string to be encrypted";
var key = 'EB7CB21AA6FB33D3B1FF14BBE7DB4962';
var encrypted = nlapiEncrypt(x,'aes',key);
var decrypted = nlapiDecrypt(encrypted ,'aes',key);

^^works fine^^

The code in Java

final String strPassPhrase = "EB7CB21AA6FB33D3B1FF14BBE7DB4962"; //min 24 chars

    SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
    SecretKey key = factory.generateSecret(new DESedeKeySpec(strPassPhrase.getBytes()));
    Cipher cipher = Cipher.getInstance("DESede");

    cipher.init(Cipher.DECRYPT_MODE, key);

    String encrypted = "3764b8140ae470bda73f7ebed3c33b0895f70c3497c85f39043345128a4bc3b3";
    String decrypted = new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(encrypted)));
    System.out.println("Text Decryted : " + decrypted);

With the above code, I get an exception javax.crypto.BadPaddingException: Given final block not properly padded

The key was generated using openssl

openssl enc -aes-128-ecb -k mypassphrase -P
like image 421
Binoy Samuel Avatar asked Dec 20 '25 17:12

Binoy Samuel


1 Answers

it looks like you are encrypting with AES, and decrypting with DES. I think the ciphertext needs to be decrypted with the same symmetric algorithm that you used to encrypt.

like image 123
Douglas Held Avatar answered Dec 22 '25 07:12

Douglas Held



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!