字符串比较加密和解密android中的密钥

时间:2014-01-17 15:27:52

标签: android encryption public-key-encryption string-matching

我正在使用此代码加密和解密文本

public class Encryption {
private static String charsetName = "UTF8";
private static String algorithm = "DES";
private static int base64Mode = Base64.DEFAULT;

public String getCharsetName() {
    return charsetName;
}

public void setCharsetName(String charsetName) {
    this.charsetName = charsetName;
}

public String getAlgorithm() {
    return algorithm;
}

public void setAlgorithm(String algorithm) {
    this.algorithm = algorithm;
}

public int getBase64Mode() {
    return base64Mode;
}

public void setBase64Mode(int base64Mode) {
    this.base64Mode = base64Mode;
}

public static String encrypt(String key, String data) {
    if (key == null || data == null)
        return null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        byte[] dataBytes = data.getBytes(charsetName);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.encodeToString(cipher.doFinal(dataBytes), base64Mode);
    } catch (Exception e) {
        return null;
    }
}

public static String decrypt(String key, String data) {
    if (key == null || data == null)
        return null;
    try {
        byte[] dataBytes = Base64.decode(data, base64Mode);
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(charsetName));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
        SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
        return new String(dataBytesDecrypted);
    } catch (Exception e) {
        return null;
    }
}

}   

我正在尝试匹配我为加密和放置的密钥。在解密之前,如果两个密钥都匹配解密文本,否则提示我错误的密钥这里是我正在尝试做的事情

string a="sfdsfsjbgdfkbgdfkjbgk";//encrypted text just for assuming 

decrypted=Encryption.decrypt(key, a.trim());

encrypted=Encryption.encrypt(key, decrypted.trim());

if(a.contentEquals(encrypted.trim())){
//show the decrypted text
}

else 
{
//Show the wrong key
}

这是我正在尝试做的,但当我把正确的键else语句执行 和应用程序在if语句上停止 任何人都可以指出我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

我无法重现你的问题。这里有一点SSCCE未能证明问题:

注意:如果您提供的代码示例也是SSCCE ,这会更容易)

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;

import android.util.Base64

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class EncryptDecryptTest {

  public static void main(String[] args) throws Exception {
    String key = "12345678";
    String ciphertext = encrypt(key, "foo");

    String decrypted = decrypt(key, ciphertext.trim());
    String encrypted = encrypt(key, decrypted.trim());

    if (ciphertext.contentEquals(encrypted.trim())) {
      System.out.println("decrypted!");
    } else {
      System.out.println("wrong key!");
    }
  }

  public static String encrypt(String key, String data)
      throws GeneralSecurityException, UnsupportedEncodingException {
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    byte[] dataBytes = data.getBytes("UTF8");
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return Base64.encodeToString(cipher.doFinal(dataBytes), Base64.DEFAULT);
  }

  public static String decrypt(String key, String data)
      throws GeneralSecurityException, UnsupportedEncodingException {
    byte[] dataBytes = Base64.decode(data, Base64.DEFAULT);
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF8"));
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] dataBytesDecrypted = (cipher.doFinal(dataBytes));
    return new String(dataBytesDecrypted);
  }    
}

对我来说,这会按预期打印decrypted!。请注意,我使用标准Java(不是Android)执行了测试,但我尝试修复在Android上执行的代码。

一些评论意见:

  • 您对trim()的不断使用似乎很奇怪。它似乎没有实现任何目标。
  • 当您可以使用contentEquals()时,可以使用equals()
  • 您指定DES作为算法,它将密码模式和填充保留为默认值。你真的应该明确指出这两个。
  • DES太弱而无法提供任何安全性,因此您可能需要考虑转向更强大的算法。