AES加密使用CBC和PKCS5 / 7Padding在java / android中使用充气城堡

时间:2016-09-13 15:52:18

标签: java android bouncycastle

我找到了java bouncy castle https://www.bouncycastle.org/fips/BCUserGuide.pdf

的指南

我尝试运行以下示例3.3.1使用CBC和PKCS5 / 7Padding进行AES加密:

static byte[] encryptBytes(FipsOutputEncryptor outputEncryptor, byte[] plainText) throws IOException
 {
 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
 CipherOutputStream encOut = outputEncryptor.getEncryptingStream(bOut);
 encOut.update(plainText);
 encOut.close();
 return bOut.toByteArray();
 }

static byte[] decryptBytes(FipsInputDecryptor inputDecryptor,
 byte[] cipherText) throws IOException
 {
 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
 InputStream encIn = inputDecryptor.getDecryptingStream(
 new ByteArrayInputStream(cipherText));
 int ch;
 while ((ch = encIn.read()) >= 0)
 {
 bOut.write(ch);
 }
 return bOut.toByteArray();
 }


// 3.3.1 AES Encryption using CBC and PKCS5/7Padding
     // ensure a FIPS DRBG in use.
     CryptoServicesRegistrar.setSecureRandom(
     FipsDRBG.SHA512_HMAC.fromEntropySource(
     new BasicEntropySourceProvider(new SecureRandom(), true))
     .build(null, true));
     byte[] iv = new byte[16];
     CryptoServicesRegistrar.getSecureRandom().nextBytes(iv);
     FipsSymmetricKeyGenerator<SymmetricSecretKey> keyGen =
     new FipsAES.KeyGenerator(128,
    CryptoServicesRegistrar.getSecureRandom());
     SymmetricSecretKey key = keyGen.generateKey();
     FipsSymmetricOperatorFactory<FipsAES.Parameters> fipsSymmetricFactory =
     new FipsAES.OperatorFactory();
     FipsOutputEncryptor<FipsAES.Parameters> outputEncryptor =
     fipsSymmetricFactory.createOutputEncryptor(key,
     FipsAES.CBCwithPKCS7.withIV(iv));

     byte[] output = encryptBytes(outputEncryptor, new byte[16]);
     FipsInputDecryptor<FipsAES.Parameters> inputDecryptor =
     fipsSymmetricFactory.createInputDecryptor(key,
     FipsAES.CBCwithPKCS7.withIV(iv));
     byte[] plain = decryptBytes(inputDecryptor, output);

并且代码无法编译。

我将以下库添加到类路径

bcprov-jdk15on-155.jar
bcmail-jdk15on-155.jar
bcpg-jdk15on-155.jar
bcpkix-jdk15on-155.jar

我使用该库的原因是将AesCbcPkcs7与我的Android应用程序集成。你能指点我的任何提示来编译上面的例子吗?

最诚挚的问候, 奥勒利安

1 个答案:

答案 0 :(得分:0)

我使用以下代码测试 - 没有充气城堡 - 并且工作完美:

import android.util.Base64;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by aurelian.rosca.
 */
public class EncryptionProvider2 {
    private final String characterEncoding = "UTF-8";
    private final String cipherTransformation = "AES/CBC/PKCS5Padding";
    private final String aesEncryptionAlgorithm = "AES";

    public  byte[] decrypt(byte[] cipherText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher cipher = Cipher.getInstance(cipherTransformation);
        SecretKeySpec secretKeySpecy = new SecretKeySpec(key, aesEncryptionAlgorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpecy, ivParameterSpec);
        cipherText = cipher.doFinal(cipherText);
        return cipherText;
    }

    public byte[] encrypt(byte[] plainText, byte[] key, byte [] initialVector) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException
    {
        Cipher cipher = Cipher.getInstance(cipherTransformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, aesEncryptionAlgorithm);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
        plainText = cipher.doFinal(plainText);
        return plainText;
    }

    private byte[] getKeyBytes(String key) throws UnsupportedEncodingException {
        byte[] keyBytes= new byte[16];
        byte[] parameterKeyBytes= key.getBytes(characterEncoding);
        System.arraycopy(parameterKeyBytes, 0, keyBytes, 0, Math.min(parameterKeyBytes.length, keyBytes.length));
        return keyBytes;
    }


    public String encrypt(String plainText, String key) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
        byte[] plainTextbytes = plainText.getBytes(characterEncoding);
        byte[] keyBytes = getKeyBytes(key);
        return Base64.encodeToString(encrypt(plainTextbytes,keyBytes, keyBytes), Base64.NO_WRAP);
    }


    public String decrypt(String encryptedText, String key) throws KeyException, GeneralSecurityException, GeneralSecurityException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, IOException {
        byte[] cipheredBytes = Base64.decode(encryptedText, Base64.NO_WRAP);
        byte[] keyBytes = getKeyBytes(key);
        return new String(decrypt(cipheredBytes, keyBytes, keyBytes), characterEncoding);
    }
}