Jump to content

need help converting java encryption to delphi

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
Ewe Loon

Ewe Loon

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I tracked the following java code down, and need to convert it to deplhi , anyone able to help


   public static String encryptedAES(String text, String key, String iv) {

        SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");

        IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());


        try {

            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);

            return new String(toHexString(cipher.doFinal(padChar(text,

                    '\u0000', 16).getBytes())));

        } catch (Exception e) {

            SWGAide.printError("SimpleCrypto:encryptedAES: ", e);

        }

        return text;

    }


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The problem is there isn't a built in library for Delphi encryption, like there is with Java. Your first step will be to get an appropriate library (there are some free ones) and add them to your Delphi installation.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Ewe Loon

Ewe Loon

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I have already downloaded many encryption libs for delphi, but the problem is most of them dont use a key, but the one used by java uses 2 keys, (keyspec, ivspec)
nothing I have looked at does this.

#4
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts

Ewe Loon said:

I have already downloaded many encryption libs for delphi, but the problem is most of them dont use a key

All encryprion uses key. You may confuse it with hashing when think of encryption without key.

To port the code you have to understand what ivspec is, i.e. why does that java code needs it. Perhaps you need to study that java Cipher library to get better understanding. Then translate or find workaround.

What is your encryption library? if you are using DCPCrypt (my favorite) then you must note that AES is the same with Rijndael.

Ewe Loon said:

, but the one used by java uses 2 keys, (keyspec, ivspec) nothing I have looked at does this.

Well, actually it's nothing special. That line was just initializing the Cipher object. This usually to make sure that same content do not encrypted into the same values. To increase the difficulty for attackers. I forgot the term for this. Salting is one popular method for this .

But like I have stated initially, you still need to know the detail of ivspec to know the exact translation or workaround to do.