gpt4 book ai didi

java - 在javascript中加密字符串并在java中解密

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

我想知道是否有人知道有什么库可以在 javascript 中进行加密并在 java 中进行解密。我已经尝试了很多API,但是在java中没有得到相同的值。
我想要公钥-私钥加密,因此尝试使用RSA。我用过的很少有:

  1. http://www-cs-students.stanford.edu/~tjw/jsbn/
  2. http://ats.oka.nu/titaniumcore/js/crypto/readme.txt
  3. http://www.ohdave.com/rsa/

我检查的很少,javascript将字符串分成小块,然后对它们进行加密,这使得java和javascript中的密文不同。我编辑了 javascript 代码以将字符串作为一个整体使用,但没有成功。

我也尝试将 html 页面的字符集设置为 utf-8 但它也不起作用。我成功地加密了像“K”这样的单个字符串,以便正确加密和解​​密,这让我认为通过将 JavaScript 中的字符串分成小块来加密字符串存在问题(我检查过,但将其加密为小块时失败)所有的)。

我的java实现是:

BigInteger d = new BigInteger("1f3fac65c4ae222e3a3074dd4c38fbb72c0705c4bbac0385b867c12c25a44e01", 16);
BigInteger e = new BigInteger("65537");
BigInteger N = new BigInteger("b42e91fbca364cf2a125aec67ffbdab624fd401100c40ea05189ba34d1028b0d", 16);
String messageToEncrypt = "kishor";
byte [] messageByte = messageToEncrypt.getBytes();
BigInteger message = new BigInteger(messageByte);
//Encrypting and Decrypting messages
//Encrypt a message using N and e:
BigInteger ciphertext = message.modPow(e, N);
//Decrypt the message using N and d:
BigInteger plaintext = ciphertext.modPow(d, N);
byte[] plainTextByte = plaintext.toByteArray();
String decryptMessage = new String(plainTextByte);
/*System.out.println("p : " + p);
System.out.println("q : " + q);*/
System.out.println("N : " + N.toString(16));
System.out.println("e : " + e.toString(16));
System.out.println("d : " + d.toString(16));
/*System.out.println("PhiN : " + PhiN);*/
System.out.println("ciphertext : " + ciphertext.toString(16));
System.out.println("decryptMessage : " + decryptMessage);
}

请告诉我是否可能,因为我已经搜索了很多问题(在 stackoverflow 本身)但无法找到解决方案。

最佳答案

尝试乱码 AES(JS Lib) https://github.com/mdp/gibberish-aes/

关于java - 在javascript中加密字符串并在java中解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11737390/

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