Java decryption (AES/CBC) exception on Linux: Java.lang.SecurityException:JCE cannot authenticate the provider BC approach

Source: Internet
Author: User
Tags decrypt

When using MapReduce to do data cleansing, the data must be decrypted, the encryption method is: Aes/cbc/pkcs7padding, because Java itself does not support, need to add dependencies, with the dependency is:

1 <Dependency>2         <groupId>Org.bouncycastle</groupId>3         <Artifactid>Bcprov-jdk15on</Artifactid>4         <version>1.56</version>5 </Dependency>

A tool class for encrypting and decrypting, with the code attached:

1  Packagecom.js.utils;2 3 ImportOrg.bouncycastle.jce.provider.BouncyCastleProvider;4 5 ImportJavax.crypto.Cipher;6 ImportJavax.crypto.spec.IvParameterSpec;7 ImportJavax.crypto.spec.SecretKeySpec;8 ImportJava.security.Key;9 Importjava.security.Security;Ten Importjava.util.Arrays; One  A  Public classAescbcutil { -     //algorithm name -     FinalString key_algorithm = "AES"; the     //encryption/decryption algorithm/mode/fill mode -     FinalString algorithmstr = "Aes/cbc/pkcs7padding"; -     // -     Privatekey key; +     PrivateCipher Cipher; -     Booleanisinited =false; +  A     byte[] IV = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; at      Public voidInitbyte[] keybytes) { -  -         //if the key is less than 16 bits, then it will be replenished. The content in this if is important -         intBase = 16; -         if(keybytes.length% Base! = 0) { -             intGroups = Keybytes.length/base + (keybytes.length% base! = 0? 1:0); in             byte[] temp =New byte[Groups *base]; -Arrays.fill (Temp, (byte) 0); toSystem.arraycopy (keybytes, 0, temp, 0, keybytes.length); +Keybytes =temp; -         } the         //Initialize *Security.addprovider (NewBouncycastleprovider ()); $         //Key format converted to JavaPanax NotoginsengKey =NewSecretkeyspec (keybytes, key_algorithm); -         Try { the             //Initialize cipher +cipher = Cipher.getinstance (algorithmstr, "BC"); A}Catch(Exception e) { the e.printstacktrace (); +         } -     } $     /** $ * Encryption Method -      * @paramcontent to encrypt a string -      * @paramkeybytes Encryption Key the      * @return -      */Wuyi      Public byte[] Encrypt (byte[] content,byte[] keybytes) { the         byte[] Encryptedtext =NULL; - init (keybytes); Wu         Try { -Cipher.init (Cipher.encrypt_mode, Key,NewIvparameterspec (iv)); AboutEncryptedtext =cipher.dofinal (content); $}Catch(Exception e) { - e.printstacktrace (); -         } -         returnEncryptedtext; A     } +     /** the * Decryption Method -      * @paramEncryptedData The string to decrypt $      * @paramkeybytes decryption Key the      * @return the      */ the      Public byte[] Decrypt (byte[] EncryptedData,byte[] keybytes) { the         byte[] Encryptedtext =NULL; - init (keybytes); in         Try { theCipher.init (Cipher.decrypt_mode, Key,NewIvparameterspec (iv)); theEncryptedtext =cipher.dofinal (EncryptedData); About}Catch(Exception e) { the e.printstacktrace (); the         } the         returnEncryptedtext; +     } -}

When Windows developed the MapReduce program, the test was fine, but the Linux cluster would have an error:


Worry about the dead, never encountered this problem, no way to read the blog!
In fact, this is due to the Linux version of Java and Windows have some differences caused by the following workaround:
1, in the MVN warehouse to find the download package Bcprov-jdk15on-1.56.jar, or if you do not use Maven, as long as you can download to this package, placed under the Linux $java_home/jre\lib\ext;
2. Configure my Security properties file: Vim/usr/java/jdk1.8.0_112/jre/lib/security/java.security

1security.provider.1=Sun.security.provider.Sun2Security.provider.2=sun.security.rsa.SunRsaSign3security.provider.3=Sun.security.ec.SunEC4security.provider.4=Com.sun.net.ssl.internal.ssl.Provider5security.provider.5=Com.sun.crypto.provider.SunJCE6security.provider.6=Sun.security.jgss.SunProvider7security.provider.7=Com.sun.security.sasl.Provider8security.provider.8=Org.jcp.xml.dsig.internal.dom.XMLDSigRI9security.provider.9=Sun.security.smartcardio.SunPCSCTenSecurity.provider.10=org.bouncycastle.jce.provider.bouncycastleprovider

The 10th line is I add, is by the own document content number to determine;

Because it is a distributed cluster, each node has to be the same as the environment Oh!

After completing these configurations, re-execute this task and succeed!

Java decryption (AES/CBC) exception on Linux: Java.lang.SecurityException:JCE cannot authenticate the provider BC approach

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.