2016-09-14 70 views
1

我想加密整數值。我寫了一個加密字符串值的例子。 在此整數加密,我不想轉換爲字符串整數。Java加密:加密整數值

這是我的字符串加密,

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); 
} 

加密值不應該在字符串。它可能在BigInteger或類似的東西。

有什麼想法?

+1

你是什麼意思_」在這個整數加密中,我不想將整數轉換爲字符串。「_?加密結果應該是一個整數? –

+1

所有加密使用字節作爲源數據並返回字節。如果要加密的BigInteger只使用toByteArray()和新的BigInteger(字節[] VAL) – JEY

+0

加密的結果不應該是一個字符串:@SamuelKok – Barrier

回答

0

我把你的代碼是並更改加密的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); 
    } 

您可以加密任何類型和加密數據返回一個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)); 

     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); 
    } 
+0

我猜這是驗證加密?我有點困惑... – Barrier

+0

不,它只是如何表示字節。 – JEY