gpt4 book ai didi

javascript - 在 Java 和 JavaScript 之间实现 AES

转载 作者:行者123 更新时间:2023-12-02 11:46:34 27 4
gpt4 key购买 nike

我正在尝试实现 aes/cbc/pkcs5padding 算法。我从 JS 中的一些 CryptoJS 开始,几秒钟之内就发现了加密和解密的工作原理是多么美妙。我搜索了许多兼容的解决方案,因为返回的值总是错误的。

不重要:我的目标是以下概念:

  1. 用户帐户是在服务器端创建的,密码使用 SHA-256 进行哈希处理
  2. 用户尝试登录网页(客户端 JS),其中一个数据包被发送到服务器,该数据包以他的纯用户名开头,后跟 AES 加密的 JSONObject 和简单的成功消息。 AES 的 key 是本地散列的用户密码的 SHA-256
  3. 服务器接收此数据包并检查所有用户帐户是否有任何名称与数据包的开头匹配。在这种情况下,应用程序会分离其中的 AES 部分,并尝试使用服务器自创建用户以来就知道的 SHA-256 哈希密码对其进行解密
  4. 如果解密成功,客户端和服务器将继续使用此 key 并进行良好的通信,而无需随时使用 Websocket 传输纯 AES key 。如果解密失败,用户会收到错误消息“凭据不正确”

这只是为了让您的信息能够帮助我;)因为我不知道 CryptoJS 到底如何处理它获得的数据,所以我从 Java 开始,最终得到了以下 Java 实现:

import io.cloudsystem.module.network.NetworkModule;
import io.netty.handler.codec.DecoderException;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64;

public class Crypto {

public static String getSHA256(String password) {

try {

MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(password.getBytes("UTF-8"));

byte[] dig = digest.digest();
String base = Base64.getEncoder().encodeToString(dig);
System.out.println("SHA-KEY (size): " + dig.length);
System.out.println("SHA-KEY (raw): " + new String(dig, "UTF-8"));
System.out.println("SHA-KEY (base): " + base);

return base;

} catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
NetworkModule.handleException(e);
}

return null;

}

public static String encrypt(String plainText, String key) {

try {

byte[] clean = plainText.getBytes("UTF-8");

int ivSize = 16;
byte[] iv = new byte[ivSize];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

System.out.println("ENC-IV (length): " + ivParameterSpec.getIV().length);
System.out.println("ENC-IV (raw): " + new String(ivParameterSpec.getIV(), "UTF-8"));

byte[] keyFetch = key.getBytes("UTF-8");
byte[] keyBytes = new byte[16];
System.arraycopy(keyFetch, 0, keyBytes, 0, keyBytes.length);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

System.out.println("ENC-KEY (length): " + secretKeySpec.getEncoded().length);
System.out.println("ENC-KEY (raw): " + new String(secretKeySpec.getEncoded(), "UTF-8"));

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(clean);

System.out.println("ENC-RAW (length): " + encrypted.length);
System.out.println("ENC-RAW (raw): " + new String(encrypted, "UTF-8"));

byte[] encryptedIVAndText = new byte[ivSize + encrypted.length];
System.arraycopy(iv, 0, encryptedIVAndText, 0, ivSize);
System.arraycopy(encrypted, 0, encryptedIVAndText, ivSize, encrypted.length);

System.out.println("ENC-FET (length): " + encryptedIVAndText.length);
System.out.println("ENC-FET (raw): " + new String(encryptedIVAndText, "UTF-8"));

String base = Base64.getEncoder().encodeToString(encryptedIVAndText);
System.out.println("ENC-BASE: " + base);
return base;

} catch (NoSuchAlgorithmException | NoSuchPaddingException | UnsupportedEncodingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
NetworkModule.handleException(e);
}

return null;

}

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

byte[] encryptedIvTextBytes = Base64.getDecoder().decode(encryped);
int ivSize = 16;
int keySize = 16;
try {
byte[] iv = new byte[ivSize];
System.arraycopy(encryptedIvTextBytes, 0, iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

System.out.println("DEC-IV (length): " + ivParameterSpec.getIV().length);
System.out.println("DEC-IV (raw): " + new String(ivParameterSpec.getIV(), "UTF-8"));

int encryptedSize = encryptedIvTextBytes.length - ivSize;
byte[] encryptedBytes = new byte[encryptedSize];
System.arraycopy(encryptedIvTextBytes, ivSize, encryptedBytes, 0, encryptedSize);

System.out.println("DEC-ENC (length): " + encryptedBytes.length);
System.out.println("DEC-ENC (raw): " + new String(encryptedBytes, "UTF-8"));

byte[] keyFetch = key.getBytes();
byte[] keyBytes = new byte[keySize];
System.arraycopy(keyFetch, 0, keyBytes, 0, keyBytes.length);
SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");

System.out.println("DEC-KEY (length): " + secretKeySpec.getEncoded().length);
System.out.println("DEC-KEY (raw): " + new String(secretKeySpec.getEncoded(), "UTF-8"));

Cipher cipherDecrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipherDecrypt.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decrypted = cipherDecrypt.doFinal(encryptedBytes);

System.out.println("DEC (length): " + decrypted.length);
System.out.println("DEC: " + new String(decrypted));

return new String(decrypted);

} catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) {
NetworkModule.handleException(e);
}

return null;

}

}

对于代码中的误导性填充,顺便说一句,我们深表歉意。示例场景可能如下所示:

SHA-KEY (size): 32
SHA-KEY (raw): �F os���>P�`��o�8e5*gf�

SHA-KEY (base): 4kYAb3O+EQTB9hE+UJxg9rNv8AQ4ZTUeKmdmzxoKAJE=
ENC-IV (length): 16
ENC-IV (raw): �I?dm�@�ܹTa؞�
ENC-KEY (length): 16
ENC-KEY (raw): 4kYAb3O+EQTB9hE+
ENC-RAW (length): 16
ENC-RAW (raw): B;��\`A0��z��
ENC-FET (length): 32
ENC-FET (raw): �I?dm�@�ܹTa؞�B;��\`A0��z��
ENC-BASE: FfVJP2RtuED53LlUYdie9EI7uJITXGBBMN7OepQeAqU=
DEC-IV (length): 16
DEC-IV (raw): �I?dm�@�ܹTa؞�
DEC-ENC (length): 16
DEC-ENC (raw): B;��\`A0��z��
DEC-KEY (length): 16
DEC-KEY (raw): 4kYAb3O+EQTB9hE+
DEC (length): 10
DEC: helloworld

我的结果:加密和解密在java中有效。现在我需要将它实现为javascript。经过大量时间的测试、编码、解码,并被 UTF16(JS 默认)和 UTF8(Java 字符集)的差异所困扰,我最终得到了与我在 java 中使用以下代码得到的相同的值:

var key = new Buffer("4kYAb3O+EQTB9hE+UJxg9rNv8AQ4ZTUeKmdmzxoKAJE=").subarray(0, 16)
var cryptobase = "FfVJP2RtuED53LlUYdie9EI7uJITXGBBMN7OepQeAqU="
varr crypto = new Buffer(cryptobase, 'base64')
var iv = crypto.subarray(0, 16)
var text = crypto.subarray(16, crypto.length)

console.log("Key: " + new TextDecoder("utf-8").decode(key))
console.log("IV: " + new TextDecoder("utf-8").decode(iv))
console.log("Text: " + new TextDecoder("utf-8").decode(text))

console.log(CryptoJS.AES.decrypt(text, key, {iv: iv}).toString(CryptoJS.enc.Utf8))

结果如下:

Key: 4kYAb3O+EQTB9hE+
IV: �I?dm�@�ܹTa؞�
Text: B;��\`A0��z��

与我们在 Java 端解密时得到的值完全相同。除了一个:CryptoJS 结果。这是“”(空字符串)现在我的大问题是:现在如何继续,我可以使用 CryptoJS 吗?我是否做错了什么、不安全或不安全的事情?

请认识:

  • 我见过https://github.com/mpetersen/aes-example但这对我不起作用,我想使用我自己的 key 。又测试了一下。使用 pbkdf2 仍然有不同的值

  • 我不想使用 AES-256,因为我不想要求软件用户安装 JCE

  • 我知道 SHA-256 不是对密码进行哈希处理的方法。我在生产中使用 pbkdf2

最佳答案

在实现 https://github.com/mpetersen/aes-example 中的 aes 示例后,我得到了它的工作如源代码中所示,而不是描述中。我将其修改为在一个字符串中传递数据,因此您只需要密码和 key 即可进行加密/解密。这是我的源代码(mpetersens aes-example 的修改版本):

JavaScript:

const CryptoJS = require("crypto-js")
var Crypto = new AES()

function AES() {}

AES.prototype.generateKey = function(salt, passPhrase) {
var key = CryptoJS.PBKDF2(passPhrase, CryptoJS.enc.Hex.parse(salt), { keySize: 4, iterations: 1000 });
return key;
}

AES.prototype.encrypt = function(password, message) {
var salt = CryptoJS.lib.WordArray.random(128/8).toString(CryptoJS.enc.Hex)
var iv = CryptoJS.lib.WordArray.random(128/8).toString(CryptoJS.enc.Hex)
var encrypted = CryptoJS.AES.encrypt(message, this.generateKey(salt, password), { iv: CryptoJS.enc.Hex.parse(iv) })
var base64 = encrypted.ciphertext.toString(CryptoJS.enc.Base64)
return salt + base64.substring(0, base64.length-2) + iv
}

AES.prototype.decrypt = function(password, message) {
var salt = message.substring(0, 32)
var iv = message.substring(message.length-32, message.length)
var cipherParams = CryptoJS.lib.CipherParams.create({
ciphertext: CryptoJS.enc.Base64.parse(message.substring(32, message.length-32) + "==")
});
var decrypted = CryptoJS.AES.decrypt(cipherParams, this.generateKey(salt, password), { iv: CryptoJS.enc.Hex.parse(iv) })
return decrypted.toString(CryptoJS.enc.Utf8)
}

Java:

private static final char[] HEX = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'b', 'c', 'D', 'e', 'F'};
private static Cipher cipher;

public static void init() {

try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
NetworkModule.handleException(e);
}

}

public static String encrypt(String password, String message) {

try {

String salt = random(16);
String iv = random(16);
SecretKey key = generateKey(salt, password);
byte[] encrypted = doFinal(Cipher.ENCRYPT_MODE, key, iv, message.getBytes("UTF-8"));
String code = Base64.getEncoder().encodeToString(encrypted);
return salt + code.substring(0, code.length() - 2) + iv;

} catch (UnsupportedEncodingException e) {
NetworkModule.handleException(e);
return null;
}

}

public static String decrypt(String password, String message) {

try {

String salt = message.substring(0, 32);
String iv = message.substring(message.length() - 32, message.length());
String base = message = message.substring(32, message.length() - 32) + "==";
SecretKey key = generateKey(salt, password);
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, Base64.getDecoder().decode(base));
return new String(decrypted, "UTF-8");

} catch (UnsupportedEncodingException e) {
NetworkModule.handleException(e);
return null;
}

}

private static byte[] doFinal(int encryptMode, SecretKey key, String iv, byte[] bytes) {

try {

cipher.init(encryptMode, key, new IvParameterSpec(hex(iv)));
return cipher.doFinal(bytes);

} catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException e) {
NetworkModule.handleException(e);
return null;
}

}

private static SecretKey generateKey(String salt, String passphrase) {

try {

SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), 1000, 128);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
return key;

} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
NetworkModule.handleException(e);
return null;
}

}

private static String random(int length) {
byte[] salt = new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}

private static String hex(byte[] data) {

int l = data.length;
char[] out = new char[l << 1];
int i = 0;

for (int var5 = 0; i < l; ++i) {
out[var5++] = HEX[(240 & data[i]) >>> 4];
out[var5++] = HEX[15 & data[i]];
}

return new String(out);

}

private static byte[] hex(String hex) {

char[] data = hex.toCharArray();
int len = data.length;

if ((len & 1) != 0) {
return null;
} else {

byte[] out = new byte[len >> 1];
int i = 0;

for (int j = 0; j < len; ++i) {

int f = Character.digit(data[j], 16) << 4;
++j;
f |= Character.digit(data[j], 16);
++j;
out[i] = (byte) (f & 255);

}

return out;

}

}

关于javascript - 在 Java 和 JavaScript 之间实现 AES,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48159345/

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