Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android和.Net中的AES 128加密,带有自定义密钥和IV_Java_Android_Asp.net_Aes - Fatal编程技术网

Java Android和.Net中的AES 128加密,带有自定义密钥和IV

Java Android和.Net中的AES 128加密,带有自定义密钥和IV,java,android,asp.net,aes,Java,Android,Asp.net,Aes,我在我的android应用程序中有一个密码字符串。我需要使用SOAPweb服务通过.netweb服务发送密码(即以.aspx结尾)。在发送密码之前,我需要使用自定义密钥和IV使用AES 128加密密码 他们在.net中有一个带有自定义密钥和Iv的加密/解密工具。该工具要求使用16位和Iv 8位的自定义密钥。如果给定字符串,则生成加密字符串。范例 例如: Key : 1234567812345678 IV : 12345678 String : android Encrypted string :

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

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

例如:

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

我不知道如何在安卓系统中做到这一点。需要帮助。

我不知道正在使用的AES算法(即模式和填充方法)的详细信息,但应该大致如下:

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"));
上面的函数可以这样使用:

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"));

完整的示例可以帮助您:

加密/解密函数,使用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。所以需要使用Base64编码器。但如果我使用它,它将导入包“sun.misc.BASE64Encoder”。但是我们不应该使用sun软件包,因为我如何解决这个问题?因为您使用的是Android,如果API级别为8或更高,那么您可以使用
Base64
类本身。是的,我让它工作了。太多了。需要更多帮助,我的输出与那里的输出不同。因此,在这种情况下,我们将是错误的。输出的长度相同。我马上就要回答了。有人能告诉我PHP语言中的等效代码是什么吗?