Java AES CBC Fill method Discovery

Source: Internet
Author: User
Tags base64

such as the Java code, after the block is manually populated, so that it is an integer multiple of 16, when the encryption is forced to fill the 16-bit, I am trying to use Golang implementation of this encryption, repeated changes for a long time, found that the Golang version is always more than the Java encryption and Base64 results less than 20 bits, so the results of each step in the middle of the fight out, found that there is no difference, and then try to force the Golang after the addition of 16 padding, then fill what? Yes, I was from 0x0 to 0x10 a try out, and finally found that when filled with 16 0x10, Golang and Java encryption results are exactly the same, the following paste Golang and Java two code:

Ps:iv and key are identical. Get a lesson, when Golang and Java under the two same patterns, if the encrypted ciphertext is different, try to modify the method of filling, it is always possible to try a few more times.

Java:

 Public StaticString AesEncrypt2 (String Appsecret, String data)throwsException {//setting the encryption keyString key = appsecret.substring (16);        SYSTEM.OUT.PRINTLN (key); Secretkeyspec Keyspec=NewSecretkeyspec (Key.getbytes (), "AES"); //initialization VectorSYSTEM.OUT.PRINTLN ("MD5:" + Digestutils.md5hex (Appsecret). substring (0, 16)); String IV= Digestutils.md5hex (Appsecret). substring (0, 16); Ivparameterspec Ivspec=NewIvparameterspec (Iv.getbytes ()); //Setting the encryption modeCipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding"); //Fill Algorithm        intBlockSize =cipher.getblocksize (); byte[] Databytes =data.getbytes (); intPlaintextlength =databytes.length; if(plaintextlength% BlockSize! = 0) {Plaintextlength= Plaintextlength + (blockSize-(plaintextlength%blockSize)); }        byte[] plaintext =New byte[Plaintextlength]; System.arraycopy (Databytes,0, plaintext, 0, databytes.length); System.out.println ("Padding after length =" +plaintext.length);  for(inti = 0;i < plaintext.length; i++) System.out.print (Plaintext[i]+ " "); //EncryptCipher.init (Cipher.encrypt_mode, Keyspec, Ivspec); byte[] encrypted =cipher.dofinal (plaintext); System.out.println ("");  for(inti = 0;i < encrypted.length;i++) System.out.print (Encrypted[i]+ " "); System.out.println (""); returnbase64.encodebase64string (encrypted); }
View Code

Golang:

Func GetMD5 (ciphertextstring)string{md5ctx:=MD5. New () Md5ctx.write ([]byte(ciphertext)) Cipherstr:=md5ctx.sum (Nil)returnHex. Encodetostring (CIPHERSTR)}func pkcs5padding (ciphertext []byte, blockSizeint) []byte{padding:= Blocksize-len (ciphertext)%blockSize//padtext: = bytes. Repeat ([]byte{byte (padding)}, padding)     forI: =0; i < padding; i++{ciphertext= Append (Ciphertext,0x0)    }     forI: = -; I < +; i++{ciphertext= Append (Ciphertext,0x10)} FMT. Println (ciphertext) fmt. Printf ("blocksieze[%d]\n", Len (ciphertext))//return Append (ciphertext, padtext ...)    returnCiphertext}func Aesencrypt (Secret, datastring)string {    ifLen (Secret) < -{log. Errorf ("aesencrypt secret[%s] length less")        return ""} fmt. Println (secret[ -:]) Key:= []byte(secret[ -:]) IV:= []byte(GetMD5 (Secret) [: -]) fmt. Println ("MD5:"+ GetMD5 (Secret) [: -]) block, err:=AES. Newcipher (Key)ifErr! =Nil {log. Errorf ("Key Error[%v]", Err)return ""} ECB:=cipher. Newcbcencrypter (block, iv) content:= Pkcs5padding ([]byte(data), block. BlockSize ()) crypted:= Make ([]byte, Len (content)) ECB. Cryptblocks (crypted, content) fmt. Println (crypted)returnBase64. Stdencoding.encodetostring (crypted)}
View Code

Java AES CBC Fill method Discovery

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.