2015-05-21 59 views
1

1)我有我写了一个解密方法RSA加密编码使用Apache编解码器问题

运行解密方法,而我得到了下面一个txt文件

2)私钥采用Android解密上述例外。

import java.io.File; 
import java.io.FileInputStream; 
import java.security.KeyFactory; 
import java.security.PrivateKey; 
import java.security.spec.EncodedKeySpec; 
import java.security.spec.PKCS8EncodedKeySpec; 
import java.util.Calendar; 
import java.util.Random; 

import javax.crypto.Cipher; 

import android.util.Base64; 
public static String decrypt(String inputString, byte[] keyBytes) { 
    String resultStr = null; 
    Calendar cal = Calendar.getInstance(); 
    int mDay = cal.get(Calendar.DAY_OF_MONTH); 
    Random generator = new Random(mDay); 
    int num = (generator.nextInt()) % 100; 
    String salt = "XXwerr" + num; 
    PrivateKey privateKey = null; 
    try { 
     KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
     EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(keyBytes); 
     privateKey = keyFactory.generatePrivate(privateKeySpec); 
    } catch (Exception e) { 
     System.out.println("Exception privateKey::::::::::::::::: " 
       + e.getMessage()); 
    } 
    byte[] decodedBytes = null; 
    try { 
     Cipher c = Cipher.getInstance("RSA"); 
     //Also tried 
     // Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 
     c.init(Cipher.DECRYPT_MODE, privateKey); 
     decodedBytes = c.doFinal(Base64.decode(inputString, Base64.NO_CLOSE)); 

    } catch (Exception e) { 
     System.out.println("Exception privateKey1::::::::::::::::: " 
       + e.getMessage()); 
     e.printStackTrace(); 
    } 
    if (decodedBytes != null) { 
     resultStr = new String(decodedBytes); 
     System.out.println("resultStr:::" + resultStr + ":::::"); 
     resultStr = resultStr.replace(salt, ""); 
    } 
    return resultStr; 

} 

下面是主要方法

public static void main(String[] args) { 
    FileInputStream fileInputStream = null; 

    File file = new File("/Users/buta1/Downloads/private.txt"); 

    byte[] bFile = new byte[(int) file.length()]; 

    try { 
     // convert file into array of bytes 
     fileInputStream = new FileInputStream(file); 
     fileInputStream.read(bFile); 
     fileInputStream.close(); 

     // for (int i = 0; i < bFile.length; i++) { 
     // System.out.println((char)bFile[i]); 
     // } 
     decrypt("08F8CFE58F2E707C314F4D7894E0F1", bFile); 
     System.out.println("Done"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

所使用的加密方法如下使用Android的Base64类

public static String encrypt(String inputString, byte [] keyBytes) 
    { 
    Calendar cal = Calendar.getInstance(); 
    int mDay = cal.get(Calendar.DAY_OF_MONTH); 
    //System.out.println("Day of month :::" + mDay); 
    String encryptedString = ""; 
    Key publicKey = null; 
    try { 
    Random generator = new Random(mDay); 
    int num = (generator.nextInt()) % 100; 
    String salt = "xx"+num; 
    inputString += salt; 
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(keyBytes); 
    KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 
    publicKey = keyFactory.generatePublic(publicKeySpec); 
    } catch (Exception e) { 
    System.out.println("Exception rsaEncrypt::::::::::::::::: "+ 
    e.getMessage()); 
    } 
    // Encode the original data with RSA public key 
    byte[] encodedBytes = null; 
    try { 
    Cipher c = Cipher.getInstance("RSA"); 
    c.init(Cipher.ENCRYPT_MODE, publicKey); 
    encodedBytes = c.doFinal(inputString.getBytes()); 
    encryptedString = 
    Base64.encodeToString(encodedBytes, Base64.NO_CLOSE); 
    } catch (Exception e) { 
    System.out.println("Exception rsaEncrypt::::::::::::::::: "+ 
    e.getMessage()); 
    } 

    return encryptedString; 
    } 

使用公编解码基础后64

Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding "); 

得到以下错误

javax.crypto.BadPaddingException: Decryption error 
    at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380) 
    at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291) 
    at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365) 
    at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391) 
    at javax.crypto.Cipher.doFinal(Cipher.java:2087) 
    at RSAEncryption.decrypt(RSAEncryption.java:41) 
    at RSAEncryption.main(RSAEncryption.java:108) 
+0

堆栈跟踪告诉你抛出异常的位置。尝试将其降低到隔离错误的一小段代码。 –

+0

Base64解码正在下降 – Tarik

回答

1

我假设你想在你的类路径中运行从IDE内部的Android代码与android.jar(例如通过在eclipse中使用ADT插件)。然后,这看起来像“按预期工作”(参见https://code.google.com/p/android/issues/detail?id=33188)。

android.jar只包含所有类的存根实现,因为预期的方法是在模拟器中运行该代码。

如果您正在尝试编写Android代码,并且发现在模拟器中运行代码太麻烦,您可以尝试Roboelectric,它基本上用真正的实现取代所有这些存根,并允许您从内部运行代码你骑。

在另一方面,如果你不试图编写代码的Android,你可以简单地用org.apache.commons.codec.binary.Base64java.util.Base64.Decoder(从Java 8)取代android.util.Base64

+0

谢谢马文...我应该使用哪个包来解密加密的字符串?加密是使用android.jar中的Base 64完成的。由于解密部分必须在任何设备之外完成,以便在收集数据之后进行分析。 – Dutta

+0

@Dutta:如果解密没有在设备上运行,您应该可以使用其中一个提到的替换。 – Marvin

+0

我使用的是commons codec,但是'cipher c = Cipher.getInstance(“RSA/ECB/PKCS1Padding”);'获得以下异常'javax.crypto.BadPaddingException:解密错误 \t at sun.security.rsa.RSAPadding.unpadV15(RSAPadding的.java:380) \t在sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291) \t在com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:365) \t在COM。 sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:391) \t在javax.crypto.Cipher.doFinal(Cipher.java:2087) \t在RSAEncryption.decrypt(RSAEncryption.java:41) \t在与RSAEncryption 。主要(RSAEncrypti on.java:108)” – Dutta