熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

用javascript與java進行RSA加密與解密

2022-06-13   來源: Java核心技術 

  這幾天一直做安全登錄網上查了好多資料不盡如意

  具體實現思路如下

  服務端生成公鑰與私鑰保存

  客戶端在請求到登錄頁面後隨機生成一字符串

  後此隨機字符串作為密鑰加密密碼再用從服務端獲取到的公鑰加密生成的隨機字符串

  將此兩段密文傳入服務端服務端用私鑰解出隨機字符串再用此私鑰解出加密的密文

  這其中有一個關鍵是解決服務端的公鑰傳入客戶端客戶端用此公鑰加密字符串後後又能在服務端用私鑰解出

  此文即為實現此步而作

  加密算法為RSA

  服務端的RSA  java實現

  /**
     *
     */
    package comsunsoftstrutsutil;

  import javaioByteArrayOutputStream;
    import javaioFileInputStream;
    import javaioFileOutputStream;
    import javaioObjectInputStream;
    import javaioObjectOutputStream;
    import javamathBigInteger;
    import javasecurityKeyFactory;
    import javasecurityKeyPair;
    import javasecurityKeyPairGenerator;
    import javasecurityNoSuchAlgorithmException;
    import javasecurityPrivateKey;
    import javasecurityPublicKey;
    import javasecuritySecureRandom;
    import javasecurityinterfacesRSAPrivateKey;
    import javasecurityinterfacesRSAPublicKey;
    import javasecurityspecInvalidKeySpecException;
    import javasecurityspecRSAPrivateKeySpec;
    import javasecurityspecRSAPublicKeySpec;

  import javaxcryptoCipher;

  /**
     * RSA 工具類提供加密解密生成密鑰對等方法
     * 需要到下載bcprovjdkjar
     *
     */
    public class RSAUtil {
        /**
         * * 生成密鑰對 *
         *
         * @return KeyPair *
         * @throws EncryptException
         */
        public static KeyPair generateKeyPair() throws Exception {
            try {
                KeyPairGenerator keyPairGen = KeyPairGeneratorgetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                final int KEY_SIZE = ;// 沒什麼好說的了這個值關系到塊加密的大小可以更改但是不要太大否則效率會低
                keyPairGeninitialize(KEY_SIZE new SecureRandom());
                KeyPair keyPair = keyPairGengenerateKeyPair();
                saveKeyPair(keyPair);
                return keyPair;
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  public static KeyPair getKeyPair()throws Exception{
            FileInputStream fis = new FileInputStream(C:/RSAKeytxt);
             ObjectInputStream oos = new ObjectInputStream(fis);
             KeyPair kp= (KeyPair) oosreadObject();
             oosclose();
             fisclose();
             return kp;
        }

  public static void saveKeyPair(KeyPair kp)throws Exception{

  FileOutputStream fos = new FileOutputStream(C:/RSAKeytxt);
             ObjectOutputStream oos = new ObjectOutputStream(fos);
             //生成密鑰
             ooswriteObject(kp);
             oosclose();
             fosclose();
        }

  /**
         * * 生成公鑰 *
         *
         * @param modulus *
         * @param publicExponent *
         * @return RSAPublicKey *
         * @throws Exception
         */
        public static RSAPublicKey generateRSAPublicKey(byte[] modulus
                byte[] publicExponent) throws Exception {
            KeyFactory keyFac = null;
            try {
                keyFac = KeyFactorygetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
            } catch (NoSuchAlgorithmException ex) {
                throw new Exception(exgetMessage());
            }

  RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
                    modulus) new BigInteger(publicExponent));
            try {
                return (RSAPublicKey) keyFacgeneratePublic(pubKeySpec);
            } catch (InvalidKeySpecException ex) {
                throw new Exception(exgetMessage());
            }
        }

  /**
         * * 生成私鑰 *
         *
         * @param modulus *
         * @param privateExponent *
         * @return RSAPrivateKey *
         * @throws Exception
         */
        public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus
                byte[] privateExponent) throws Exception {
            KeyFactory keyFac = null;
            try {
                keyFac = KeyFactorygetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
            } catch (NoSuchAlgorithmException ex) {
                throw new Exception(exgetMessage());
            }

  RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
                    modulus) new BigInteger(privateExponent));
            try {
                return (RSAPrivateKey) keyFacgeneratePrivate(priKeySpec);
            } catch (InvalidKeySpecException ex) {
                throw new Exception(exgetMessage());
            }
        }

  /**
         * * 加密 *
         *
         * @param key
         *            加密的密鑰 *
         * @param data
         *            待加密的明文數據 *
         * @return 加密後的數據 *
         * @throws Exception
         */
 

  public static byte[] encrypt(PublicKey pk byte[] data) throws Exception {
            try {
                Cipher cipher = CiphergetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                cipherinit(CipherENCRYPT_MODE pk);
                int blockSize = ciphergetBlockSize();// 獲得加密塊大小加密前數據為個byte而key_size=
                // 加密塊大小為
                // byte加密後為個byte;因此共有個加密塊第一個
                // byte第二個為個byte
                int outputSize = ciphergetOutputSize(datalength);// 獲得加密塊加密後塊大小
                int leavedSize = datalength % blockSize;
                int blocksSize = leavedSize != ? datalength / blockSize +
                        : datalength / blockSize;
                byte[] raw = new byte[outputSize * blocksSize];
                int i = ;
                while (datalength i * blockSize > ) {
                    if (datalength i * blockSize > blockSize)
                        cipherdoFinal(data i * blockSize blockSize raw i
                                * outputSize);
                    else
                        cipherdoFinal(data i * blockSize datalength i
                                * blockSize raw i * outputSize);
                    // 這裡面doUpdate方法不可用查看源代碼後發現每次doUpdate後並沒有什麼實際動作除了把byte[]放到
                    // ByteArrayOutputStream中而最後doFinal的時候才將所有的byte[]進行加密可是到了此時加密塊大小很可能已經超出了
                    // OutputSize所以只好用dofinal方法

  i++;
                }
                return raw;
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  /**
         * * 解密 *
         *
         * @param key
         *            解密的密鑰 *
         * @param raw
         *            已經加密的數據 *
         * @return 解密後的明文 *
         * @throws Exception
         */
        public static byte[] decrypt(PrivateKey pk byte[] raw) throws Exception {
            try {
                Cipher cipher = CiphergetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                cipherinit(cipherDECRYPT_MODE pk);
                int blockSize = ciphergetBlockSize();
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                int j = ;

  while (rawlength j * blockSize > ) {
                    boutwrite(cipherdoFinal(raw j * blockSize blockSize));
                    j++;
                }
                return bouttoByteArray();
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  /**
         * * *
         *
         * @param args *
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
            RSAPublicKey rsap = (RSAPublicKey) RSAUtilgenerateKeyPair()getPublic();
            String test = hello world;
            byte[] en_test = encrypt(getKeyPair()getPublic()testgetBytes());
            byte[] de_test = decrypt(getKeyPair()getPrivate()en_test);
            Systemoutprintln(new String(de_test));
        }
    }

  /**
     *
     */
    package comsunsoftstrutsutil;

  import javaioByteArrayOutputStream;
    import javaioFileInputStream;
    import javaioFileOutputStream;
    import javaioObjectInputStream;
    import javaioObjectOutputStream;
    import javamathBigInteger;
    import javasecurityKeyFactory;
    import javasecurityKeyPair;
    import javasecurityKeyPairGenerator;
    import javasecurityNoSuchAlgorithmException;
    import javasecurityPrivateKey;
    import javasecurityPublicKey;
    import javasecuritySecureRandom;
    import javasecurityinterfacesRSAPrivateKey;
    import javasecurityinterfacesRSAPublicKey;
    import javasecurityspecInvalidKeySpecException;
    import javasecurityspecRSAPrivateKeySpec;
    import javasecurityspecRSAPublicKeySpec;

  import javaxcryptoCipher;

  /**
     * RSA 工具類提供加密解密生成密鑰對等方法
     * 需要到下載bcprovjdkjar
     *
     */
    public class RSAUtil {
        /**
         * * 生成密鑰對 *
         *
         * @return KeyPair *
         * @throws EncryptException
         */
        public static KeyPair generateKeyPair() throws Exception {
            try {
                KeyPairGenerator keyPairGen = KeyPairGeneratorgetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                final int KEY_SIZE = ;// 沒什麼好說的了這個值關系到塊加密的大小可以更改但是不要太大否則效率會低
                keyPairGeninitialize(KEY_SIZE new SecureRandom());
                KeyPair keyPair = keyPairGengenerateKeyPair();
                saveKeyPair(keyPair);
                return keyPair;
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  public static KeyPair getKeyPair()throws Exception{
            FileInputStream fis = new FileInputStream(C:/RSAKeytxt);
             ObjectInputStream oos = new ObjectInputStream(fis);
             KeyPair kp= (KeyPair) oosreadObject();
             oosclose();
             fisclose();
             return kp;
        }

  public static void saveKeyPair(KeyPair kp)throws Exception{

  FileOutputStream fos = new FileOutputStream(C:/RSAKeytxt);
             ObjectOutputStream oos = new ObjectOutputStream(fos);
             //生成密鑰
             ooswriteObject(kp);
             oosclose();
             fosclose();
        }

  /**
         * * 生成公鑰 *
         *
         * @param modulus *
         * @param publicExponent *
         * @return RSAPublicKey *
         * @throws Exception
         */
        public static RSAPublicKey generateRSAPublicKey(byte[] modulus
                byte[] publicExponent) throws Exception {
            KeyFactory keyFac = null;
            try {
                keyFac = KeyFactorygetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
            } catch (NoSuchAlgorithmException ex) {
                throw new Exception(exgetMessage());
            }

  RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(
                    modulus) new BigInteger(publicExponent));
            try {
                return (RSAPublicKey) keyFacgeneratePublic(pubKeySpec);
            } catch (InvalidKeySpecException ex) {
                throw new Exception(exgetMessage());
            }
        }

  /**
         * * 生成私鑰 *
         *
         * @param modulus *
         * @param privateExponent *
         * @return RSAPrivateKey *
         * @throws Exception
         */
        public static RSAPrivateKey generateRSAPrivateKey(byte[] modulus
                byte[] privateExponent) throws Exception {
            KeyFactory keyFac = null;
            try {
                keyFac = KeyFactorygetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
            } catch (NoSuchAlgorithmException ex) {
                throw new Exception(exgetMessage());
            }

  RSAPrivateKeySpec priKeySpec = new RSAPrivateKeySpec(new BigInteger(
                    modulus) new BigInteger(privateExponent));
            try {
                return (RSAPrivateKey) keyFacgeneratePrivate(priKeySpec);
            } catch (InvalidKeySpecException ex) {
                throw new Exception(exgetMessage());
            }
        }

  /**
         * * 加密 *
         *
         * @param key
         *            加密的密鑰 *
         * @param data
         *            待加密的明文數據 *
         * @return 加密後的數據 *
         * @throws Exception
         */
        public static byte[] encrypt(PublicKey pk byte[] data) throws Exception {
            try {
                Cipher cipher = CiphergetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                cipherinit(CipherENCRYPT_MODE pk);
                int blockSize = ciphergetBlockSize();// 獲得加密塊大小加密前數據為個byte而key_size=
                // 加密塊大小為
                // byte加密後為個byte;因此共有個加密塊第一個
                // byte第二個為個byte
                int outputSize = ciphergetOutputSize(datalength);// 獲得加密塊加密後塊大小
                int leavedSize = datalength % blockSize;
                int blocksSize = leavedSize != ? datalength / blockSize +
                        : datalength / blockSize;
                byte[] raw = new byte[outputSize * blocksSize];
                int i = ;
                while (datalength i * blockSize > ) {
                    if (datalength i * blockSize > blockSize)
                        cipherdoFinal(data i * blockSize blockSize raw i
                                * outputSize);
                    else
                        cipherdoFinal(data i * blockSize datalength i
                                * blockSize raw i * outputSize);

  // 這裡面doUpdate方法不可用查看源代碼後發現每次doUpdate後並沒有什麼實際動作除了把byte[]放到
                    // ByteArrayOutputStream中而最後doFinal的時候才將所有的byte[]進行加密可是到了此時加密塊大小很可能已經超出了
                    // OutputSize所以只好用dofinal方法

  i++;
                }
                return raw;
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  /**
         * * 解密 *
         *
         * @param key
         *            解密的密鑰 *
         * @param raw
         *            已經加密的數據 *
         * @return 解密後的明文 *
         * @throws Exception
         */
        public static byte[] decrypt(PrivateKey pk byte[] raw) throws Exception {
            try {
                Cipher cipher = CiphergetInstance(RSA
                        new orgbouncycastlejceproviderBouncyCastleProvider());
                cipherinit(cipherDECRYPT_MODE pk);
                int blockSize = ciphergetBlockSize();
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                int j = ;

  while (rawlength j * blockSize > ) {
                    boutwrite(cipherdoFinal(raw j * blockSize blockSize));
                    j++;
                }
                return bouttoByteArray();
            } catch (Exception e) {
                throw new Exception(egetMessage());
            }
        }

  /**
         * * *
         *
         * @param args *
         * @throws Exception
         */
        public static void main(String[] args) throws Exception {
            RSAPublicKey rsap = (RSAPublicKey) RSAUtilgenerateKeyPair()getPublic();
            String test = hello world;
            byte[] en_test = encrypt(getKeyPair()getPublic()testgetBytes());
            byte[] de_test = decrypt(getKeyPair()getPrivate()en_test);
            <A title=system target=_blank>system</A>outprintln(new String(de_test));
        }
    }

  測試頁面

  IndexActionjava

  

  /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClassvtl
     */
    package comsunsoftstrutsaction;

  import javasecurityinterfacesRSAPrivateKey;
    import javasecurityinterfacesRSAPublicKey;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;

  import comsunsoftstrutsutilRSAUtil;

  /**
     * MyEclipse Struts
     * Creation date:
     *
     * XDoclet definition:
     * @strutsaction 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) RSAUtilgetKeyPair()getPublic();
            String module = rsapgetModulus()toString();
            String empoent = rsapgetPublicExponent()toString();
            Systemoutprintln(module);
            Systemoutprintln(module);
            Systemoutprintln(empoent);
            Systemoutprintln(empoent);
            requestsetAttribute(m module);
            requestsetAttribute(e empoent);
            return mappingfindForward(login);
        }
    }


  /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClassvtl
     */
    package comsunsoftstrutsaction;

  import javasecurityinterfacesRSAPrivateKey;
    import javasecurityinterfacesRSAPublicKey;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;

  import comsunsoftstrutsutilRSAUtil;

  /**
     * MyEclipse Struts
     * Creation date:
     *
     * XDoclet definition:
     * @strutsaction 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) RSAUtilgetKeyPair()getPublic();
            String module = rsapgetModulus()toString();
            String empoent = rsapgetPublicExponent()toString();
            <A title=system target=_blank>system</A>outprintln(module);
            <A title=system target=_blank>system</A>outprintln(module);
            <A title=system target=_blank>system</A>outprintln(empoent);
            <A title=system target=_blank>system</A>outprintln(empoent);
            requestsetAttribute(m module);
            requestsetAttribute(e empoent);
            return mappingfindForward(login);
        }
    }

  通過此action進入登錄頁面並傳入公鑰的 Modulus 與PublicExponent的hex編碼形式

  登錄頁面 loginjsp

  <%@ page language=java pageEncoding=GBK%>

  <%@ taglib uri=bean prefix=bean %>
    <%@ taglib uri=html prefix=html %>
    <%@ taglib uri=logic prefix=logic %>
    <%@ taglib uri=tiles prefix=tiles %>

  <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
    <html:html lang=true>
      <head>
        <html:base />

  <title>login</title>

  <meta httpequiv=pragma content=nocache>
        <meta httpequiv=cachecontrol content=nocache>
        <meta httpequiv=expires content=>
        <meta httpequiv=keywords content=keywordkeywordkeyword>
        <meta httpequiv=description content=This is my page>
        <!
        <link rel=stylesheet type=text/css >
        >
    <script type=text/javascript src=js/RSAjs></script>
    <script type=text/javascript src=js/BigIntjs></script>
    <script type=text/javascript src=js/Barrettjs></script>
    <script type=text/javascript>
    function rsalogin()
    {
       bodyRSA();
       var result = encryptedString(key documentgetElementById(pwd)value);
       //alert(result);
       loginFormaction=logindo?result=+result;
       loginFormsubmit();
    }
    var key ;
    function bodyRSA()
    {
        setMaxDigits();
        key = new RSAKeyPair(ccdaedaafedccfaeafefcabacac
defdbfaffccfdeaabaffdbdfbaeedceefbbaecdc
aeeedfeecaaeadfdaccfebcabfcfbfddefcbd);

  }

  </script>
      </head>

  <body >
        <html:form action=login method=post focus=username>
          <table border=>
            <tr>
              <td>Login:</td>
              <td><html:text property=username /></td>
            </tr>
            <tr>
              <td>Password:</td>
              <td><html:password property=password styleId=pwd/></td>
            </tr>
            <tr>
              <td colspan= align=center><input type=button value=SUBMIT onclick=rsalogin();/></td>
            </tr>
          </table>
        </html:form>
      </body>
    </html:html>


  <%@ page language=java pageEncoding=GBK%>

  <%@ taglib uri=bean prefix=bean %>
    <%@ taglib uri=html prefix=html %>
    <%@ taglib uri=logic prefix=logic %>
    <%@ taglib uri=tiles prefix=tiles %>

  <!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
    <html:html lang=true>
      <head>
        <html:base />

  <title>login</title>

  <meta httpequiv=pragma content=nocache>
        <meta httpequiv=cachecontrol content=nocache>
        <meta httpequiv=expires content=>
        <meta httpequiv=keywords content=keywordkeywordkeyword>
        <meta httpequiv=description content=This is my page>
        <!
        <link rel=stylesheet type=text/css >
        >
    <script type=text/javascript src=js/RSAjs></script>
    <script type=text/javascript src=js/BigIntjs></script>
    <script type=text/javascript src=js/Barrettjs></script>
    <script type=text/javascript>
    function rsalogin()
    {
       bodyRSA();
       var result = encryptedString(key documentgetElementById(pwd)value);
       //alert(result);
       loginFormaction=logindo?result=+result;
       loginFormsubmit();
    }
    var key ;
    function bodyRSA()
    {
        setMaxDigits();
        key = new RSAKeyPair(ccdaedaafedccfaeafefcabacac
defdbfaffccfdeaabaffdbdfbaeedceefbbaecd
caeeedfeecaaeadfdaccfebcabfcfbfddefcbd);

  }

  </script>
      </head>

  <body >
        <html:form action=login method=post focus=username>
          <table border=>
            <tr>
              <td>Login:</td>
              <td><html:text property=username /></td>
            </tr>
            <tr>
              <td>Password:</td>
              <td><html:password property=password styleId=pwd/></td>
            </tr>
            <tr>
              <td colspan= align=center><input type=button value=SUBMIT onclick=rsalogin();/></td>
            </tr>
          </table>
        </html:form>
      </body>
    </html:html>

  /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClassvtl
     */
    package comsunsoftstrutsaction;

  import javamathBigInteger;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;

  import comsunsoftstrutsutilRSAUtil;

  /**
     * MyEclipse Struts
     * Creation date:
     *
     * XDoclet definition:
     * @strutsaction path=/login name=loginForm input=/loginjsp scope=request validate=true
     * @strutsactionforward name=error path=/errorjsp
     * @strutsactionforward name=success path=/successjsp
     */
    public class LoginAction 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{
            //LoginForm loginForm = (LoginForm) form;
            String result = requestgetParameter(result);
            Systemoutprintln(原文加密後為);
            Systemoutprintln(result);
            byte[] en_result = new BigInteger(result )toByteArray();
            Systemoutprintln(轉成byte[]+new String(en_result));
            byte[] de_result = RSAUtildecrypt(RSAUtilgetKeyPair()getPrivate()en_result);
            Systemoutprintln(還原密文);

  Systemoutprintln(new String(de_result));
            StringBuffer sb = new StringBuffer();
            sbappend(new String(de_result));
            Systemoutprintln(sbreverse()toString());
            return mappingfindForward(success);
        }
    }


  /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClassvtl
     */
    package comsunsoftstrutsaction;

  import javamathBigInteger;

  import javaxservlethttpHttpServletRequest;
    import javaxservlethttpHttpServletResponse;

  import orgapachestrutsactionAction;
    import orgapachestrutsactionActionForm;
    import orgapachestrutsactionActionForward;
    import orgapachestrutsactionActionMapping;

  import comsunsoftstrutsutilRSAUtil;

  /**
     * MyEclipse Struts
     * Creation date:
     *
     * XDoclet definition:
     * @strutsaction path=/login name=loginForm input=/loginjsp scope=request validate=true
     * @strutsactionforward name=error path=/errorjsp
     * @strutsactionforward name=success path=/successjsp
     */
    public class LoginAction 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{
            //LoginForm loginForm = (LoginForm) form;
            String result = requestgetParameter(result);
            <A title=system target=_blank>system</A>outprintln(原文加密後為);
            <A title=system target=_blank>system</A>outprintln(result);
            byte[] en_result = new BigInteger(result )toByteArray();
            <A title=system target=_blank>system</A>outprintln(轉成byte[]+new String(en_result));
            byte[] de_result = RSAUtildecrypt(RSAUtilgetKeyPair()getPrivate()en_result);
            <A title=system target=_blank>system</A>outprintln(還原密文);

  <A title=system target=_blank>system</A>outprintln(new String(de_result));
            StringBuffer sb = new StringBuffer();
            sbappend(new String(de_result));
            <A title=system target=_blank>system</A>outprintln(sbreverse()toString());
            return mappingfindForward(success);
        }
    }



From:http://tw.wingwit.com/Article/program/Java/hx/201311/27165.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.