2015-04-03 44 views

回答

-1

您可以使用AES,DES和3DES,它們都包含在java中。我從這裏發佈了一個簡單的程序http://sanjaal.com/java/186/java-encryption/tutorial-java-des-encryption-and-decryption/這是使用DES加密/解密

import java.security.spec.KeySpec;

import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESKeySpec; 
import sun.misc.BASE64Decoder; 
import sun.misc.BASE64Encoder; 

class DESEncryption { 

    private static final String UNICODE_FORMAT = "UTF8"; 
    public static final String DES_ENCRYPTION_SCHEME = "DES"; 
    private KeySpec myKeySpec; 
    private SecretKeyFactory mySecretKeyFactory; 
    private Cipher cipher; 
    byte[] keyAsBytes; 
    private String myEncryptionKey; 
    private String myEncryptionScheme; 
    SecretKey key; 

    public DESEncryption() throws Exception 
    { 
     myEncryptionKey = "ThisIsSecretEncryptionKey"; 
     myEncryptionScheme = DES_ENCRYPTION_SCHEME; 
     keyAsBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); 
     myKeySpec = new DESKeySpec(keyAsBytes); 
     mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme); 
     cipher = Cipher.getInstance(myEncryptionScheme); 
     key = mySecretKeyFactory.generateSecret(myKeySpec); 
    } 

    /** 
    * Method To Encrypt The String 
    */ 
    public String encrypt(String unencryptedString) { 
     String encryptedString = null; 
     try { 
      cipher.init(Cipher.ENCRYPT_MODE, key); 
      byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT); 
      byte[] encryptedText = cipher.doFinal(plainText); 
      BASE64Encoder base64encoder = new BASE64Encoder(); 
      encryptedString = base64encoder.encode(encryptedText); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 
    /** 
    * Method To Decrypt An Ecrypted String 
    */ 
    public String decrypt(String encryptedString) { 
     String decryptedText=null; 
     try { 
      cipher.init(Cipher.DECRYPT_MODE, key); 
      BASE64Decoder base64decoder = new BASE64Decoder(); 
      byte[] encryptedText = base64decoder.decodeBuffer(encryptedString); 
      byte[] plainText = cipher.doFinal(encryptedText); 
      decryptedText= bytes2String(plainText); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return decryptedText; 
    } 


    private static String bytes2String(byte[] bytes) { 
     StringBuffer stringBuffer = new StringBuffer(); 
     for (int i = 0; i < bytes.length; i++) { 
      stringBuffer.append((char) bytes[i]); 
     } 
     return stringBuffer.toString(); 
    } 

    /** 
    * Testing the DES Encryption And Decryption Technique 
    */ 
    public static void main(String args []) throws Exception 
    { 
     DESEncryption myEncryptor= new DESEncryption(); 

     String stringToEncrypt="Sanjaal.com"; 
     String encrypted=myEncryptor.encrypt(stringToEncrypt); 
     String decrypted=myEncryptor.decrypt(encrypted); 

     System.out.println("String To Encrypt: "+stringToEncrypt); 
     System.out.println("Encrypted Value :" + encrypted); 
     System.out.println("Decrypted Value :"+decrypted); 

    } 

} 
+1

它花了一點兒弄亂,以獲得加密功能在Android上工作,但我得到它的工作。 非常感謝!如果其他人發現這一點,這是一個androidified版本:0​​ http://pastebin.com/aByHyd7x – Boncey 2015-04-03 19:57:46

+0

1.您的代碼有一些html文物。請刪除這些。 2.你不應該建議人們使用DES。使用AES或3DES或Blowfish。 DES很容易暴力破解。 3.您需要具有密文完整性,因此應該添加HMAC,或者更加簡單,如GCM等經過身份驗證的模式。 4.總是使用完全合格的Cipher字符串,如「AES/CBC/PKCS5Padding」(也是@Boncey) – 2015-04-03 20:16:51

+0

我會研究AES加密。無論如何,我打算這麼做,但我現在只進行測試。感謝您的建議 :) – Boncey 2015-04-03 23:38:47