我有以下用Golang编写的AES加密函数。
func encrypt(key []byte, text string) string {
plaintext := []byte(text)
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return base64.URLEncoding.EncodeToString(ciphertext)
}
我很难理解如何使用Java解密生成的文本流。非常感谢您能提供任何帮助!
这里是Scala代码,不确定其中存在什么问题。
def decode(input:String) = {
val keyBytes = Hex.decodeHex("someKey".toCharArray)
val inputWithoutPadding = input.substring(0,input.size - 2)
val inputArr:Seq[Byte] = Hex.decodeHex(inputWithoutPadding.toCharArray)
val skSpec = new SecretKeySpec(keyBytes, "AES")
val iv = new IvParameterSpec(inputArr.slice(0,16).toArray)
val dataToDecrypt = inputArr.slice(16,inputArr.size)
val cipher = Cipher.getInstance("AES/CFB/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv)
cipher.doFinal(dataToDecrypt.toArray)
}
XORKeyStream
。 - zaph