AES encryption and decryption algorithm, the most complex is the CFB model, because the CFB model each additional random one IV, resulting in the same key, each generation of the encryption string is different. Decryption time to use this iv,iv attached to the encrypted data inside, and some implementation is the IV put the last data, some of the most before, such as the data after encryption is XXX, then the final data is 16Bytes iv+xxx or Xxx+16bytes IV.
Package main import ("Crypto/aes" "Crypto/cipher" "Crypto/rand" "encoding/base64" "Flag" "FMT" "io" "Log" "OS" "Errors") var pass = flag.
String ("Pass", "Password111111111111111111111111", "Password char phrase-can is set to anything but keep it private") var text = flag. String ("text", "HelloWorld", "Plain text to encode") var ciphertext = flag. String ("Cipher", "", "cipher text") var verbose = flag. Bool ("Verbose", false, "verbose flag") Func main () {flag. Parse () Key: = []byte (*pass)//bytes If Len (OS). Args) < 2 {println ("Usage:goaes-pass password111111111111111111111111-text HelloWorld") println ("or") println ("Usage:goaes-pass password111111111111111111111111-cipher Hnonmpzab32fz1f80vil2pjq+ahp/upo") os. Exit (1)} Data: = "Iloveyouforever" mystring: = base64. Stdencoding.encodetostring ([]byte (data)) FMT. Printf ("BASE64 =%s\n", mystring) if *ciphertext = = "" {plaintext: = []byte (*text) ciphertextoutput, err: = Actia_ Encrypt (key, plainText) If Err!= nil {log. Fatal (Err)} ciphertextOutput1, err: = Mike_encrypt (key, plaintext) FMT. Printf ("Actia encrytp =%s\n", ciphertextoutput) fmt. Printf ("MIKE encrytp =%s\n", CIPHERTEXTOUTPUT1)} else {cipherbytes, err: = base64. Stdencoding.decodestring (*ciphertext) If Err!= nil {log. Fatal ("err=", Err)} println ("Start ddecrypt") result, ERR: = Actia_decrypt (Key, cipherbytes) if ERR!= nil {L og. Fatal (Err)} FMT. Printf ("actia_decrypt=%s\n", result) result1, err: = Mike_decrypt (Key, Cipherbytes) FMT. Printf ("mike_decrypt=%s\n", Result1)}} func Actia_encrypt (key, text []byte) ([]byte, error) {block, err: = AES. Newcipher (key) If Err!= nil {return nil, err}//b: = base64. Stdencoding.encodetostring (text) b: = text ciphertext: = Make ([]byte, AES. Blocksize+len (b)) if *verbose {println ("blocksize=", AES. BlockSize, "ciphertext=", string (ciphertext))} IV: = Ciphertext[:aes. BlockSize] If _, err: = Io. Readfull (Rand. ReAder, iv); Err!= Nil {return nil, err} CFB: = Cipher. Newcfbencrypter (block, IV) CFB. Xorkeystream (Ciphertext[aes. BlockSize:], []byte (b)] return []byte (base64. Stdencoding.encodetostring (ciphertext)), nil//return ciphertext, nil} func Actia_decrypt (key, text []byte) (]byte, Error) {block, err: = AES. Newcipher (key) If Err!= nil {return nil, err} IV: = Text[:aes. BlockSize] Text = Text[aes. BlockSize:] If *verbose {println ("IV", base64. Stdencoding.encodetostring (iv), "cipher text", base64. Stdencoding.encodetostring (iv))} CFB: = Cipher. Newcfbdecrypter (block, []byte (iv)) CFB. Xorkeystream (text, text)//data, err: = base64. Stdencoding.decodestring (String (text))//if err!= Nil {//return nil, err//}//STR: = String (text[:])//fmt. Println (str) return text, nil}//--------------------------------------------------------------------------------- --------//here are our Encrypt:func mike_encrypt (key, text []byte) ([]byte, error) {BLOck, err: = AES.
Newcipher (key) If Err!= nil {return nil, err} Ciphertext: = Make ([]byte, AES. Blocksize+len (text) IV: = Ciphertext[:aes. BlockSize] If _, err: = Io. Readfull (Rand. Reader, iv); Err!= Nil {return nil, err} CFB: = Cipher. Newcfbencrypter (block, IV) CFB. Xorkeystream (Ciphertext[aes. BlockSize:], text) return []byte (base64. Stdencoding.encodetostring (ciphertext)), nil}//and this should to be a working decrypt:func (key, text [Mike_decrypt e) ([]byte, error) {block, err: = AES. Newcipher (key) If Err!= nil {return nil, err} If Len (text) < AES. BlockSize {return nil, errors. New ("Ciphertext Too Short")} IV: = Text[:aes. BlockSize] Text = Text[aes. BlockSize:] CFB: = cipher. NewcfbdecRypter (block, IV) CFB. Xorkeystream (text, text) data, err: = base64. Stdencoding.decodestring (String (text)) If Err!= nil {return nil, err} return data, n
Il}
With Golang encryption, with the Java decryption, Java decryption code in my uploaded resources, I uploaded the resources I have modified the code.
Java source on the GitHub download address is: https://github.com/platinumjesus/crypto015,
You need to download the Us_export_policy.jar and Local_policy.jar packages that correspond to your JDK version and replace the two packages in your installation directory, otherwise there will be a problem.
Http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters
Java Cryptography Extension (JCE) Unlimited strength jurisdiction Policy Files 6
Java Cryptography Extension (JCE) Unlimited Strength jurisdiction Policy Files 7 Download
Java Cryptography Extension (JCE) Unlimited strength jurisdiction Policy Files 8 Download
Also need to pay attention to is the position of IV, the go code above put IV in front, Java code placed in the back, have to modify the corresponding, otherwise go encrypted data Java can not decrypt.
And the secret key I used was 256-bit.
The go language is directly a 32 byte string, Java needs to convert 32 byte strings into a 16-character string, with a length of 64 bytes, and the code converts 64 byte strings to 32 byte binary keys, which are essentially 256-bit. It's just easy to see the secret key manually.