[英]AES Encryption in Golang and Decryption in Java
我有以下用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)
}
Java解碼器(另見在線runnable演示 ,打開並單擊“執行”):
String decode(String base64Text, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] inputArr = Base64.getUrlDecoder().decode(base64Text);
SecretKeySpec skSpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
int blockSize = cipher.getBlockSize();
IvParameterSpec iv = new IvParameterSpec(Arrays.copyOf(inputArr, blockSize));
byte[] dataToDecrypt = Arrays.copyOfRange(inputArr, blockSize, inputArr.length);
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv);
byte[] result = cipher.doFinal(dataToDecrypt);
return new String(result, StandardCharsets.UTF_8);
}
Kevin在評論中提供了原始Go編碼器的演示 ,我們可以看到以下結果:
encrypt([]byte("0123456789abcdef"), "test text 123")
是c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0=
。
讓我們看看上面的Java解碼器如何處理該輸入:
String text = "c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0=";
byte[] key = "0123456789abcdef".getBytes();
System.out.println(decode(text, key));
打印test text 123
123✔
Scala版本( 在線runnable演示 ):
def decode(input:String, key:String) = {
val cipher = Cipher.getInstance("AES/CFB/NoPadding")
val blockSize = cipher.getBlockSize()
val keyBytes = key.getBytes()
val inputArr = Base64.getUrlDecoder().decode(input)
val skSpec = new SecretKeySpec(keyBytes, "AES")
val iv = new IvParameterSpec(inputArr.slice(0, blockSize).toArray)
val dataToDecrypt = inputArr.slice(blockSize, inputArr.size)
cipher.init(Cipher.DECRYPT_MODE, skSpec, iv)
new String(cipher.doFinal(dataToDecrypt.toArray))
}
def main(args: Array[String]) {
print(decode("c1bpFhxn74yzHQs-vgLcW6E5yL8zJfgceEQgYl0=", "0123456789abcdef"));
}
我認為Scala版本中唯一的錯誤是使用Hex.decodeHex
。 您需要一個Base64解碼器,它使用RFC 4648中描述的URL安全字母表, java.util.Base64
提供了(自Java 8以來)其getUrlDecoder()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.