繁体   English   中英

使用apache编解码器使用Android Decrypt进行RSA加密编码问题

[英]RSA Encryption Encode using Android Decrypt using apache codec Issue

1)我在txt文件中有一个私钥

2)我写了一个解密方法

运行解密方法时,出现以下异常。

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;
     }

使用Commons Codec Base 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)

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

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

如果您尝试为Android编写代码,并且发现在模拟器中运行代码太麻烦,则可以尝试Roboelectric ,它基本上用实际的实现替换了所有这些存根,并允许您从IDE内部运行代码。

另一方面,如果您打算为Android编写代码,则可以简单地将android.util.Base64替换为org.apache.commons.codec.binary.Base64java.util.Base64.Decoder (从Java 8开始)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM