gpt4 book ai didi

java - AES 加密 : Encrypt using Arduino and decrypt using Java

转载 作者:行者123 更新时间:2023-12-04 17:54:39 27 4
gpt4 key购买 nike

我想使用 Arduino 加密文本并使用 Java 解密。我从 this link 尝试了这段代码,但没有成功。

我正在使用此 Arduino library 在 Arduino 上进行加密,并在 Java 端使用 Java 加密扩展 (JCE) 框架。

这是 Arduino 代码:

#include <AESLib.h>  //replace the ( with < to compile (forum posting issue)
#include <Base64.h>

void setup() {
Serial.begin(9600);
uint8_t key[] = {50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50};
//expressed in 16 unsigned in characters, be careful not to typecast this as a char in a decrypter
//16- 50's (uint8) is the way to express 16 2's in ASCII, the encryption matches to what will show up on <a href="http://aesencryption.net/" target="_blank" rel="nofollow">http://aesencryption.net/</a>
char data[] = "0123456789012345";
//The message to encrypt, 16 chars == 16 bytes, no padding needed as frame is 16 bytes
char encryptedData[100];
int *size;
Serial.print("Message:");
Serial.println(data);
aes128_enc_single(key, data);
Serial.print("encrypted:");
Serial.println(data);
int inputLen = sizeof(data);
int encodedLen = base64_enc_len(inputLen);
char encoded[encodedLen];
base64_encode(encoded, data, inputLen);
Serial.print("encrypted(base64):"); //used
Serial.println(encoded);
Serial.println("***********Decrypter************");
int input2Len = sizeof(encoded);
int decodedLen = base64_dec_len(encoded, input2Len);
char decoded[decodedLen];
base64_decode(decoded, encoded, input2Len);
Serial.print("encrypted (returned from Base64):");
Serial.println(decoded);
Serial.print("decrypted:");
Serial.println(decoded);
}

void loop() {
}

这是 Java 代码:

package main;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class ForTest {
public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
String message= "0123456789012345";//Message to encode 
String key = "2222222222222222"; 
// 128 bit key  -this key is processed as ASCII values 
System.out.println("Processing 3.0 AES-128 ECB Encryption/Decryption Example");
System.out.println("++++++++++++++++++++++++++++++++");
System.out.println("Original Message: " + message);
System.out.println("Key: " + key);
System.out.println("key in bytes: "+key.getBytes("UTF-8"));
System.out.println("==========================");
//Encrypter
SecretKeySpec skeySpec_encode = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher_encode = Cipher.getInstance("AES/ECB/NoPadding");
// Cipher cipher_encode = Cipher.getInstance("AES/ECB/PKCS5PADDING"); //AES-CBC with IV encoding, ECB is used without the IV, example shown on <a href="http://aesencryption.net/" target="_blank" rel="nofollow">http://aesencryption.net/</a>
cipher_encode.init(Cipher.ENCRYPT_MODE, skeySpec_encode);
byte[] encrypted = cipher_encode.doFinal(message.getBytes());
System.out.println("Encrypted String (base 64): "
+ DatatypeConverter.printBase64Binary(encrypted));
//encode without padding: Base64.getEncoder().withoutPadding().encodeToString(encrypted));
//encode with padding:  Base64.getEncoder().encodeToString(encrypted));
String base64_encrypted = DatatypeConverter.printBase64Binary(encrypted);
//Decrypter
SecretKeySpec skeySpec_decode = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher_decode = Cipher.getInstance("AES/ECB/NoPadding");
// Cipher cipher_decode = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher_decode.init(Cipher.DECRYPT_MODE, skeySpec_decode);
System.out.println("length: "+"Ouril+UTDF8htLzE".length());
byte[] decrypted_original = cipher_decode.doFinal(DatatypeConverter.parseBase64Binary("Ouril+UTDF8htLzEhiRj7wA="));
String decrypt_originalString = new String(decrypted_original);
System.out.println("Decrypted String: " + decrypt_originalString);
}
}

在 Java 中,当我尝试通过 Arduino 解密编码的字符串时,我得到了这个:

Processing 3.0 AES-128 ECB Encryption/Decryption Example
++++++++++++++++++++++++++++++++
Original Message: 0123456789012345
Key: 2222222222222222
key in bytes: [B@2a139a55
==========================
Encrypted String (base 64): Ouril+UTDF8htLzEhiRj7w==
length: 16
Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length not multiple of 16 bytes
at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1016)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:960)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at main.ForTest.main(ForTest.java:46)

有什么想法吗?谢谢!

最佳答案

大约一个星期后我就能够让它工作了——关于与其他系统集成的工作 Arduino 文档是废话:)

工作的 Arduino 代码:

#include "mbedtls/aes.h"
#include <Arduino.h>
#include <HTTPClient.h>
#include <base64.h>

void makeUpdateAPICall()
{
if (WiFi.status() == WL_CONNECTED)
{
HTTPClient http;

// Your Domain name with URL path or IP address with path
http.begin(serverName);

// Specify content-type header
http.addHeader("Content-Type", "text/plain");
http.addHeader("Authorization", "Bearer XXXXXXXX [whatever your web token is]");
http.addHeader("X-Content-Type-Options", "nosniff");
http.addHeader("X-XSS-Protection", "1; mode=block");

//AES Encrypt
esp_aes_context aesOutgoing;
unsigned char key[32] = "1234567812345678123456781234567" ;
key[31] = '8'; // we replace the 32th (index 31) which contains '/0' with the '8' char.

char *input = "Tech tutorials x";
unsigned char encryptOutput[16];

mbedtls_aes_init(&aesOutgoing);
mbedtls_aes_setkey_enc(&aesOutgoing, key, 256);
int encryptAttempt = mbedtls_aes_crypt_ecb(&aesOutgoing, MBEDTLS_AES_ENCRYPT, (const unsigned char *)input, encryptOutput);
USE_SERIAL.println();
USE_SERIAL.println("MBEDTLS_AES_EBC encryption result:\t ");
USE_SERIAL.print(encryptAttempt); //0 means that the encrypt/decrypt function was successful
USE_SERIAL.println();
mbedtls_aes_free(&aesOutgoing);

int encryptSize = sizeof(encryptOutput) / sizeof(const unsigned char);
USE_SERIAL.println("Size of AES encrypted output: ");
USE_SERIAL.println(encryptSize);

//Base 64 Encrypt
int inputStringLength = sizeof(encryptOutput);
int encodedLength = Base64.decodedLength((char *)encryptOutput, inputStringLength);
char encodedCharArray[encodedLength];
Base64.encode(encodedCharArray, (char *)encryptOutput, inputStringLength);
//Send to server
USE_SERIAL.print("Sending to server.");
int httpResponseCode = http.POST(encodedCharArray);

String payload = "{}";

if (httpResponseCode > 0)
{
//Retrieve server response
payload = http.getString();
}
// Free resources
http.end();
}
WiFi.disconnect();
}

工作 Java 代码:

public static String decrypt(String strToDecrypt, String key) {

    byte[] encryptionKeyBytes = key.getBytes();  
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/ECB/NoPadding");
SecretKey secretKey = new SecretKeySpec(encryptionKeyBytes, "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt.getBytes("UTF-8"))));
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

现在正在处理返回流程。

您使用此代码调用 Java 端:

final String decryptedText = AES.decrypt(encryptedStr, "12345678123456781234567812345678");
System.out.println("解密后的 AES ECB 字符串:");
System.out.println(解密文本);

想为任何发现他/她在同一条船上的可怜的懒汉提供这个:)

希望这对您有所帮助!

关于java - AES 加密 : Encrypt using Arduino and decrypt using Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41333727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com