나는 네이티브 Android 앱과 하이브리드 IOS 앱을 개발 중입니다. BL로 요청을 보내기 전에 암호를 암호화하고 있습니다. 아래는 내 기본 코드입니다.Ionic을 사용하여 IOS 용 RSA 암호화를 수행하는 방법
public String Encrypt (String plain) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
{
try {
AssetManager assets = context.getAssets();
byte[] key = readFully(
assets.open("encryption.der", AssetManager.ACCESS_BUFFER));
KeySpec publicKeySpec = new X509EncodedKeySpec(key);
KeyFactory kf = KeyFactory.getInstance("RSA");
Key pk = kf.generatePublic(publicKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pk);
ByteArrayOutputStream out = new ByteArrayOutputStream();
CipherOutputStream cout = new CipherOutputStream(out, cipher);
try {
cout.write(plain.getBytes(UTF_8));
cout.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
cout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
encrypted = new String(encode(out.toByteArray(), DEFAULT), "UTF-8");
return encrypted;
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
static byte[] readFully(InputStream inputStream) throws IOException {
InputStream in = new BufferedInputStream(inputStream);
byte[] tmp = new byte[1024];
int readLen, size = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((readLen = in.read(tmp)) != -1) {
if (((long) size + readLen) > Integer.MAX_VALUE) {
// woah! did we just ship an app of 2GB?
throw new IllegalStateException("Invalid file. File size exceeds expected "
+ "maximum of 2GB");
}
size += readLen;
out.write(tmp, 0, readLen);
}
return out.toByteArray();
}
내 encryption.der 파일에 내 키가 있습니다. 모든 것은 안드로이드에서 잘 작동합니다. 이제는 Ionic을 개발하기 위해 IOS를 사용하고 있습니다. 나는 암호화 부분을 달성 할 수 없다. 나는 "cryptico"를 사용했다 : link : https://github.com/wwwtyro/cryptico/blob/master/README.md. 그리고 마지막으로 Base64로 변환합니다.
var EncryptionPassword = cryptico.encrypt($scope.userInfo.Password, publicKey);
$scope.encPass = base64.encode(EncryptionPassword.cipher);
하지만 BL에서 ArrayIndexOutOfBound 예외가 발생합니다. 정확한 동일한 해결책이 angularjs를위한 android도 가지고 있다고 제안 할 수 있습니까? 따라서 RSA 암호화는 IOS와 Android 모두에서 작동합니다.