本文目錄一覽:
- 1、js怎麼解密,js解密工具js怎麼查看這些代碼麻煩給解決一下
- 2、JS解密eval(function(p,a,c,k,e,d)
- 3、如何實現用javascript實現rsa加解密
- 4、JS解密window[“\x65\x76\x61\x6c”](function
js怎麼解密,js解密工具js怎麼查看這些代碼麻煩給解決一下
首先你的加密後的代碼有誤,無法執行,應將return(c35?String.fromCharCode(c+29):
修改為return(ca?””:e(parseInt(c/a)))+((c=c%a)35?String.fromCharCode(c+29):
c和35之間的部分你沒複製出來
這種加密的演算法的解密方法很簡單
html
body
div id=”test”/div
script type=”text/javascript”
document.getElementById(‘test’).innerHTML = 將需要破解的代碼全部複製過來,注意你原來的代碼不對,請通過替換先將上面說的那個錯誤改正
/script
/body
/html
將上面的代碼保存成html文件,打開此文件即可看到加密前的內容,由於你的問題解密後是廣告語,所以不在此處給出,自己試一下
JS解密eval(function(p,a,c,k,e,d)
把開頭的eval替換為alert,然後執行,可以在彈出的對話框里得到解密結果。。。
如何實現用javascript實現rsa加解密
服務端生成公鑰與私鑰,保存。
客戶端在請求到登錄頁面後,隨機生成一字元串。
後此隨機字元串作為密鑰加密密碼,再用從服務端獲取到的公鑰加密生成的隨機字元串
將此兩段密文傳入服務端,服務端用私鑰解出隨機字元串,再用此私鑰解出加密的密文。這其中有一個關鍵是解決服務端的公鑰,傳入客戶端,客戶端用此公鑰加密字元串後,後又能在服務端用私鑰解出。
步驟:
服務端的RSA Java實現:
/**
*
*/
package com.sunsoft.struts.util;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
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.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import javax.crypto.Cipher;
/**
* RSA 工具類。提供加密,解密,生成密鑰對等方法。
* 需要到
下載bcprov-jdk14-123.jar。
*
*/
public class RSAUtil {
/**
* * 生成密鑰對 *
*
* @return KeyPair *
* @throws EncryptException
*/
public static KeyPair generateKeyPair() throws Exception {
try {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(“RSA”,
new org.bouncycastle.jce.provider.BouncyCastleProvider());
final int KEY_SIZE = 1024;// 沒什麼好說的了,這個值關係到塊加密的大小,可以更改,但是不要太大,否則效率會低
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.generateKeyPair();
saveKeyPair(keyPair);
return keyPair;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
public static KeyPair getKeyPair()throws Exception{
FileInputStream fis = new FileInputStream(“C:/RSAKey.txt”);
ObjectInputStream oos = new ObjectInputStream(fis);
KeyPair kp= (KeyPair) oos.readObject();
oos.close();
fis.close();
return kp;
}
public static void saveKeyPair(KeyPair kp)throws Exception{
FileOutputStream fos = new FileOutputStream(“C:/RSAKey.txt”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
//生成密鑰
oos.writeObject(kp);
oos.close();
fos.close();
}
/**
* * 生成公鑰 *
*
* @param modulus *
* @param publicExponent *
* @return RSAPublicKey *
* @throws Exception
*/
public static RSAPublicKey generateRSAPublicKey(byte[] modulus,
byte[] publicExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance(“RSA”,
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
modulus), new BigInteger(publicExponent));
try {
return (RSAPublicKey) keyFac.generatePublic(pubKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * 生成私鑰 *
*
* @param modulus *
* @param privateExponent *
* @return RSAPrivateKey *
* @throws Exception
*/
public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus,
byte[] privateExponent) throws Exception {
KeyFactory keyFac = null;
try {
keyFac = KeyFactory.getInstance(“RSA”,
new org.bouncycastle.jce.provider.BouncyCastleProvider());
} catch (NoSuchAlgorithmException ex) {
throw new Exception(ex.getMessage());
}
RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
modulus), new BigInteger(privateExponent));
try {
return (RSAPrivateKey) keyFac.generatePrivate(priKeySpec);
} catch (InvalidKeySpecException ex) {
throw new Exception(ex.getMessage());
}
}
/**
* * 加密 *
*
* @param key
* 加密的密鑰 *
* @param data
* 待加密的明文數據 *
* @return 加密後的數據 *
* @throws Exception
*/
public static byte[] encrypt(PublicKey pk, byte[] data) throws Exception {
try {
Cipher cipher = Cipher.getInstance(“RSA”,
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();// 獲得加密塊大小,如:加密前數據為128個byte,而key_size=1024
// 加密塊大小為127
// byte,加密後為128個byte;因此共有2個加密塊,第一個127
// byte第二個為1個byte
int outputSize = cipher.getOutputSize(data.length);// 獲得加密塊加密後塊大小
int leavedSize = data.length % blockSize;
int blocksSize = leavedSize != 0 ? data.length / blockSize + 1
: data.length / blockSize;
byte[] raw = new byte[outputSize * blocksSize];
int i = 0;
while (data.length – i * blockSize 0) {
if (data.length – i * blockSize blockSize)
cipher.doFinal(data, i * blockSize, blockSize, raw, i
* outputSize);
else
cipher.doFinal(data, i * blockSize, data.length – i
* blockSize, raw, i * outputSize);
// 這裡面doUpdate方法不可用,查看源代碼後發現每次doUpdate後並沒有什麼實際動作除了把byte[]放到
// ByteArrayOutputStream中,而最後doFinal的時候才將所有的byte[]進行加密,可是到了此時加密塊大小很可能已經超出了
// OutputSize所以只好用dofinal方法。
i++;
}
return raw;
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * 解密 *
*
* @param key
* 解密的密鑰 *
* @param raw
* 已經加密的數據 *
* @return 解密後的明文 *
* @throws Exception
*/
public static byte[] decrypt(PrivateKey pk, byte[] raw) throws Exception {
try {
Cipher cipher = Cipher.getInstance(“RSA”,
new org.bouncycastle.jce.provider.BouncyCastleProvider());
cipher.init(cipher.DECRYPT_MODE, pk);
int blockSize = cipher.getBlockSize();
ByteArrayOutputStream bout = new ByteArrayOutputStream(64);
int j = 0;
while (raw.length – j * blockSize 0) {
bout.write(cipher.doFinal(raw, j * blockSize, blockSize));
j++;
}
return bout.toByteArray();
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
/**
* * *
*
* @param args *
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RSAPublicKey rsap = (RSAPublicKey) RSAUtil.generateKeyPair().getPublic();
String test = “hello world”;
byte[] en_test = encrypt(getKeyPair().getPublic(),test.getBytes());
byte[] de_test = decrypt(getKeyPair().getPrivate(),en_test);
System.out.println(new String(de_test));
}
}
測試頁面IndexAction.java:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.sunsoft.struts.action;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.sunsoft.struts.util.RSAUtil;
/**
* MyEclipse Struts
* Creation date: 06-28-2008
*
* XDoclet definition:
* @struts.action validate=”true”
*/
public class IndexAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
RSAPublicKey rsap = (RSAPublicKey) RSAUtil.getKeyPair().getPublic();
String module = rsap.getModulus().toString(16);
String empoent = rsap.getPublicExponent().toString(16);
System.out.println(“module”);
System.out.println(module);
System.out.println(“empoent”);
System.out.println(empoent);
request.setAttribute(“m”, module);
request.setAttribute(“e”, empoent);
return mapping.findForward(“login”);
}
}
通過此action進入登錄頁面,並傳入公鑰的 Modulus 與PublicExponent的hex編碼形式。
JS解密window[“\x65\x76\x61\x6c”](function
[“\x65\x76\x61\x6c”]這個是正則表達式,代表的是十六進位的ASCII 編碼 轉換的結果是eval
eval在js中的用法如下
eval 方法
檢查 JScript 代碼並執行.
eval(codeString)
必選項 codestring 參數是包含有效 JScript 代碼的字元串值。這個字元串將由 JScript 分析器進行分析和執行。
說明
eval 函數允許 JScript 源代碼的動態執行。例如,下面的代碼創建了一個包含 Date 對象的新變數 mydate :
eval(“var mydate = new Date();”);
傳遞給 eval 方法的代碼執行時的上下文和調用 eval 方法的一樣.
原創文章,作者:PGCPG,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/315920.html