您的位置:首页 > 编程语言 > Java开发

C#和Java实现互通的RSA&DES加解密算法(二)

2014-12-10 13:51 375 查看
2 Java部分

2.1 RSA加密

2.1.1 返回字符串

    /// <summary>

    /// RSA字符串加密(加密最大长度为117字节)

    /// </summary>

    /// <param name="data">待加密字符串</param>

    /// <param name="keyFile">公钥文件路径</param>    

    /// <returns>加密后的字节数组base64后的字符串</returns>

    public static String RSAEncryptStr(String data, String keyFile) {

        try {

            KeyFactory fact = KeyFactory.getInstance("RSA");

            

            // 读取公钥

            readKeyFromFile(keyFile);            

            byte[] modulusBytes = Base64.decode(module);

            byte[] exponentBytes = Base64.decode(exponentString);

            BigInteger modulus = new BigInteger(1, modulusBytes);

            BigInteger exponent = new BigInteger(1, exponentBytes);

            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);            

            PublicKey pubKey = fact.generatePublic(rsaPubKey);

            Cipher cipher = Cipher.getInstance("RSA");            

            cipher.init(Cipher.ENCRYPT_MODE, pubKey);            

            byte[] byteCipherData = cipher.doFinal(data.getBytes());

            

            String strEncrypt = Base64.encode(byteCipherData);

            return strEncrypt;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.1.2 返回字节数组

    /// <summary>

    /// RSA字符串加密(加密最大长度为117字节)

    /// </summary>

    /// <param name="data">待加密字符串</param>

    /// <param name="keyFile">公钥文件路径</param>    

    /// <returns>加密后的字节数组</returns>

    public static byte[] RSAEncrypt(String data, String keyFile) {

        try {

            KeyFactory fact = KeyFactory.getInstance("RSA");

            

            // 读取公钥

            readKeyFromFile(keyFile);            

            byte[] modulusBytes = Base64.decode(module);

            byte[] exponentBytes = Base64.decode(exponentString);

            BigInteger modulus = new BigInteger(1, modulusBytes);

            BigInteger exponent = new BigInteger(1, exponentBytes);

            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);            

            PublicKey pubKey = fact.generatePublic(rsaPubKey);

            Cipher cipher = Cipher.getInstance("RSA");

            cipher.init(Cipher.ENCRYPT_MODE, pubKey);

            byte[] byteCipherData = cipher.doFinal(data.getBytes());

            return byteCipherData;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.2 RSA解密字符串

2.2.1 返回字符串

    /// <summary>

    /// RSA字符串解密(解密最大长度是128字节)

    /// </summary>

    /// <param name="data">待解密字符串</param>

    /// <param name="keyFile">私钥文件路径</param>    

    /// <returns>解密字符串</returns>

    public static String RSADecryptStr(String data, String keyFile) {

        try {

            KeyFactory factory = KeyFactory.getInstance("RSA");

            

            // 读取私钥

            readKeyFromFile(keyFile);            

            byte[] expBytes = Base64.decode(delement);

            byte[] modBytes = Base64.decode(module);

            BigInteger modules = new BigInteger(1, modBytes);

            BigInteger exponent = new BigInteger(1, expBytes);

            RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);

            PrivateKey privKey = factory.generatePrivate(privSpec);

            

            Cipher cipher = Cipher.getInstance("RSA");            

            cipher.init(Cipher.DECRYPT_MODE, privKey);            

            byte[] byteEncrypt = Base64.decode(data);

            String strDecrypt = new String(cipher.doFinal(byteEncrypt));

            return strDecrypt;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.2.2 返回字节数组

    /// <summary>

    /// RSA解密字节数组解密(解密最大长度是128字节)

    /// </summary>

    /// <param name="data">待解密字节数组</param>

    /// <param name="keyFile">私钥文件路径</param>    

    /// <returns>解密后的字节数组</returns>

    public static byte[] RSADecrypt(byte[] data, String keyFile) {

        try {

            KeyFactory factory = KeyFactory.getInstance("RSA");

            

            // 读取私钥

            readKeyFromFile(keyFile);            

            byte[] expBytes = Base64.decode(delement);

            byte[] modBytes = Base64.decode(module);

            BigInteger modules = new BigInteger(1, modBytes);

            BigInteger exponent = new BigInteger(1, expBytes);

            RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);

            PrivateKey privKey = factory.generatePrivate(privSpec);

            

            Cipher cipher = Cipher.getInstance("RSA");            

            cipher.init(Cipher.DECRYPT_MODE, privKey);

            byte[] byteDecrypted = cipher.doFinal(data);

            return byteDecrypted;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.3 SHA1WITHRSA签名

    /// <summary>

    /// SHA1WITHRSA签名

    /// </summary>

    /// <param name="data">待签名字符串</param>

    /// <param name="keyFile">私钥文件路径</param>    

    /// <returns>签名后的字节数组base64后的字符串</returns>

    public static String Sign(String data, String keyFile) {

        try {

            KeyFactory factory = KeyFactory.getInstance("RSA");

            

            // 读取公私钥

            readKeyFromFile(keyFile);

            

            byte[] expBytes = Base64.decode(delement);

            byte[] modBytes = Base64.decode(module);

            BigInteger modules = new BigInteger(1, modBytes);

            BigInte
4000
ger exponent = new BigInteger(1, expBytes);            

            RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, exponent);            

            PrivateKey privKey = factory.generatePrivate(privSpec);

            

            Signature sign = Signature.getInstance("SHA1withRSA");

            sign.initSign(privKey);

            sign.update(data.getBytes());

            byte[] byteSign = sign.sign();

            

            String strSign = Base64.encode(byteSign);

            return strSign;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.4 SHA1WITHRSA验证签名

    /// <summary>

    /// SHA1WITHRSA验证签名

    /// </summary>

    /// <param name="data">明文字符串</param>

    /// <param name="sign">签名字符串</param>

    /// <param name="keyFile">公钥文件路径</param>    

    /// <returns>验签结果,true验签通过,false验签未通过</returns>

    public static boolean VerifySign(String data, String sign, String keyFile) {

        try {

            KeyFactory factory = KeyFactory.getInstance("RSA");

            

            // 读取公私钥

            readKeyFromFile(keyFile);

            

            byte[] modulusBytes = Base64.decode(module);

            byte[] exponentBytes = Base64.decode(exponentString);

            BigInteger modulus = new BigInteger(1, modulusBytes);

            BigInteger exponent = new BigInteger(1, exponentBytes);        

            RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);

            PublicKey pubKey = factory.generatePublic(rsaPubKey);

            

            Signature signa = Signature.getInstance("SHA1withRSA");

            signa.initVerify(pubKey);

            signa.update(data.getBytes());

            

            return signa.verify(Base64.decode(sign));

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

2.5 DES加密

    /// <summary>

      /// DES加密

      /// </summary>

      /// <param name="encryptString">待加密字符串</param>

    /// <param name="encryptKey">des密钥(8字节)</param>

      /// <returns>加密后的字符串</returns>

    public static String DESEncrypt(String encryptString, String encryptKey) {

        try {            

            IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);

            SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);

            byte[] encryptedData = cipher.doFinal(encryptString.getBytes());

            

            return Base64.encode(encryptedData);

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;        

    }

2.6 DES解密

    /// <summary>

      /// DES解密

      /// </summary>

    /// <param name="decryptString">解密字符串</param>

    /// <param name="encryptKey">des密钥(8字节)</param>

      /// <returns>解密后的字符串</returns>

    public static String DESDecrypt(String decryptString, String decryptKey) {

        try {

            byte[] byteMi = Base64.decode(decryptString);

            

            IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]);

            SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");

            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

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

            byte decryptedData[] = cipher.doFinal(byteMi);

            

            String strData = new String(decryptedData);

            return strData;

            

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }

2.7 相关的几个函数

    /// <summary>

    /// 从C#生成的RSA算法密钥文件中读取公私钥对应的字符串

    /// </summary>

    /// <param name="keyFile">公私钥文件路径</param>

    /// <returns></returns>

    public static void readKeyFromFile(String keyFile){

        try{            

            File  xmlFile = new File(keyFile);            

            DocumentBuilderFactory  builderFactory =  DocumentBuilderFactory.newInstance();            

            DocumentBuilder   builder = builderFactory.newDocumentBuilder();            

            org.w3c.dom.Document  doc = builder.parse(xmlFile);            

            doc.getDocumentElement().normalize();            

//            System.out.println("Root  element: " + doc.getDocumentElement().getNodeName());

            

            if(doc.hasChildNodes()){

               NodeList nodeList = doc.getChildNodes();

               for (int i = 0; i < nodeList.getLength(); i++){

                Node  node = (Node)nodeList.item(i);

                    if((node.getNodeType() == Node.ELEMENT_NODE) && ("RSAKeyValue".equals(node.getNodeName()))){

                        if (node.hasChildNodes()){

                            getKey(node.getChildNodes());

                        }

                    }

                   }

                }

        }catch(Exception  e){            

            e.printStackTrace();            

        }        

    }

    

    /// <summary>

      /// 读取公私钥字符串

      /// </summary>

      /// <param name="nodeList">RSAKeyValue子节点列表</param>

      /// <returns></returns>

    private static void getKey(NodeList nodeList){

         for(int i = 0;  i < nodeList.getLength(); i++){           

             Node  node = (Node)nodeList.item(i);

             if(node.getNodeType() == Node.ELEMENT_NODE){               

                 if("Modulus".equals(node.getNodeName())){

                     module = node.getTextContent();

                 }

                 else if ("Exponent".equals(node.getNodeName())){

                     exponentString = node.getTextContent();

                 }

                 else if ("D".equals(node.getNodeName())){

                     delement = node.getTextContent();

                 }

             }       

         }

    }

    /// <summary>

      /// 产生8位随机数(“0-9”+“a-z”)

      /// </summary>

      /// <returns>8位随机数</returns>

    public static String Rands() {

        Random rd = new Random(); // 创建随机对象

        String n = "";            //保存随机数

        int rdGet; // 取得随机数

        do {

            if (rd.nextInt() % 2 == 1) {

            rdGet = Math.abs(rd.nextInt()) % 10 + 48; // 产生48到57的随机数(0-9的键位值)

           } else {

             rdGet = Math.abs(rd.nextInt()) % 26 + 97; // 产生97到122的随机数(a-z的键位值)

           }

        char num1 = (char) rdGet;            //int转换char

        String dd = Character.toString(num1);

        n += dd;

        

        } while (n.length() < 8);// 设定长度,此处假定长度小于8

        

        return n;

    }

参考的几篇博文链接如下,在此对各博主表示感谢:
http://blog.csdn.net/road2010/article/details/40071843 http://blog.csdn.net/llwinnner/article/details/4011936 http://blog.csdn.net/mr_qu/article/details/8363538
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  加密 解密 java rsa