android和php之间的非对称加密

时间:2017-04-19 16:18:08

标签: java php android encryption encryption-asymmetric

以下非对称函数用于在android中加密和解密..它工作得很好。但是,当我使用下面的函数加密并使用php或其他函数解密时(使用eclipse非常相似)..我得到null ..糟糕的填充异常..我无法弄清楚问题..因为我编码结果..为什么它只在我在android或eclipse中加密和解密时才起作用..但是在php和android之间或者只是在两个java程序之间不起作用..但是在eclipse和android上..

Android程序:

public  String encryptAsymmetric(String input, Key key) throws GeneralSecurityException, IOException {
    byte[] crypted = null;
    try{

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        crypted = cipher.doFinal(input.getBytes());
    }catch(Exception e){
        System.out.println(e.toString());
    }//Base64.encodeBase64(crypted)

    return new String(Base64.encode(crypted, Base64.DEFAULT));
}
public  String decryptAsymmetric(String input, Key key){
    byte[] output = null;
    try{

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);//Base64.decodeBase64(input.getBytes())
        output = cipher.doFinal(Base64.decode(input.getBytes(), Base64.DEFAULT));
    }catch(Exception e){
        System.out.println(e.toString());
    }
    return new String(output);
}

Eclipse(Java程序):

public static String encryptAsymmetric(String input, Key key){
      byte[] crypted = null;

      try{

          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          crypted = cipher.doFinal(input.getBytes());
        }catch(Exception e){
            System.out.println(e.toString());
        }//Base64.encodeBase64(crypted)
        return new String(Base64.getEncoder().encode(crypted));
    }
public static String decryptAsymmetric(String input, Key key){
    byte[] output = null;
    try{

      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, key);//Base64.decodeBase64(input.getBytes())
      output = cipher.doFinal(Base64.getDecoder().decode(input.getBytes()));
    }catch(Exception e){
      System.out.println(e.toString());
    }
    return new String(output);
}

1 个答案:

答案 0 :(得分:1)

正如@James在注释中所提到的,input.getBytes和getInstance不可移植..我通过添加UTf-8代码更改并编写了RSA / ECB / PKCS1Padding而不是RSA,它解决了我的问题..

 public  String encryptAsymmetric(String input, Key key) throws GeneralSecurityException, IOException {
    byte[] crypted = null;
    try{
        byte[] bytes = input.getBytes("UTF-8");
        //String text = new String(bytes, "UTF-8");
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        crypted = cipher.doFinal(bytes);
    }catch(Exception e){
        System.out.println(e.toString());
    }//Base64.encodeBase64(crypted)

    return new String(Base64.encode(crypted, Base64.DEFAULT));
}
public  String decryptAsymmetric(String input, Key key){
    byte[] output = null;
    try{

        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);//Base64.decodeBase64(input.getBytes())
        output = cipher.doFinal(Base64.decode(input.getBytes("UTf-8"), Base64.DEFAULT));
    }catch(Exception e){
        System.out.println(e.toString());
    }
    return new String(output);
}