2012-07-10 54 views
1

如何(使用JavaServerPages)可予解密用PL/SQL解密在JSP

dbms_obfuscation_toolkit.DESEncrypt 

的關鍵是已知的加密的數據。我是JSP新手,請解釋你的答案。

+0

[JSP代碼解密dbms_obfuscation_toolkit.DESEncrypt oracle]的可能的重複(http://stackoverflow.com/questions/11393397/jsp-code-to-decrypt-dbms-obfuscation-toolkit-desencrypt-oracle),另請參閱:http://stackoverflow.com/questions/115503 – 2012-07-10 07:18:03

回答

0

此加密方法dbms_obfuscation_toolkit.DESEncrypt允許您輸入密碼,或者您只能以8個字符的倍數表示字符串。所以,下面我已經編寫了一個樣本JSP頁面,顯示ü一個字符串的加密和解密..

先決條件:該代碼使用庫org.apache.commons.codec.binary,下載org.apache .commons.codec.binary 1.5二進制文件並將其放在lib文件夾中。

<%@ page import="java.io.*" %> 
<%@ page import="java.security.*" %> 
<%@ page import="javax.crypto.*" %> 
<%@ page import="javax.crypto.spec.*" %> 
<%@ page import="java.lang.*" %> 
<%@ page import="org.apache.commons.codec.binary.*" %> 

    <HTML> 
    <HEAD> 
    <TITLE> Cheers! </TITLE> 
    </HEAD> 
    <BODY> 

    <% 
     String algorithm1 = "DES";//magical mystery constant 
     String algorithm2 = "DES/CBC/NoPadding";//magical mystery constant 
     IvParameterSpec iv = new IvParameterSpec(new byte [] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });//magical mystery constant 
     Cipher cipher; 
     SecretKey key; 
     String k="12345abc"; //Just a random pick for testing 
     key = new SecretKeySpec(k.getBytes("UTF-8"), algorithm1); 
     cipher = Cipher.getInstance(algorithm2); 

     String str="test4abc"; //Test String, 8 characters 

     cipher.init(Cipher.ENCRYPT_MODE, key, iv); //normally you could leave out the IvParameterSpec argument, but not with Oracle 

     byte[] bytes=str.getBytes("UTF-8"); 

     byte[] encrypted = cipher.doFinal(bytes); 

     String encoded = new String(Hex.encodeHex(encrypted)); 
     out.println("Encrypted/Encoded: \"" + encoded + "\""); 



     cipher.init(Cipher.DECRYPT_MODE, key, iv);  

     //byte [] decoded = org.apache.commons.codec.binary.Hex.decodeHex(encoded.toCharArray()); 
     byte [] decoded = Hex.decodeHex(encoded.toCharArray()); 

     String decrypted = new String (cipher.doFinal(decoded)); 
     out.println("DECRYPTED: \"" + decrypted + "\""); 
    } 

    %> 
    </BODY> 
    </HTML> 

這應該可能解決您的問題。 乾杯!

+0

工作就像一個魅力!謝謝! – Shahzeb 2012-07-10 07:32:22