1

I want to encrypt integer values . I wrote an example for encrypt String values . In this integer encryption, I don't want convert integer to String .

This is my String encryption,

String strDataToEncrypt = new String();
String strCipherText = new String();
String strDecryptedText = new String();

try {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(128);
    SecretKey secretKey = keyGen.generateKey();

    final int AES_KEYLENGTH = 128;  
    byte[] iv = new byte[AES_KEYLENGTH / 8];    
    SecureRandom prng = new SecureRandom();
    prng.nextBytes(iv);

    Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS5PADDING");

    aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));

    strDataToEncrypt = "Hello World of Encryption using AES ";
    byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
    byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);

    strCipherText = new BASE64Encoder().encode(byteCipherText);
    System.out.println("Cipher Text generated using AES is " + strCipherText);


    Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 

    aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
    byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText);
    strDecryptedText = new String(byteDecryptedText);

    System.out.println(" Decrypted Text message is " + strDecryptedText);

} catch (NoSuchAlgorithmException noSuchAlgo) {
    System.out.println(" No Such Algorithm exists " + noSuchAlgo);
} catch (NoSuchPaddingException noSuchPad) {
    System.out.println(" No Such Padding exists " + noSuchPad);
} catch (InvalidKeyException invalidKey) {
    System.out.println(" Invalid Key " + invalidKey);
} catch (BadPaddingException badPadding) {
    System.out.println(" Bad Padding " + badPadding);
} catch (IllegalBlockSizeException illegalBlockSize) {
    System.out.println(" Illegal Block Size " + illegalBlockSize);
} catch (InvalidAlgorithmParameterException invalidParam) {
    System.out.println(" Invalid Parameter " + invalidParam);
}

Encrypted value shouldn't be in String. It may be in BigInteger or something like it.

Have any ideas ?

4
  • 1
    What do you mean by "In this integer encryption, I don't want convert integer to String ." ? The encrypted result should be an integer? Commented Sep 14, 2016 at 9:43
  • 1
    All encryption use byte as source data and return byte. If you want to encrypt BigInteger just use toByteArray() and new BigInteger(byte[] val) Commented Sep 14, 2016 at 9:44
  • The encrypted result shouldn't be a String .: @SamuelKok Commented Sep 14, 2016 at 9:44
  • @JEY : Explain with more details. Commented Sep 14, 2016 at 9:45

1 Answer 1

0

I took your code as is and change to encrypt BigInteger:

try {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey secretKey = keyGen.generateKey();

        final int AES_KEYLENGTH = 128;  
        byte[] iv = new byte[AES_KEYLENGTH / 8];    
        SecureRandom prng = new SecureRandom();
        prng.nextBytes(iv);

        Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS5PADDING");

        aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));

        BigInteger bigIntToEncrypt = new BigInteger("123465746443654687461161655434494984631323");
        byte[] byteDataToEncrypt = bigIntToEncrypt.toByteArray();
        byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);

        BigInteger chipherBigInt = new BigInteger(byteCipherText);
        System.out.println("Cipher Int generated using AES is " + chipherBigInt);


        Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 

        aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText);
        BigInteger decryptedInt = new BigInteger(byteDecryptedText);

        System.out.println(" Decrypted Text message is " + decryptedInt);

    } catch (NoSuchAlgorithmException noSuchAlgo) {
        System.out.println(" No Such Algorithm exists " + noSuchAlgo);
    } catch (NoSuchPaddingException noSuchPad) {
        System.out.println(" No Such Padding exists " + noSuchPad);
    } catch (InvalidKeyException invalidKey) {
        System.out.println(" Invalid Key " + invalidKey);
    } catch (BadPaddingException badPadding) {
        System.out.println(" Bad Padding " + badPadding);
    } catch (IllegalBlockSizeException illegalBlockSize) {
        System.out.println(" Illegal Block Size " + illegalBlockSize);
    } catch (InvalidAlgorithmParameterException invalidParam) {
        System.out.println(" Invalid Parameter " + invalidParam);
    }

You can encrypt any type and return a BigInteger from the encrypted data:

try {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey secretKey = keyGen.generateKey();

        final int AES_KEYLENGTH = 128;  
        byte[] iv = new byte[AES_KEYLENGTH / 8];    
        SecureRandom prng = new SecureRandom();
        prng.nextBytes(iv);

        Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS5PADDING");

        aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));

        String dataToEncrypt = "Nice text i want to encrypt";
        byte[] byteDataToEncrypt = dataToEncrypt.getBytes();
        byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);

        BigInteger chipherBigInt = new BigInteger(byteCipherText);
        System.out.println("Cipher Int generated using AES is " + chipherBigInt);


        Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS5PADDING"); 

        aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText);
        String decryptedInt = new String(byteDecryptedText);

        System.out.println(" Decrypted Text message is " + decryptedInt);

    } catch (NoSuchAlgorithmException noSuchAlgo) {
        System.out.println(" No Such Algorithm exists " + noSuchAlgo);
    } catch (NoSuchPaddingException noSuchPad) {
        System.out.println(" No Such Padding exists " + noSuchPad);
    } catch (InvalidKeyException invalidKey) {
        System.out.println(" Invalid Key " + invalidKey);
    } catch (BadPaddingException badPadding) {
        System.out.println(" Bad Padding " + badPadding);
    } catch (IllegalBlockSizeException illegalBlockSize) {
        System.out.println(" Illegal Block Size " + illegalBlockSize);
    } catch (InvalidAlgorithmParameterException invalidParam) {
        System.out.println(" Invalid Parameter " + invalidParam);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

No it's just how you represents bytes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.