解决方案 »

  1.   

    RSA是密钥对,公钥+私钥,任何一个都可以加密,用另外一个解密。你有一个可以加密,但就解不了了
      

  2.   

    嗯,只要加密就行了,还有RSA加密C#格式转java格式清不清楚?
      

  3.   

    加密的时候 需要对数据进行补齐,padding,要公开padding方法
      

  4.   

    使用公钥加密public static byte[] encryptByPublicKey(byte[] data, byte[] keyBytes )
    throws Exception {
    // 取得公钥
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(“RSA”);
    Key publicKey = keyFactory.generatePublic(x509KeySpec); // 对数据加密
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data);
    }
      

  5.   

    /**
     * 获取RSA加密信息
     * @param info
     * @return
     */
    public static String getRSAInfo(String dataStr){
    byte[] en = encrypt(dataStr);  
    String info = Base64.encode(en);
    try {
    info = URLEncoder.encode(info,"UTF-8");
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return info;
    }

    public static byte[] encrypt(String info) {  
            try {  
                byte[] modulusBytes = Base64.decode("yrEAJvQfhhoZcWMzsQw0LZd2OqOTMgOzVl7Ja2w6R1rF+tl+V+6T08zvZAhWye2omdZ8JccefDQNkYTqIcNXu9XqYMTl79FaFVt32wykCIHnvfN8IafVnQKcuWpWDuCbZAocoBisT6LWugyiXWe7cr+2qFSW4nL5YIQ4jxXJn8U=");  
                byte[] exponentBytes = Base64.decode("AQAB");  
                BigInteger modulus = new BigInteger(1, modulusBytes);  
                BigInteger exponent = new BigInteger(1, exponentBytes);  
      
                RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);  
                KeyFactory fact = KeyFactory.getInstance("RSA");  
                PublicKey pubKey = fact.generatePublic(rsaPubKey);  
      
                Cipher cipher = Cipher.getInstance("RSA");  
                cipher.init(Cipher.ENCRYPT_MODE, pubKey);  
      
                byte[] cipherData = cipher.doFinal(new String(info).getBytes());  
                return cipherData;  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            return null;  
      
        }