如何在android中使用特殊字符加密字符串?

问题描述:

我需要加密用户名和密码,并对它们进行编码并通过SOAP头发送它。 我用DES算法对明文`如何在android中使用特殊字符加密字符串?

String key = "qwer1234qwetr123wqw"; 
     String x = "sadgsagd:%%^%ghsagdh"; 
     byte[] keyBytes = new byte[1024]; 
     byte[] plaintext = x.getBytes(); 
     byte[] tdesKeyData = key.getBytes(); 

    Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding"); 
    SecretKeySpec myKey = new SecretKeySpec(tdesKeyData, "DESede"); 
    IvParameterSpec ivspec = new IvParameterSpec(keyBytes); 

    c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec); 
    byte[] cipherText = c3des.doFinal(plaintext); 
    int hash = Base64.encode(cipherText).hashCode(); 

    return Base64.encode(cipherText);` 

加密在运行时,我得到一个例外:

javax.crypto.BadPaddingException:鉴于

+0

你的字节缓冲区大小是多少? – 2013-04-24 04:54:00

如果您正在使用字节的缓冲区的64,我可能是太长时间更改为32, 或使用的file像这样byte[] buffer = new byte[(int)new File("data").length()];

这样做后我的问题就解决了..