android中的AES加密和解密

时间:2019-03-08 08:59:22

标签: c# encryption

我正在尝试从我的android应用程序加密和解密我的消息,并成功使用了以下代码。

我想在基于c#的Web应用程序服务中解密此消息。任何人都可以用下面的加密代码**中的c#中的等效代码来帮助我。

这是加密代码

// encrypt the message
                byte[] encryptedMsg = encryptSMS(secretKeyString,
                        msgContentString);
                // convert the byte array to hex format in order for
                // transmission
                String msgString = byte2hex(encryptedMsg);
                // send the message through SMS
                sendSMS(recNumString, msgString);
                // finish
                finish();
            } else
                Toast.makeText(getBaseContext(),"Please enter phone number, secret key and the message. Secret key must be 16 characters!",Toast.LENGTH_SHORT).show();
        }
        });
    }

    public static void sendSMS(String recNumString, String encryptedMsg) {
    try {
        // get a SmsManager
        SmsManager smsManager = SmsManager.getDefault();
        // Message may exceed 160 characters
        // need to divide the message into multiples
        ArrayList<String> parts = smsManager.divideMessage(encryptedMsg);
        smsManager.sendMultipartTextMessage(recNumString, null, parts,
                null, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
    }
    // utility function
    public static String byte2hex(byte[] b) {
    String hs = "";
    String stmp = "";
    for (int n = 0; n < b.length; n++) {
        stmp = Integer.toHexString(b[n] & 0xFF);
        if (stmp.length() == 1)hs += ("0" + stmp);
        else hs += stmp;
    }
    return hs.toUpperCase();
    }

    // encryption function
    public static byte[] encryptSMS(String secretKeyString,
                                String msgContentString) {
    try {
        byte[] returnArray;
        // generate AES secret key from user input
        Key key = generateKey(secretKeyString);
        // specify the cipher algorithm using AES
        Cipher c = Cipher.getInstance("AES");
        // specify the encryption mode
        c.init(Cipher.ENCRYPT_MODE, key);
        // encrypt
        returnArray = c.doFinal(msgContentString.getBytes());
        return returnArray;
    } catch (Exception e) {
        e.printStackTrace();

        byte[] returnArray = null;
        return returnArray;
    }
    }
    private static Key generateKey(String secretKeyString) throws Exception {
    Key key = new SecretKeySpec(secretKeyString.getBytes(), "AES");
    return key;
    }
}

这是解密代码

    // utility function: convert hex array to byte array
    public static byte[] hex2byte(byte[] b) {
    if ((b.length % 2) != 0) throw new IllegalArgumentException("hello");
    byte[] b2 = new byte[b.length / 2];
    for (int n = 0; n < b.length; n += 2) {
    String item = new String(b, n, 2);
    b2[n / 2] = (byte) Integer.parseInt(item, 16);
    }
    return b2;
    }

    // decryption function
    public static byte[] decryptSMS(String secretKeyString, byte[] encryptedMsg)
    throws Exception {
    // generate AES secret key from the user input secret key
    Key key = generateKey(secretKeyString);
    // get the cipher algorithm for AES
    Cipher c = Cipher.getInstance("AES");
    // specify the decryption mode
    c.init(Cipher.DECRYPT_MODE, key);
    // decrypt the message
    byte[] decValue = c.doFinal(encryptedMsg);
    return decValue;
    }


    private static Key generateKey(String secretKeyString) throws Exception {
    // generate AES secret key from a String
    Key key = new SecretKeySpec(secretKeyString.getBytes(), "AES");
    return key;
    }
}

0 个答案:

没有答案