Java uses aes/cbc/pkcs7padding and decrypts strings

Source: Internet
Author: User
Tags decrypt

Pkcs7padding is not supported in Java, only supports pkcs5padding but pkcs7padding and pkcs5padding are no different
To implement pkcs7padding padding on the Java side, you need to use the Bouncycastle component to implement

So a jar is needed to support it. Bcprov-jdk16-146.jar

: Http://central.maven.org/maven2/org/bouncycastle/bcprov-jdk16/1.46/bcprov-jdk16-1.46.jar

Nonsense not to say, below the code

Add Decryption class

/** * * @authorNgh * AES128 algorithm * CBC mode * * pkcs7padding fill mode * * CBC mode need to add a parameter IV * * In Java does not support pkcs7padding, only support pkcs5padding but PKC There's no difference between s7padding and pkcs5padding. * To implement pkcs7padding padding on the Java side, you need to use the Bouncycastle component to implement*/ Public classAES {//algorithm name FinalString key_algorithm = "AES"; //encryption/decryption algorithm/mode/fill mode FinalString algorithmstr = "Aes/cbc/pkcs7padding"; // Privatekey key;PrivateCipher Cipher;Booleanisinited =false; byte[] IV = {0x30, 0x31, 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30, 0x37, 0x30, 0x38 };  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); byte[] temp =New byte[Groups *Base]; Arrays.fill (temp, (byte) 0); System.arraycopy (Keybytes,0, temp, 0, keybytes.length); Keybytes=temp; }  //InitializeSecurity.addprovider (NewBouncycastleprovider ()); //Key format converted to JavaKey =NewSecretkeyspec (keybytes, key_algorithm); Try {   //Initialize ciphercipher = Cipher.getinstance (algorithmstr, "BC"); } Catch(nosuchalgorithmexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchpaddingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(nosuchproviderexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } } /*** Encryption Method * *@paramcontent * String to encrypt *@paramkeybytes * Encryption Key *@return  */  Public byte[] Encrypt (byte[] content,byte[] keybytes) {  byte[] Encryptedtext =NULL;  Init (keybytes); System.out.println ("IV:" +NewString (iv)); Try{cipher.init (Cipher.encrypt_mode, Key,NewIvparameterspec (iv)); Encryptedtext=cipher.dofinal (content); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }  returnEncryptedtext;} /*** Decryption Method * *@paramEncryptedData * The string to decrypt *@paramkeybytes * Decryption key *@return  */  Public byte[] Decrypt (byte[] EncryptedData,byte[] keybytes) {  byte[] Encryptedtext =NULL;  Init (keybytes); System.out.println ("IV:" +NewString (iv)); Try{cipher.init (Cipher.decrypt_mode, Key,NewIvparameterspec (iv)); Encryptedtext=cipher.dofinal (EncryptedData); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }  returnencryptedtext;}}

Test class

UblicclassTest { Public Static voidMain (string[] args) {AES AES=NewAES ();//Add decryption Key  byte[] keybytes = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38 }; String content= "1"; //Encrypt stringSystem.out.println ("Pre-encryption:" +content); System.out.println ("Encryption key:" +NewString (keybytes)); //Encryption Method  byte[] enc =Aes.encrypt (Content.getbytes (), keybytes); System.out.println ("Encrypted content:" +NewString (Hex.encode (ENC))); //Decryption Method  byte[] Dec =aes.decrypt (ENC, keybytes); System.out.println ("Decrypted content:" +NewString (DEC)); }}
test Result: Pre-encryption:1 Encryption key:12345678IV:0102030405060708  Content after encryption: B59227d86200d7fedfb8418a59a8eea9iv:0102030405060708 decrypted content:1

Java uses aes/cbc/pkcs7padding and decrypts strings

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.