Асимметричное шифрование между Android и переполнением стека

Следующие асимметричные функции используются для шифрования и дешифрования в 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);
}

0

Решение

Как уже упоминалось @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);
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector