用javascript与java进行RSA加密与解密

这几天一直做安全登录,网上查了好多资料,不尽如意。

    具体实现思路如下:

    1。服务端生成公钥与私钥,保存。

    2。客户端在请求到登录页面后,随机生成一字符串。

    3。后此随机字符串作为密钥加密密码,再用从服务端获取到的公钥加密生成的随机字符串。

    4。将此两段密文传入服务端,服务端用私钥解出随机字符串,再用此私钥解出加密的密文。

    这其中有一个关键是解决服务端的公钥,传入客户端,客户端用此公钥加密字符串后,后又能在服务端用私钥解出。

    此文即为实现此步而作。

    加密算法为RSA:

    1。服务端的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 工具类。提供加密,解密,生成密钥对等方法。
     * 需要到http://www.bouncycastle.org下载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
         */
http://www.qqread.com/javascript/d418655.html

你可能感兴趣的:(JavaScript,java,算法,struts,Security)