0

私は大きな問題を抱えています。別のデバイスにメッセージを送信するために暗号化を使用するアプリを開発しました。

ここで暗号クラス、私はそれを「盗む」が、どこにあるか覚えていない:

public class AesCipher {
    public static String encrypt(String seed, String cleartext) throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] result = encrypt(rawKey, cleartext.getBytes());
        return toHex(result);
    }

    public static String decrypt(String seed, String encrypted) throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] enc = toByte(encrypted);
        byte[] result = decrypt(rawKey, enc);
        return new String(result);
    }

    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(seed);
        kgen.init(256,sr); // 192 and 256 bits may not be available
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return raw;
    }


    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
        try{
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] decrypted = cipher.doFinal(encrypted);
            return decrypted;
        }
        catch (Exception e) {return encrypted;}
    }

    public static String toHex(String txt) {
        return toHex(txt.getBytes());
    }
    public static String fromHex(String hex) {
        return new String(toByte(hex));
    }

    public static byte[] toByte(String hexString) {
        int len = hexString.length()/2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
        return result;
    }

    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2*buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }
    private final static String HEX = "0123456789ABCDEF";
    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
    }
}

したがって、同じデバイスにメッセージを送信すると、アプリは正常に機能しますが、異なるデバイスバージョン(andorid2.3とandroid4.0)を使用すると、受信者はメッセージを復号化できません。

調べてみると、問題はSecureRandomであり、さまざまな実装での互換性を保証できないことがわかりました。どうすれば解決できますか?

私の英語でごめんなさい...

4

1 に答える 1

1

はい、私はそのひどい例を削除しようとしましたが、役に立ちませんでした。

2.3デバイスからバイトを取得し、結果のraw[]バイト配列を使用してを作成する必要がありSecretKeySpecます。これをキーとして直接使用SecretKeySpecして、2.3デバイスで暗号化したものをすべて復号化できます。

残念ながら、4つのデバイスを使用して何かを暗号化し、「破棄」したraw[]場合、唯一のオプションは中断する"AES/ECB/PKCS5Padding"ことです(を使用する場合のデフォルト"AES")。これも安全ではないため、ECBモードからわずかな情報を取得できる可能性がありますが、そうでない場合は、AES暗号文のセキュリティを破ることになります-そしてこの世界の誰も-私たちの知る限り-それを行うことはできません。


コードを作成するときに自分でパスワードから始める場合は、最初にパスワードベースの鍵導出関数を呼び出して、パスワードから鍵を作成する必要があります。JavaにはPBKDF2が組み込まれています。

于 2012-08-02T22:43:06.823 に答える