1

如何(使用 JavaServerPages)解密使用 PL/SQL 加密的数据

dbms_obfuscation_toolkit.DESEncrypt

钥匙是已知的。我是 JSP 的新手,所以请解释一下你的答案。

4

1 回答 1

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>

这应该可以解决您的问题。干杯!

于 2012-07-10T07:26:11.870 回答