Java(Android)解密带附加IV的消息 [英] Java (Android) Decrypting msg with IV attached

查看:203
本文介绍了Java(Android)解密带附加IV的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成一个随机IV,这个IV连接(以普通字节为单位)到加密消息的前面,如下所示;

I generate a random IV, this IV is attached (in plain bytes) to front of the encrypted msg as shown below;

public String encrypt(String plainText, byte[] encryptionKey) throws Exception {
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");           
    cipher.init(Cipher.ENCRYPT_MODE, key, iV);
    byte[] data = new byte[iV.getIV().length + plainText.getBytes("UTF-8").length];
    // Merge together plain IV and encrypted cipher text
    System.arraycopy(iV.getIV(), 0, data, 0, iV.getIV().length);
    System.arraycopy(cipher.doFinal(plainText.getBytes("UTF-8")), 0, data, iV.getIV().length, plainText.getBytes("UTF-8").length);

    return Base64.encodeToString(data, Base64.DEFAULT);

}

使用WiFi Direct在设备之间发送消息。这是在我的MainActivity中处理的;

The message is sent between devices using WiFi Direct. This is handled in my MainActivity;

case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;

    crypto.iV = new IvParameterSpec(Arrays.copyOf(readBuf, 16));
    // Construct a string from the valid bytes in the buffer
    String readMessage = new String(readBuf, 0, msg.arg1);
    Log.d(TAG, readMessage);

    try {
        String decryptMsg = crypto.decrypt(readMessage, SECRET_KEY);
        // Present the message
        (chatFragment).pushMessage("Buddy (decrypt): " + decryptMsg);

        Log.d(TAG, decryptMsg);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Present the message (comment out after testing!)
    //(chatFragment).pushMessage("Buddy (encrypt): " + readMessage);
    break;

在解密期间它失败并显示警告;

During decryption it fails with the warning;

04-04 14:55:05.770:W / System.err(9847):javax.crypto.IllegalBlockSizeException:解密时最后一个块不完整

04-04 14:55:05.770: W/System.err(9847): javax.crypto.IllegalBlockSizeException: last block incomplete in decryption

04-04 14:55:05.789:W / System.err(9847):at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:850)

04-04 14:55:05.789: W/System.err(9847): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:850)

04-04 14:55:05.790:W / System.err(9847):at javax.crypto.Cipher.doFinal(Cipher.java:1340)

04-04 14:55:05.790: W/System.err(9847): at javax.crypto.Cipher.doFinal(Cipher.java:1340)

04-04 14:55:05.790:W / System.err(9847):at com.example.cryptochat.Crypto.decrypt(Crypto.java:52)

04-04 14:55:05.790: W/System.err(9847): at com.example.cryptochat.Crypto.decrypt(Crypto.java:52)

04-04 14:55:05.790:W / System.err(9847):at com.example.cryptochat.MainActivity.handleMessage(MainActivity.java:463)

04-04 14:55:05.790: W/System.err(9847): at com.example.cryptochat.MainActivity.handleMessage(MainActivity.java:463)

问题在于解密方法,但我不确定我做错了什么。该方法如下;

The problem lies in the decrypt method, i am however unsure on what i am doing wrong. That method is below;

public String decrypt(String cipherText, byte[] encryptionKey) throws Exception {
    SecretKeySpec key = new SecretKeySpec(encryptionKey, "AES");          
    cipher.init(Cipher.DECRYPT_MODE, key, iV);
    String decrypt = new String(cipher.doFinal( Base64.decode(cipherText, Base64.DEFAULT)));
    decrypt = new String(Arrays.copyOfRange(decrypt.getBytes(), 16, decrypt.getBytes().length));

    return decrypt;
} 


推荐答案

使用分组密码时填充加密文本总是大于解密文本。但是,您只能将解密文本中的字节数复制到要发送的消息中。因此,您的加密邮件不完整。

When using a block cipher with padding the encrypted text is always larger that the decrypted text. But you only copy as many bytes as the decrypted text has into the message to be sent. Therefore your encrypted message is incomplete.

byte[] encryped = cipher.doFinal(plainText.getBytes("UTF-8"));
byte[] data = new byte[iV.getIV().length + encrypted.length];
System.arraycopy(iV.getIV(), 0, data, 0, iV.getIV().length);
System.arraycopy(encrypted, 0, data, iV.getIV().length, encrypted.length);

这篇关于Java(Android)解密带附加IV的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆