I see encoding function on Java back-end and I need to write same functionality on NestJS (NodeJS). The result of these transformations is something like so C6EB48123226CDBB3A5048FE6A92CA06, I see it in a database table.
So, I tried to use cripto-js library, it works with DES encryption but I couldn't get result that looks like C6EB48123226CDBB3A5048FE6A92CA06.
As instance (works fine):
var encrypted = CryptoJS.DES.encrypt("DataFromInput", "secretKey");
var decrypted = CryptoJS.DES.decrypt(encrypted, "secretKey");
But my question still is open. I don't have any idea that I have to do for getting string similar to C6EB48123226CDBB3A5048FE6A92CA06, because I don't understand that Java code below does and how I can implement it in Javascript.
I need some step-by-step instruction like: Encrypt string -> Take property X from result -> Transform it to array -> Get hash of array etc.
public static String getEncrypted( String str, String key )
{
try
{
KeySpec keySpec = new DESKeySpec( key.getBytes( Charset.forName( "UTF-8")));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DES");
SecretKey desKey = keyFactory.generateSecret( keySpec);
try ( ByteArrayOutputStream cipheredBaos = new ByteArrayOutputStream() )
{
Cipher desCipher = Cipher.getInstance( "DES/ECB/PKCS5Padding");
desCipher.init( Cipher.ENCRYPT_MODE, desKey);
CipherOutputStream cos = new CipherOutputStream( cipheredBaos, desCipher);
cos.write( str.getBytes( "UTF-8"));
cos.close( );
return DatatypeConverter.printHexBinary( cipheredBaos.toByteArray( ));
}
}
catch ( Exception e)
{
System.out.println( e.getMessage() );
return "";
}
}