0
A
回答
-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,PHP
- 2. Android到PHP加密代碼
- 3. PHP - 解密加密密碼
- 4. C到PHP的密碼加密(逆轉)
- 5. PHP Salt密碼加密
- 6. Android/PHP RSA加密/解密?
- 7. 加密密碼觸發器
- 8. 加密密碼以匹配存儲的加密密碼。
- 9. PHP鹽加密/解密密碼
- 10. 使用php加密和解密密碼
- 11. Android的密碼加密/解密
- 12. C#加密到PHP解密
- 13. 加密Android代碼?
- 14. 加密密碼
- 15. 密碼加密
- 16. 加密密碼
- 17. 加密密碼
- 18. 加密密碼
- 19. 加密密碼
- 20. 密碼加密
- 21. 密碼加密
- 22. 密碼加密
- 23. 無法比較/解密密碼(PHP/Android)
- 24. VS2012發佈配置文件 - 加密密碼
- 25. 如何在從登錄表單發佈之前加密密碼?
- 26. Android Facebook密鑰需要在應用發佈時加密嗎?
- 27. PHP解密密碼
- 28. 如何將加密密碼設置爲證書以使用sbt發佈Nexus OSS?
- 29. php數據庫myisam密碼加密
- 30. PHP TripleDES密碼加密註冊表格
它花了一點兒弄亂,以獲得加密功能在Android上工作,但我得到它的工作。 非常感謝!如果其他人發現這一點,這是一個androidified版本:0 http://pastebin.com/aByHyd7x – Boncey 2015-04-03 19:57:46
1.您的代碼有一些html文物。請刪除這些。 2.你不應該建議人們使用DES。使用AES或3DES或Blowfish。 DES很容易暴力破解。 3.您需要具有密文完整性,因此應該添加HMAC,或者更加簡單,如GCM等經過身份驗證的模式。 4.總是使用完全合格的Cipher字符串,如「AES/CBC/PKCS5Padding」(也是@Boncey) – 2015-04-03 20:16:51
我會研究AES加密。無論如何,我打算這麼做,但我現在只進行測試。感謝您的建議 :) – Boncey 2015-04-03 23:38:47