首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java加密:加密整数值

Java加密:加密整数值
EN

Stack Overflow用户
提问于 2016-09-14 09:40:32
回答 1查看 1.6K关注 0票数 1

我想加密整数值。我编写了一个加密字符串值的示例。在这个整数加密中,我不希望将整数转换为字符串

这是我的字符串加密,

代码语言:javascript
复制
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之类的地方。

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-09-14 09:52:46

我以您的代码为原样,并更改为加密BigInteger:

代码语言:javascript
复制
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:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39487229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档