【问题标题】:AES 128 encryption in Android and .Net with custom key and IVAndroid 和 .Net 中的 AES 128 加密,带有自定义密钥和 IV
【发布时间】:2012-11-14 18:49:30
【问题描述】:

我的android 应用程序中有一个密码字符串。我需要使用SOAP 网络服务通过.net 网络服务(即以.aspx 结尾)发送密码。在发送密码之前,我需要使用自定义密钥和 IV 使用 AES 128 加密密码。

他们在 .net 中有一个加密/解密工具,带有自定义密钥和 Iv。该工具要求一个 16 位和 IV 8 位的自定义密钥。如果给出字符串,它会生成加密字符串。例子

例子:

Key : 1234567812345678
IV : 12345678
String : android
Encrypted string : oZu5E7GgZ83Z3yoK4y8Utg==

我不知道如何在 android 中执行此操作。需要帮助。

【问题讨论】:

标签: java android asp.net aes


【解决方案1】:

一个完整的例子可以帮助你:

加密/解密函数,使用 IV

public static byte[] encrypt(byte[] data, byte[] key, byte[] ivs) {
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        byte[] finalIvs = new byte[16];
        int len = ivs.length > 16 ? 16 : ivs.length;
        System.arraycopy(ivs, 0, finalIvs, 0, len);
        IvParameterSpec ivps = new IvParameterSpec(finalIvs);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivps);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

public static byte[] decrypt(byte[] data, byte[] key, byte[] ivs) {
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        byte[] finalIvs = new byte[16];
        int len = ivs.length > 16 ? 16 : ivs.length;
        System.arraycopy(ivs, 0, finalIvs, 0, len);
        IvParameterSpec ivps = new IvParameterSpec(finalIvs);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivps);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

你可以像下面这样使用它:

    String dataToEncryptDecrypt = "android";
    String encryptionDecryptionKey = "1234567812345678";
    String ivs = "12345678";

    byte[] encryptedData = encrypt(dataToEncryptDecrypt.getBytes(), encryptionDecryptionKey.getBytes(),
            ivs.getBytes());
    // here you will get the encrypted bytes. Now you can use Base64 encoding on these bytes, before sending to your web-service

    byte[] decryptedData = decrypt(encryptedData, encryptionDecryptionKey.getBytes(), ivs.getBytes());
    System.out.println(new String(decryptedData));

【讨论】:

  • 太棒了!我得到了字节的答案。现在我需要将其转换为 Base64。所以需要用到Base64Encoder。但如果我使用它,它会导入包“sun.misc.BASE64Encoder”。但是我们不应该使用 sun 包,因为oracle.com/technetwork/java/faq-sun-packages-142232.html 我该如何解决这个问题?
  • 由于您使用的是Android,如果API级别为8或更高,您可以使用Base64类本身。 developer.android.com/reference/android/util/Base64.html
  • 是的,我得到了它的工作。非常感谢。需要更多帮助,我的输出与那里的输出不同。所以在这种情况下将会是错误。输出的长度相同。我很接近回答了。
  • 谁能告诉这个PHP语言的等效代码是什么?
【解决方案2】:

不知道AES算法在使用中的细节(即mode & padding方法),大概是这样的:

public static byte[] encrypt(byte[] data, byte[] key) {
try {
    Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding");
    SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
    byte[] empty = new byte[16]; // For better security you should use a random 16 byte key!!!
    IvParameterSpec ivps = new IvParameterSpec(empty);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivps);
    return cipher.doFinal(data);
} catch (Exception e) {
    // ...
}

return null;
}

上面的函数可以这样使用:

String data = "android";
String key = "1234567812345678";
byte encrypted  = encrypt(data.getbytes("UTF-8"), key.getbytes("UTF-8"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 2012-02-21
    • 2022-04-29
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多