웹 페이지 는 js 로 민감 한 정 보 를 암호 화하 고 자바 단 복호화 합 니 다.
10142 단어 자바
우선 전단 js 암호 화 도구 가 필요 합 니 다. 이 도 구 는 JSencrypt 이 라 고 합 니 다.
공식 홈 페이지:http://travistidwell.com/jsencrypt/
다운로드 주소:https://github.com/travist/jsencrypt/archive/master.zip
패키지 안에 jsencrypt. min. js 파일 이 있 습 니 다. 바로 우리 가 가 져 올 파일 입 니 다. 먼저 jsp 페이지 에서 이 js 파일 을 가 져 옵 니 다.
var encrypt = new JSEncrypt();
encrypt.setPublicKey("<%=RSAUtil.publicKey%>");
console.log(" 123456 :"+encrypt.encrypt("123456"));
자바 단 복호화:
System.out.println( RSACoder.decryptBase64AndRSAByPrivateKey(" ") );
RSAUtil 은 자바 도구 클래스 입 니 다:
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author zqz 20180818
*/
public class RSACoder {
private static Logger logger = LoggerFactory.getLogger(RSACoder.class);
public static final String KEY_ALGORITHM = "RSA";
public static final String SIGNATURE_ALGORITHM = "MD5withRSA";
private static final String PUBLIC_KEY = "RSAPublicKey";
private static final String PRIVATE_KEY = "RSAPrivateKey";
/** , key, **/
public static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKsSDFQDGtwz9qRQC6PLuShXMfaRUY9Or4CwqQTiTiukU8rTjmk6xcWGarieJNTjLkGRUbTjzkSzcDtGt1AItLUbS0hZvsd4K7BsrkT67/oNI3xZ9f5mw8SPfvyZnnMvNTy9KkkssMz/TnXrZLg4FI3U5yNRZQVOUVSPIYcGFc1Z6zQIFwIDAQAB";
/** , key, **/
public static String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmASDFScAgEAAoGBAMa3DP2pFALo8u5KFcx9pFRj06vgLCpBOJOK6RTytOOaTrFxYZquJ4k1OMuQSDdfffffRtOPORLNwO0a3UAi0tRtLSFm+BODgrsGyuRPrv+g0jfFn1/mbDxI9+/Jmecy81PL0qSSywzP9OdetkuDgUjdTnI1FlBU5RVI8hhwYVzVnrNAgXAgMBAAECgYAddPctBwpbYBLa9gqDNMDObbnsCaAiZ2auoHHq1AuyjaZKsYcJefWMZmJT8sScq1A88vFWca/td7cW4VZVXCVNASLC0TWUgGqe42NArT+2A9UiKJBO83JEdABo1UNlbhS2PdQRutsq8j1JyCwBehF7GcIlPbC0TcCKy1DMl2u5qQJBAPRDA3JwVHOs/eXkiQRjbpWEKZX2Fc6kbWIrf96oXQi7aRsrKVK/g4qgNNaNcCYn/VksCLfHbe74qed5sVBd1IMCQQDQQ7Tyyl2wshggxL+imuAp0lSjqYtFfD/9260voHPNfYX2LI8nWjwtdlKFLmKk0ErsKxd9aOxuRYLycRGbl7HdAkEA26IaSl+JAI6PqpSpQ4Ho4JZ7EjW5fBrLpFc63koDhaOGqgBQYvCD5jWR7U42hoi0vObkW+P+DMAZfmMlNf944wJAPe+ExpeMGVo9drFMVG0+sPFOHoXzVyzGUxabgIh1J+1mg3lo8UkSkGqZy+WpjF4tieKez7r+ZSTi9nrNw7nvOQJAM/hx/sLxeWfqfI92Etuq6HqkQ3ciVthkOPuMusGyX5rjl/g4vimrpJ43vH08vyyfsiDf5AK3mVuUfsNZAicyrg==";
static {
init();
}
public static void init() {
/**
* key
*/
// Map keyMap;
// try {
// keyMap = RSACoder.initKey();
// publicKey = RSACoder.getPublicKey(keyMap);
// privateKey = RSACoder.getPrivateKey(keyMap);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// logger.error(" js :",e);
// }
logger.info(" :
\r" + publicKey);
logger.info(" :
\r" + privateKey);
}
public static byte[] decryptBASE64(String key) {
return Base64.decodeBase64(key);
}
public static String encryptBASE64(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
public static byte[] decryptBase64AndRSAByPrivateKey(String data)throws Exception{
return decryptByPrivateKey(RSACoder.decryptBASE64(data),privateKey);
}
/**
*
* @param data base64 ;
* @param key base64 ;
* @return
* @throws Exception
*/
public static byte[] decryptAndBase64ByPrivateKey(String data, String key) throws Exception{
return decryptByPrivateKey(RSACoder.decryptBASE64(data),key);
}
/**
*
* base64
* @param data
* @param key
* @return
* @throws Exception
*/
public static String encryptAndBase64ByPublicKey(String data,String key) throws Exception {
return encryptBASE64(encryptByPublicKey(data,key));
}
/**
*
*
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static String sign(byte[] data, String privateKey) throws Exception {
// base64
byte[] keyBytes = decryptBASE64(privateKey);
// PKCS8EncodedKeySpec
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
// KEY_ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//
PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
//
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(priKey);
signature.update(data);
return encryptBASE64(signature.sign());
}
/**
*
*
* @param data
* @param publicKey
* @param sign
* @return true false
* @throws Exception
*/
public static boolean verify(byte[] data, String publicKey, String sign)
throws Exception {
// base64
byte[] keyBytes = decryptBASE64(publicKey);
// X509EncodedKeySpec
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
// KEY_ALGORITHM
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
//
PublicKey pubKey = keyFactory.generatePublic(keySpec);
Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initVerify(pubKey);
signature.update(data);
//
return signature.verify(decryptBASE64(sign));
}
/**
*
* @param data
* @param privateKey
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(byte[] data, Key privateKey) throws Exception{
//
Cipher cipher = Cipher.getInstance(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
public static byte[] decryptByPrivateKey(byte[] data, String key) throws Exception{
//
byte[] keyBytes = decryptBASE64(key);
//
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
return decryptByPrivateKey(data,privateKey);
}
/**
*
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPrivateKey(String data, String key)
throws Exception {
return decryptByPrivateKey(decryptBASE64(data),key);
}
/**
*
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptByPublicKey(byte[] data, String key)
throws Exception {
//
byte[] keyBytes = decryptBASE64(key);
//
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
*
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPublicKey(String data, String key)
throws Exception {
//
byte[] keyBytes = decryptBASE64(key);
//
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key publicKey = keyFactory.generatePublic(x509KeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data.getBytes());
}
/**
*
*
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByPrivateKey(byte[] data, String key)
throws Exception {
//
byte[] keyBytes = decryptBASE64(key);
//
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
//
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
/**
*
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPrivateKey(Map keyMap)
throws Exception {
Key key = (Key) keyMap.get(PRIVATE_KEY);
return encryptBASE64(key.getEncoded());
}
/**
*
*
* @param keyMap
* @return
* @throws Exception
*/
public static String getPublicKey(Map keyMap)
throws Exception {
Key key = keyMap.get(PUBLIC_KEY);
return encryptBASE64(key.getEncoded());
}
/**
*
*
* @return
* @throws NoSuchAlgorithmException
* @throws Exception
*/
public static Map initKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator
.getInstance(KEY_ALGORITHM);
keyPairGen.initialize(1024);
KeyPair keyPair = keyPairGen.generateKeyPair();
Map keyMap = new HashMap(2);
keyMap.put(PUBLIC_KEY, keyPair.getPublic());//
keyMap.put(PRIVATE_KEY, keyPair.getPrivate());//
return keyMap;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.