开发者

AES256 algorithm with interoperability between IPhone and Android

开发者 https://www.devze.com 2023-02-12 05:52 出处:网络
I am using the following code to encrypt byte data on android device.I used the AES256 algorithm on IPHone. But unfortunately, the data is not getting decrypted properly between IPhone and Android. It

I am using the following code to encrypt byte data on android device. I used the AES256 algorithm on IPHone. But unfortunately, the data is not getting decrypted properly between IPhone and Android. It works between IPhone-IPhone or Android-Android.

Any suggestions?

public static SecretKeySpec getSecretKeySpec(String passphrase, String algorithm, int kgenbit)throws Exception
 {  
                   byte[] salt = {
                       (byte)0xA9, (b开发者_运维知识库yte)0x87, (byte)0xC8, (byte)0x32,
                       (byte)0x56, (byte)0xA5, (byte)0xE3, (byte)0xB2
                   };

                   // Iteration count
                   int iterationCount = 1024;

                   KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt, iterationCount);

                   SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);

                   MessageDigest md = MessageDigest.getInstance("MD5");
                   md.update(secretKey.getEncoded());
                   md.update(salt);
                   for(int i = 1; i < iterationCount; i++)
                   md.update(md.digest());

                   byte[] keyBytes = md.digest();
                   skeyspec = new SecretKeySpec(keyBytes, algorithm);

                   return skeyspec;

             }

    public static byte[] encrypt(byte[] voicedata, int len)throws Exception{
          Cipher cipher = Cipher.getInstance(skeyspec.getAlgorithm());
          cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
          byte[] encrypted = cipher.doFinal(voicedata, 0, len);
          return encrypted;

    }


The iPhone is little endian.

An android phone's endianness depends on the hardware device it is being run on. Call nativeOrder() to determine this:

Returns the byte order object, which is either LITTLE_ENDIAN or BIG_ENDIAN.

0

精彩评论

暂无评论...
验证码 换一张
取 消