gpt4 book ai didi

java - android和php之间的非对称加密

转载 作者:行者123 更新时间:2023-12-02 13:22:55 28 4
gpt4 key购买 nike

以下非对称函数用于在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);
}

最佳答案

正如 @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);
}

关于java - android和php之间的非对称加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43501070/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com