久久久久久久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)
主站蜘蛛池模板: 可以免费观看的av片 | 亚洲精品欧美 | 久久99国产精一区二区三区 | 欧美一区二区在线观看 | av日韩在线播放 | 亚洲激情一区二区 | 成人做爰www免费看视频网站 | 日韩中文字幕2019 | 中文字幕乱码视频32 | 欧美日韩精品在线免费观看 | 精品久久久久久久久久久下田 | 国产不卡一区 | 91精品国产乱码久久久久久 | 中文字幕一区二区在线观看 | 九色porny自拍视频 | 国产精品久久久久久久久久 | 国产精品久久久久久久久久久久久久 | 亚洲精品久久久久中文字幕欢迎你 | 久久国产精品免费 | 99精品亚洲国产精品久久不卡 | 欧美成人精品一区二区男人看 | 日韩欧美1区2区 | 日本不卡高字幕在线2019 | 一区二区精品 | 免费网站国产 | 久久躁日日躁aaaaxxxx | 日韩在线中文 | 精品亚洲一区二区三区 | 亚洲欧美综合精品久久成人 | 奇米四色在线观看 | 精品国产乱码久久久久久蜜退臀 | 99成人精品 | 久久久.com | 欧美久久精品一级c片 | 一区二区三区四区在线 | 亚洲综合视频一区 | 亚洲免费在线观看av | 女同久久另类99精品国产 | 久久精品视频91 | 欧美久久国产精品 | 成年人在线观看视频 |