如何在Java中加密并在Android和iOS中解密

sof*_*ply 2 java iphone encryption android ios

我有一台运行Java-jar文件的Linux服务器,可以加密多个文件.

Android和iPhone App下载该文件并将其解密.我必须使用什么算法?

我认识到我在Java中使用的算法在Android中不起作用.我在Java中所做的是:

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}
Run Code Online (Sandbox Code Playgroud)

什么在上面的代码中不起作用?任何替代品?

blu*_*boo 8

iOS版:

我使用NSString + AESCrypt(https://github.com/Gurpartap/AESCrypt-ObjC)

样品:

NSString* encrypted = [plainText AES256EncryptWithKey:@"MyEncryptionKey"];
NSString* decrypted = [encrypted AES256DecryptWithKey:@"MyEncryptionKey"];
Run Code Online (Sandbox Code Playgroud)

Android(AES256Cipher - https://gist.github.com/dealforest/1949873):

加密:

String base64Text="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Request(crypt) ##############
    cipherData = AES256Cipher.encrypt(ivBytes, keyBytes, passval1.getBytes("UTF-8"));
    base64Text = Base64.encodeToString(cipherData, Base64.DEFAULT);
}
catch ( Exception e ) {
    e.printStackTrace();
}        
Run Code Online (Sandbox Code Playgroud)

解密:

String base64Text="";
String plainText="";
try {
    String key = "MyEncryptionKey";
    byte[] keyBytes = key.getBytes("UTF-8");
    byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    byte[] cipherData;

    //############## Response(decrypt) ##############
base64Text = User.__currentUser.getPasscode();
    cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, Base64.decode(base64Text.getBytes("UTF-8"), Base64.DEFAULT));
    plainText = new String(cipherData, "UTF-8");            
}
catch ( Exception e )
{
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)