久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 AES 和 PBE 中使用 Bouncy Castle 輕量級 API

How to use Bouncy Castle lightweight API with AES and PBE(如何在 AES 和 PBE 中使用 Bouncy Castle 輕量級 API)
本文介紹了如何在 AES 和 PBE 中使用 Bouncy Castle 輕量級 API的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個使用 JCE 算法PBEWithSHA256And256BitAES-CBC-BC"創建的密文塊.提供者是 BouncyCastle.我想做的是使用 BouncyCastle 輕量級 API 解密這個密文.我不想使用 JCE,因為這需要安裝 Unlimited Strength Jurisdiction Policy Files.

I have a block of ciphertext that was created using the JCE algorithim "PBEWithSHA256And256BitAES-CBC-BC". The provider is BouncyCastle. What I'd like to do it decrypt this ciphertext using the BouncyCastle lightweight API. I don't want to use JCE because that requires installing the Unlimited Strength Jurisdiction Policy Files.

在將 BC 與 PBE 和 AES 結合使用時,文檔似乎很少.

Documentation seems to be thin on the ground when it comes to using BC with PBE and AES.

這是我目前所擁有的.解密代碼運行無異常但返回垃圾.

Here's what I have so far. The decryption code runs without exception but returns rubbish.

加密代碼,

String password = "qwerty";
String plainText = "hello world";

byte[] salt = generateSalt();
byte[] cipherText = encrypt(plainText, password.toCharArray(), salt);

private static byte[] generateSalt() throws NoSuchAlgorithmException {
    byte salt[] = new byte[8];
    SecureRandom saltGen = SecureRandom.getInstance("SHA1PRNG");
    saltGen.nextBytes(salt);
    return salt;
}

private static byte[] encrypt(String plainText, char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    Security.addProvider(new BouncyCastleProvider());

    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);

    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithSHA256And256BitAES-CBC-BC");
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

    Cipher encryptionCipher = Cipher.getInstance("PBEWithSHA256And256BitAES-CBC-BC");
    encryptionCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

    return encryptionCipher.doFinal(plainText.getBytes());
}

解密代碼,

byte[] decryptedText = decrypt(cipherText, password.getBytes(), salt);

private static byte[] decrypt(byte[] cipherText, byte[] password, byte[] salt) throws DataLengthException, IllegalStateException, InvalidCipherTextException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
    BlockCipher engine = new AESEngine();
    CBCBlockCipher cipher = new CBCBlockCipher(engine);

    PKCS5S1ParametersGenerator keyGenerator = new PKCS5S1ParametersGenerator(new SHA256Digest());
    keyGenerator.init(password, salt, 20);

    CipherParameters keyParams = keyGenerator.generateDerivedParameters(256);
    cipher.init(false, keyParams);

    byte[] decryptedBytes = new byte[cipherText.length];
    int numBytesCopied = cipher.processBlock(cipherText, 0, decryptedBytes, 0);

    return decryptedBytes;
}

推薦答案

我試過這個,它似乎工作.大量借鑒了 BC 類 org.bouncycastle.jce.provider.test.PBETest

I tried this and it seemed to work. Borrowed heavily from the BC class org.bouncycastle.jce.provider.test.PBETest

private byte[] decryptWithLWCrypto(byte[] cipher, String password, byte[] salt, final  int iterationCount)
        throws Exception
{
    PKCS12ParametersGenerator pGen = new PKCS12ParametersGenerator(new SHA256Digest());
    char[] passwordChars = password.toCharArray();
    final byte[] pkcs12PasswordBytes = PBEParametersGenerator
            .PKCS12PasswordToBytes(passwordChars);
    pGen.init(pkcs12PasswordBytes, salt, iterationCount);
    CBCBlockCipher aesCBC = new CBCBlockCipher(new AESEngine());
    ParametersWithIV aesCBCParams = (ParametersWithIV) pGen.generateDerivedParameters(256, 128);
    aesCBC.init(false, aesCBCParams);
    PaddedBufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(aesCBC,
            new PKCS7Padding());
    byte[] plainTemp = new byte[aesCipher.getOutputSize(cipher.length)];
    int offset = aesCipher.processBytes(cipher, 0, cipher.length, plainTemp, 0);
    int last = aesCipher.doFinal(plainTemp, offset);
    final byte[] plain = new byte[offset + last];
    System.arraycopy(plainTemp, 0, plain, 0, plain.length);
    return plain;
}

這篇關于如何在 AES 和 PBE 中使用 Bouncy Castle 輕量級 API的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 999久久久| 日本精品视频在线 | 激情国产 | 91精品一区二区三区久久久久久 | 国产欧美一区二区三区在线播放 | 国产区在线| 国产在线区 | 成人免费视频播放 | 一区二区三区在线免费 | 国产又爽又黄的视频 | 欧日韩不卡在线视频 | 欧美激情一区二区三区 | 国产精品一区二区视频 | 欧美一区二区三区国产精品 | 密室大逃脱第六季大神版在线观看 | 欧美日韩国产一区二区 | 久久九九免费 | 日韩视频在线一区二区 | 久久精品91 | 国产污视频在线 | 久久国产综合 | 成人欧美一区二区 | 欧美激情综合 | 免费激情 | 国产线视频精品免费观看视频 | 日韩免费 | 亚洲乱码一区二区三区在线观看 | 岛国av免费看| 人人爽人人爽 | 中文字幕亚洲专区 | 一区二区三区精品在线视频 | 欧美日韩国产一区二区 | 国产精品国产三级国产aⅴ中文 | 欧美乱码精品一区二区三区 | 男人天堂色 | 成人日韩 | 99re在线视频 | 91久久久久久久久 | 国产精品久久久久久久久久久免费看 | 欧美日韩精品在线一区 | 国产精品一区在线 |