3

アプリケーションにパスワード文字列がありandroidます。.netWebサービスを使用して(つまり、で終わる.aspx) Webサービスを介してパスワードを送信する必要がありますSOAPAES 128パスワードを送信する前に、カスタムキーとIVを使用した暗号化でパスワードを暗号化する必要があります。

.netには、カスタムキーとIvを使用した暗号化/復号化ツールがあります。ツールは、16桁とIV8桁のカスタムキーを要求します。文字列を指定すると、暗号化文字列が生成されます。例

例:

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

私はアンドロイドでこれを行う方法がわかりませんでした。助けが必要。

4

2 に答える 2

10

完全な例が役立つ場合があります。

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));
于 2012-11-27T09:53:23.837 に答える
3

使用中の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"));
于 2012-11-27T08:58:14.377 に答える