我现有一个公匙,请问如何用RSA将一个字符串解密呢?

解决方案 »

  1.   

    我有一个java的sourcecode,我的目的就是写一个C#的函数,能对他加密过的数据进行,不知道哪位高手能帮忙看看
    package com.panyudc.util;import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.security.InvalidKeyException;
    import java.security.KeyFactory;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.spec.InvalidKeySpecException;
    import java.security.spec.PKCS8EncodedKeySpec;
    import java.security.spec.X509EncodedKeySpec;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;/**
     * 用RSA算法对数据进行加密和解密
     */
    public class DataEncryptDecrypt
    {
        private static PublicKey pubkey;    private static PrivateKey prikey;    /**
         * 初始化公钥和私钥
         *  
         */
        public static synchronized void initKey(String pubkeyfile, String prikeyfile) throws IOException,
                NoSuchAlgorithmException, InvalidKeySpecException
        {
            FileInputStream in = null;
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            byte[] enckey = null;        //读出公钥
            in = new FileInputStream(pubkeyfile);
            enckey = new byte[in.available()];
            in.read(enckey);
            in.close();
            X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(enckey);
            pubkey = keyFactory.generatePublic(pubKeySpec);
            //读出私钥
            in = new FileInputStream(prikeyfile);
            enckey = new byte[in.available()];
            in.read(enckey);
            in.close();
            PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(enckey);
            prikey = keyFactory.generatePrivate(priKeySpec);
        }    /**
         * 对数据data进行解密或者加密,opmode 决定是加密还是解密,keymode决定是用公钥还是私钥
         * 另外,进行加密时data后面附加了系统当前的时间,时间格式(yyyyMMddHHmmss)
         */
        public static String getData(String data, int opmode, int keymode) throws NoSuchPaddingException,
                InvalidKeyException, IllegalStateException, IllegalBlockSizeException, BadPaddingException,
                NoSuchAlgorithmException, NoSuchProviderException, IOException, ClassNotFoundException,
                InvalidKeySpecException
        {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            if (Cipher.ENCRYPT_MODE == opmode)//加密
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                if (Cipher.PUBLIC_KEY == keymode)//用公钥加密
                {
                    cipher.init(Cipher.ENCRYPT_MODE, pubkey);
                }
                else if (Cipher.PRIVATE_KEY == keymode)
                {
                    cipher.init(Cipher.ENCRYPT_MODE, prikey);
                }
                return byte2hex(cipher.doFinal((data + sdf.format(new Date(System.currentTimeMillis()))).getBytes("UTF8")));
            }
            else
            //解密
            {
                if (Cipher.PRIVATE_KEY == keymode)//用私钥解密
                {
                    cipher.init(Cipher.DECRYPT_MODE, prikey);
                }
                else if (Cipher.PUBLIC_KEY == keymode)//用公钥解密
                {
                    cipher.init(Cipher.DECRYPT_MODE, pubkey);
                }
                return new String(cipher.doFinal(hex2byte(data)), "UTF8");
            }
        }    /**
         * 生成公、私密码对
         * 
         * @return
         */
        public static boolean generatekey(String pubkeyfile, String prikeyfile)
        {
            FileOutputStream out = null;
            try
            {
                KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");
                keygen.initialize(1024);
                KeyPair keys = keygen.genKeyPair();
                PublicKey pubkey = keys.getPublic();
                PrivateKey prikey = keys.getPrivate();            out = new FileOutputStream(prikeyfile);
                out.write(prikey.getEncoded());
                out.close();            out = new FileOutputStream(pubkeyfile);
                out.write(pubkey.getEncoded());
                out.close();
                return true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                if (out != null)
                {
                    try
                    {
                        out.close();
                    }
                    catch (IOException e1)
                    {
                        e1.printStackTrace();
                    }
                }
                return false;
            }
        }    /*
         * 将byte换成hex字符串
         */
        public static String byte2hex(byte[] b)
        {
            String hs = "";
            String stmp = "";
            for (int n = 0; n < b.length; n++)
            {
                stmp = (Integer.toHexString(b[n] & 0XFF));
                if (stmp.length() == 1)
                    hs = hs + "0" + stmp;
                else
                    hs = hs + stmp;        }
            return hs.toUpperCase();
        }    /*
         * 将hex串换成byte数组
         */
        public static byte[] hex2byte(String s)
        {
            byte[] b = new byte[s.length() / 2];
            for (int i = 0; i < s.length() / 2; i++)
            {
                b[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
            }
            return b;
        }    public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, IllegalStateException,
                IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
                InvalidKeySpecException, IOException, ClassNotFoundException
        {    }
    }
      

  2.   

    java 里好像可以用私匙加密,然后用公匙和数字证书X.509进行解密,.net里应该也可以实现吧
      

  3.   

    本来对于rsa来说公钥加密或者私钥加密之后,都可以使用另外的一个钥匙解密的。