2012-04-27 81 views
1

我基本上需要解密從Rijndael加密的服務器上檢索的密碼,之前我從來沒有使用過加密/解密,而且完全丟失了。我發現在這個網絡的某個地方,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 2012-04-27 21:11:13

+0

我沒有嘗試過,主要是因爲我沒有't知道在哪裏放置密碼檢索,現在就像你說的那樣嘗試它。謝謝。 – 2012-04-27 21:22:51

+0

@zapl顯然它無法解決.calling並找不到init()方法。任何想法? – 2012-04-27 22:02:08

回答

0

這是一個簡單的加密工具類,可能對您有所幫助。它基於ferenc.hechler編寫的代碼發佈在以下網址: 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

還沒有嘗試過,但有一點我不明白,在我看到的每一種方法中,結果似乎都是一種字節類型。解密後不應該有一個字符串?我的意思是,用戶輸入的內容是以字符串形式捕獲的,因此...... – 2012-04-27 23:50:23

+0

加密(或至少是分組密碼)通常只能用於字節。因此,字符串需要在加密之前轉換爲字節,並在解密後轉回。通常這是通過使用特定的字符編碼(如UTF-8)來執行的。使用'new String(byte [],Charset.forName(「UTF-8」))'來實現最常用的UTF-8編碼。 – 2012-05-10 01:21:23

+1

不要使用這個例子!我已經多次遇到過這個例子,它可以做錯誤的事情。阿科斯Cz,不要發佈你不明白的代碼。 – 2012-05-10 01:24:01