0

I'm using the jpbc java library to achieve the HIBE encryption in order to encrypt and decrypt a String data.

In this case, i found a function that's allowed me to get an Element from a String as it shown below and it make me able to encrypt & decrypt this Element :

private static void elementFromString(Element h, String s) throws NoSuchAlgorithmException 
    {
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        byte[] digest = md.digest(s.getBytes());

        h.setFromHash(digest, 0, digest.length);
    } 

But now, i need a solution to get the string data from the Element after decryption or any other idea can help me.

Thank you

arbi mahdi
  • 64
  • 2
  • 9

1 Answers1

0

Hashing is not encrypting. SHA-1 is a one-way function, i.e. you can possibly guess the input given the output, but you cannot retrieve the input by any calculation.

To encrypt and decrypt you need a cipher. A cipher often consists of a block cipher and a mode of operation such as "AES/GCM/NoPadding". Kind of surprising, it is represented by the Java Cipher class.

You may need to generate a key or key pair and store it somewhere, such as a KeyStore.


Trying to perform crypto without understanding what you are doing may result in code that runs - eventually. However, the chances that it will be secure is minimal. First try and learn about the subject.

Note that calling getBytes on a String results in platform specific encoding. This can be fun if you switch platforms in the future. Explicitly specifying UTF-8 is a good idea.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263