Java pkcs7padding Encryption cannot find any provider supporting aes/cbc/pkcs7padding solution

Source: Internet
Author: User
Tags base64

In Java with aes256 encryption, but found that Java can not use pkcs7padding, and Java is the pkcs5padding fill, the solution is, Use the Bouncycastle component to enable pkcs7padding padding inside java.

Saying spicy is more than the code:

public class Aesutil {

/**
* Encodes a String in AES-256 with a given key
*
* @param context
* @param password
* @param text
* @return string Base64 and AES encoded string
*/
public static string encode (string keystring, String stringtoencode) throws NullPointerException {
if (keystring.length () = = 0 | | keystring = = NULL) {
throw new NullPointerException ("Please give Password");
}

if (stringtoencode.length () = = 0 | | stringtoencode = = NULL) {
throw new NullPointerException ("Please give text");
}

try {
Secretkeyspec Skeyspec = GetKey (keystring);
byte[] cleartext = stringtoencode.getbytes ("UTF8");

IMPORTANT to GET same RESULTS on IOS and ANDROID
Final byte[] IV = new BYTE[16];
Arrays.fill (iv, (Byte) 0x00);
Ivparameterspec Ivparameterspec = new Ivparameterspec (iv);

/**
* This place calls Bouncycastleprovider
* Let Java support pkcs7padding
*/
Security.addprovider (New Org.bouncycastle.jce.provider.BouncyCastleProvider ());
Cipher is not thread safe
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs7padding");
Cipher.init (Cipher.encrypt_mode, Skeyspec, Ivparameterspec);

String Encrypedvalue = base64.encodetostring (cipher.dofinal (cleartext), base64.default);
LOG.D ("Jacek", "Encrypted:" + stringtoencode + "+" + encrypedvalue);
return encrypedvalue;

} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Invalidalgorithmparameterexception e) {
E.printstacktrace ();
}
Return "";
}

/**
* Decodes a String using AES-256 and Base64
*
* @param context
* @param password
* @param text
* @return desoded String
*/
public static string decode (string password, string text) throws NullPointerException {

if (password.length () = = 0 | | password = = NULL) {
throw new NullPointerException ("Please give Password");
}

if (text.length () = = 0 | | text = = NULL) {
throw new NullPointerException ("Please give text");
}

try {
Secretkey key = GetKey (password);

IMPORTANT to GET same RESULTS on IOS and ANDROID
Final byte[] IV = new BYTE[16];
Arrays.fill (iv, (Byte) 0x00);
Ivparameterspec Ivparameterspec = new Ivparameterspec (iv);

byte[] Encrypedpwdbytes = Base64.decode (text, base64.default);
Cipher is not thread safe

/**
* This place calls Bouncycastleprovider
* Let Java support pkcs7padding
*/
Security.addprovider (New Org.bouncycastle.jce.provider.BouncyCastleProvider ());
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs7padding");
Cipher.init (Cipher.decrypt_mode, Key, Ivparameterspec);
Byte[] Decrypedvaluebytes = (cipher.dofinal (encrypedpwdbytes));

String decrypedvalue = new string (decrypedvaluebytes);
LOG.D (Log_tag, "decrypted:" + text + "+ decrypedvalue");
return decrypedvalue;

} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Invalidalgorithmparameterexception e) {
E.printstacktrace ();
}
Return "";
}

/**
* Generates a secretkeyspec for given password
*
* @param password
* @return Secretkeyspec
* @throws unsupportedencodingexception
*/
private static Secretkeyspec GetKey (String password) throws Unsupportedencodingexception {

Can change it to wish
int keylength = 256;
byte[] keybytes = new BYTE[KEYLENGTH/8];
Explicitly fill with zeros
Arrays.fill (Keybytes, (byte) 0x0);

If password is shorter then key length, it'll be zero-padded
to key length
byte[] Passwordbytes = password.getbytes ("UTF-8");
int length = Passwordbytes.length < keybytes.length? PasswordBytes.length:keyBytes.length;
System.arraycopy (passwordbytes, 0, keybytes, 0, length);
Secretkeyspec key = new Secretkeyspec (keybytes, "AES");
Return key;
}

public static void Main (String args[])
{
Long startTime = System.currenttimemillis ();
String encodestr = Aesutil.encode ("1234", "Where do you want to go test123");
SYSTEM.OUT.PRINTLN ("encoder:" +ENCODESTR);
String decodestr = Aesutil.decode ("1234", ENCODESTR);
SYSTEM.OUT.PRINTLN ("decoder:" +DECODESTR);


}
}

Bouncycastle component-related jar package Bcprov-jdk15on-152.jar: http://www.bouncycastle.org

The default Java only supports 128-bit keys, and when a 256-bit key is used, the key length error is reported. You need to download a package that supports longer keys. This bag is called

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8

, which can be downloaded from here: http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html. After the zip file is finished, replace the two jar package (Local_policy.jar,us_export_policy.jar) inside the corresponding jar in the security folder of the JDK to be OK.

But I do encounter a strange problem, I do not have the problem of calling Pkcs7padding on Android, but there is a problem with the Java-written server. I don't know why, the JDK I use is 1.8.0 's JDK. Please also ask if you have encountered the same situation can be answered.

Java pkcs7padding Encryption cannot find any provider supporting aes/cbc/pkcs7padding solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.