2016-09-14 15 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(byte [] 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