2012-04-27 8 views
1

私は基本的にRijndaelで暗号化されたサーバーから取得するパスワードを復号化する必要があります。以前は暗号化/復号化に携わっていませんでした。このWebのどこかに、Javaには外部ライブラリを必要とせずに実装されたメソッドがありますが、それを取得できませんでした。私が必要とするのは、私がサーバーに書き戻すことはないからです。AndroidでRijndaelを復号する

どのように行うか、どこでドキュメントを見つけることができますか?

答えコードhereが私が探しているものであるかどうかわかりませんが、私はそれほど理解していません。私が言ったように、

byte[] sessionKey = null; //Where you get this from is beyond the scope of this post 
byte[] iv = null ; //Ditto 
byte[] plaintext = null; //Whatever you want to encrypt/decrypt 
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
//You can use ENCRYPT_MODE or DECRYPT_MODE 
cipher.calling init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv)); 
byte[] ciphertext = cipher.doFinal(plaintext); 

ありがとうございます。

+0

が見えます。あなたはそれを使ってみましたか?私はそれが 'cipher.init(Cipher.DECRYPT_MODE、...')でなければならないと思います。セッションキーと初期化ベクトル(iv)をどこから得るか、 '' AES/CBC/PKCS5Padding ''あなたがしなければならないことは、暗号化されたパスワードを 'plaintext'に記入して、最後に' cyphertext'で解読することです。 – zapl

+0

私は主にそれを試しませんでした。ありがとうございます。 –

+0

@zapl明らかに.callingを解決できず、init()メソッドが見つかりません。どのようなアイデアですか? –

答えて

0

ここでは、あなたに役立つ単純な暗号ユーティリティのクラスです。 ferenc.hechlerが次のURLに投稿したコードに基づいています。 http://www.androidsnippets.com/encryptdecrypt-strings 私のニーズに合わせていくつかの変更を加えました。ここで

import java.math.BigInteger; 
import java.security.InvalidKeyException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 

import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.KeyGenerator; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.SecretKeySpec; 

public class SimpleCrypto { 

    private static final int KEY_SIZE = 128; 

    public static String encrypt(String seed, String cleartext) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { 
     final byte[] rawKey = getRawKey(seed.getBytes()); 
     final byte[] result = encrypt(rawKey, cleartext.getBytes()); 
     return bin2hex(result); 
    } 

    public static String decrypt(String seed, String encrypted) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { 
     final byte[] rawKey = getRawKey(seed.getBytes()); 
     final byte[] enc = toByte(encrypted); 
     final byte[] result = decrypt(rawKey, enc); 
     return new String(result); 
    } 

    public static String decrypt(String seed, byte[] encrypted) throws NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { 
     final byte[] rawKey = getRawKey(seed.getBytes()); 
     final byte[] result = decrypt(rawKey, encrypted); 
     return new String(result); 
    } 

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

    public static byte[] encrypt(byte[] raw, byte[] clear) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { 
     final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 
     final Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 
     final byte[] encrypted = cipher.doFinal(clear); 
     return encrypted; 
    } 

    public static byte[] decrypt(byte[] raw, byte[] encrypted) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { 
     final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 
     final Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.DECRYPT_MODE, skeySpec); 
     final byte[] decrypted = cipher.doFinal(encrypted); 
     return decrypted; 
    } 

    public static String toHex(String txt) { 
     return bin2hex(txt.getBytes()); 
    } 

    public static String fromHex(String hex) { 
     return new String(toByte(hex)); 
    } 

    public static byte[] toByte(String hexString) { 
     final int len = hexString.length()/2; 
     final 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 byte[] getHash(String str) { 
     MessageDigest digest = null; 
     try { 
      digest = MessageDigest.getInstance("SHA-256"); 
     } catch (NoSuchAlgorithmException ex) { 
      ex.printStackTrace(); 
     } 
     digest.reset(); 
     return digest.digest(str.getBytes()); 
    } 

    static String bin2hex(byte[] data) { 
     return String.format("%0" + (data.length * 2) + "X", new BigInteger(1, data)); 
    } 
} 

は、あなたが何かを復号化するためにそれを使用する方法である:それは、コードの右の部分であるように

final String ssid = "MY_WIFI_SSID"; 
    final String encWifiKey = "myEncryptedWifiKeyString"; 

    String wifiKey = ""; 
    try { 
     wifiKey = new String(SimpleCrypto.decrypt(SimpleCrypto.getHash(ssid), Base64.decode(encWifiKey, Base64.DEFAULT))); 
    } catch (InvalidKeyException e) { 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
     e.printStackTrace(); 
    } 
+0

それを試したことはありませんが、私が理解していないことの1つは、これについてのすべてのアプローチにあります。結果はバイト型のようです。私は文字列を持っていないはずですか?つまり、ユーザーが入力する文字列は文字列としてキャプチャされます。 –

+0

暗号化(または少なくともブロック暗号化)は通常、バイトのみで動作します。したがって、文字列は暗号化される前にバイトに変換され、復号化の後に逆にされる必要があります。通常これは、UTF-8などの特定の文字エンコーディングを使用して実行されます。最も使用されるUTF-8エンコーディングでこれを達成するには、 'new String(byte []、Charset.forName(" UTF-8 "))'を使用します。 –

+1

この例は使用しないでください!私は既にこの例に何度も遭遇しましたが、間違って実行できることはすべて*間違っています。 Akos Cz、あなたが理解していないコードを投稿しないでください。 –

関連する問題