未捕获的类型错误:无法读取未定义的属性“加密" [英] Uncaught TypeError: Cannot read property 'encrypt' of undefined
本文介绍了未捕获的类型错误:无法读取未定义的属性“加密"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 JavaScript 端使用 RSA_OAEP_SHA256 进行加密.我正在使用第三方库 asmcrypto.js :
I want to encrypt using RSA_OAEP_SHA256 on the JavaScript side. I am using the third party library asmcrypto.js :
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/asmCrypto/0.22.0/asmcrypto.js"></script>
var encrypted = asmCrypto.RSA_OAEP_SHA256.encrypt(stringToBeEncrypted, pubkey, "");
获取错误:未捕获的类型错误:无法读取未定义的属性加密"
getting error: Uncaught TypeError: Cannot read property 'encrypt' of undefined
有人知道解决方案或任何有帮助的例子吗?谢谢.
Does anybody know the solution or any example that would help? Thanks.
推荐答案
所以我终于找到了解决方案,我在这里分享,以便对某人有所帮助.
So finally I found the solution, I am sharing it here so that it will be helpful to someone.
- 我更改了库并使用了 jsencrypt.min.js JSEncrypt它是支持 OAEP 填充的更新版本.
- JavaScript 代码:
- I changed the library and used jsencrypt.min.js JSEncrypt It is an updated one that supports OAEP padding.
- JavaScript Code :
<script type="text/javascript" src="/RSO/includes/jsencrypt.min.js"></script>
function get(tmpSubmit)
{
<%
String key = BouncyCastlePemUtils.readPublicKey();
System.out.println("readPublicKey: " + key);
%>
alert('<%=key %>');
var publicKey = '<%=key %>';
var password = "Dhiraj is the author";
var RSAEncrypt = new JSEncrypt();
RSAEncrypt.setPublicKey(publicKey);
var encryptedPass = RSAEncrypt.encrypt(password, true);
document.write("encryptedPass: "+encryptedPass)
}
- 使用 openssl 生成公钥和私钥,请查看下面的链接以获取命令.
- 用于加密和解密数据的 Java 类:
package com.demo.rsa;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.xml.bind.DatatypeConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.demo.util.CommonProperties;
public class PEMDemo implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public String encrypt(String plainText, RSAPublicKey publicKey) throws Exception {
Cipher cipher = getCipher();
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
return DatatypeConverter.printBase64Binary(encryptedText);
}
public String decrypt(String cipherText, RSAPrivateKey privateKey) throws Exception {
byte[] cipherBytes;
cipherBytes = DatatypeConverter.parseBase64Binary(cipherText);
Cipher cipher = getCipher();
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedText = cipher.doFinal(cipherBytes);
return new String(decryptedText, StandardCharsets.UTF_8);
}
private Cipher getCipher() throws Exception {
Security.addProvider(new BouncyCastleProvider());
return Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", new BouncyCastleProvider());
}
public static RSAPublicKey getPublicKey(){
RSAPublicKey publicKey = null;
try {
File publicKeyFile = new File(CommonProperties.propBag.getProperty("RSA_CONFIG_PATH")+"public_key.pem");
publicKey = BouncyCastlePemUtils.readX509PublicKey(publicKeyFile);
} catch (InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
return publicKey;
}
public static RSAPrivateKey getPrivateKey(){
RSAPrivateKey privateKey = null;
try {
File privateKeyFile = new File(CommonProperties.propBag.getProperty("RSA_CONFIG_PATH")+"private_key.pem");
privateKey = BouncyCastlePemUtils.readPKCS8PrivateKey(privateKeyFile);
} catch (InvalidKeySpecException | NoSuchAlgorithmException | IOException e) {
e.printStackTrace();
}
return privateKey;
}
public static void main(String[] args){
PEMDemo rsaEncriptDecrypt = new PEMDemo();
String encrypted;
try {
encrypted = rsaEncriptDecrypt.encrypt("This is plain text.", getPublicKey());
System.out.println("encrypted: " + encrypted);
// JS Encrypted cipher text
encrypted = "voxgNKCtKy6wGL/g9oi/jUqwm4lnT+Ais4ZaJ5OwJ4gozjTHl3L7yabB04tyV9UmuxfGb6EywZvrpuZRIKtqWPzO+UgW0A+5g9nPjXtDPI0qMzuv7i1E7WVjM4isJBwOC4yPdttXi4h/vbzOaR5J5r8mbyHdnxkqtuDn3o5jXOM=";
String decrypted = rsaEncriptDecrypt.decrypt(encrypted, getPrivateKey());
System.out.println("decrypted: " + decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 用于从 .PEM 文件读取公钥和私钥的实用程序类
package com.demo.rsa;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemReader;
import com.demo.util.CommonProperties;
public class BouncyCastlePemUtils {
public static RSAPublicKey readX509PublicKey(File file) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
KeyFactory factory = KeyFactory.getInstance("RSA");
try (FileReader keyReader = new FileReader(file);
PemReader pemReader = new PemReader(keyReader)) {
PemObject pemObject = pemReader.readPemObject();
byte[] content = pemObject.getContent();
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(content);
return (RSAPublicKey) factory.generatePublic(pubKeySpec);
}
}
public static RSAPrivateKey readPKCS8PrivateKey(File file) throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
KeyFactory factory = KeyFactory.getInstance("RSA");
try (FileReader keyReader = new FileReader(file);
PemReader pemReader = new PemReader(keyReader)) {
PemObject pemObject = pemReader.readPemObject();
byte[] content = pemObject.getContent();
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(content);
return (RSAPrivateKey) factory.generatePrivate(privKeySpec);
}
}
public static String readPublicKey() throws IOException {
File publicKeyFile = new File(CommonProperties.propBag.getProperty("RSA_CONFIG_PATH")+"public_key.pem");
StringBuilder st = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(publicKeyFile))){
String s = st.toString();
while ((s = br.readLine()) != null)
st.append(s);
}
return st.toString();
}
}
- Maven 依赖:
6. Maven Dependancy:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
这篇关于未捕获的类型错误:无法读取未定义的属性“加密"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文