Encryption using AES 256 in Android and IPhone (Different result)

前端 未结 1 1948
小蘑菇
小蘑菇 2021-01-01 06:59

I am trying to implement client side encryption/decryption on Android platform by referencing IOS implementations. I am wrestling with the question that encryption and decry

相关标签:
1条回答
  • 2021-01-01 07:48

    Hi me too had the same problem.

    I found 2 things causing mismatch for my code: 1. ios and android encryption algorithms was not same(i have requested PKCS7Padding algorithm in ios and i tried NoPadding and AES/CBC/PKCS5Padding algorithms in android. 2. The key generated in ios is different than in Android.

    Please see my worked code both gives same output in ios and android:

    IOS:

    - (NSString *) encryptString:(NSString*)plaintext withKey:(NSString*)key {
    NSData *data = [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
    return [data base64EncodedStringWithOptions:kNilOptions];
    }
    - (NSString *) decryptString:(NSString *)ciphertext withKey:(NSString*)key {
    if ([ciphertext isKindOfClass:[NSString class]]) { 
    
        NSData *data = [[NSData alloc] initWithBase64EncodedString:ciphertext options:kNilOptions];
        return [[NSString alloc] initWithData:[data AES256DecryptWithKey:key] encoding:NSUTF8StringEncoding];
    }
    return nil;
    }
    

    Android:(We modified aes w.r.t iOS default method)

    private static String Key = "your key"; 
    public static String encryptString(String stringToEncode) throws NullPointerException {
    
        try {
            SecretKeySpec skeySpec = getKey(Key);
            byte[] clearText = stringToEncode.getBytes("UTF8");
            final byte[] iv = new byte[16];
            Arrays.fill(iv, (byte) 0x00);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
            String encrypedValue = Base64.encodeToString(cipher.doFinal(clearText), Base64.DEFAULT);
            return encrypedValue;
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    public static String encryptString(String stringToEncode) throws NullPointerException {
    
        try {
            SecretKeySpec skeySpec = getKey(Key);
            byte[] clearText = stringToEncode.getBytes("UTF8");
            final byte[] iv = new byte[16];
            Arrays.fill(iv, (byte) 0x00);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
            byte[] cipherData = cipher.doFinal(Base64.decode(stringToEncode.getBytes("UTF-8"), Base64.DEFAULT));
            String decoded = new String(cipherData, "UTF-8");
            return decoded;
    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    
    
    private static SecretKeySpec getKey(String password) throws UnsupportedEncodingException {
        int keyLength = 256;
        byte[] keyBytes = new byte[keyLength / 8];
        Arrays.fill(keyBytes, (byte) 0x0);
        byte[] passwordBytes = password.getBytes("UTF-8");
        int length = passwordBytes.length < keyBytes.length ? passwordBytes.length : keyBytes.length;
        System.arraycopy(passwordBytes, 0, keyBytes, 0, length);
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        return key;
    }
    

    Hope this help u guys Thanks

    0 讨论(0)
提交回复
热议问题