java - 如何在Android上使用3DES算法?

标签 java android 3des

在服务器端,密码字段的加密/解密是用C#完成的。

现在,我需要在我的 android 应用程序中实现相同的功能。所以,我遵循了本教程:http://ttux.net/post/3des-java-encrypter-des-java-encryption/如下:

    import java.security.MessageDigest; 
import java.security.spec.KeySpec; 
import java.util.Arrays; 

import javax.crypto.Cipher; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.DESedeKeySpec; 
import javax.crypto.spec.IvParameterSpec; 

import org.apache.commons.codec.binary.Base64; 

public class Encrypter { 
  private KeySpec keySpec; 
  private SecretKey key; 
  private IvParameterSpec iv; 

  public Encrypter(String keyString, String ivString) { 
    try { 
      final MessageDigest md = MessageDigest.getInstance("md5"); 
      final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("utf-8"))); 
      final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); 
      for (int j = 0, k = 16; j < 8;) { 
    keyBytes[k++] = keyBytes[j++]; 
      } 

      keySpec = new DESedeKeySpec(keyBytes); 

      key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec); 

      iv = new IvParameterSpec(ivString.getBytes()); 
    } catch(Exception e) { 
      e.printStackTrace(); 
    } 
  } 

  public String encrypt(String value) { 
    try { 
      Cipher ecipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE"); 
      ecipher.init(Cipher.ENCRYPT_MODE, key, iv); 

      if(value==null) 
    return null; 

      // Encode the string into bytes using utf-8 
      byte[] utf8 = value.getBytes("UTF8"); 

      // Encrypt 
      byte[] enc = ecipher.doFinal(utf8); 

      // Encode bytes to base64 to get a string 
      return new String(Base64.encodeBase64(enc),"UTF-8"); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 

  public String decrypt(String value) { 
    try { 
      Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding","SunJCE"); 
      dcipher.init(Cipher.DECRYPT_MODE, key, iv); 

      if(value==null) 
    return null; 

      // Decode base64 to get bytes 
      byte[] dec = Base64.decodeBase64(value.getBytes()); 

      // Decrypt 
      byte[] utf8 = dcipher.doFinal(dec); 

      // Decode using utf-8 
      return new String(utf8, "UTF8"); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 
} 

但我不知道我需要为上述代码的 KeyValue 和 ivValue 提供什么值。请帮助我...

最佳答案

使用此代码加密您的字符串

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
//string encryption
public class EncryptionHelper {



    // Encrypts string and encode in Base64
    public static String encryptText(String plainText) throws Exception {
        // ---- Use specified 3DES key and IV from other source --------------
        byte[] plaintext = plainText.getBytes();//input
        byte[] tdesKeyData = Constants.getKey().getBytes();// your encryption key

        byte[] myIV = Constants.getInitializationVector().getBytes();// initialization vector

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

        c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);
        byte[] cipherText = c3des.doFinal(plaintext);
        String encryptedString = Base64.encodeToString(cipherText,
                Base64.DEFAULT);
        // return Base64Coder.encodeString(new String(cipherText));
        return encryptedString;
    }

}

这是加密字符串的方法

String encryptedPassword = EncryptionHelper.encryptText(edtText.getText().toString());

编辑 Constants.java

的代码
     Class Constants {
         private final String initializationVector = "INITALIZATION_VECTOR";
         private final String ecnryptionKey = "ENCRYPTION_KEY";
         public static String getInitializationVector() {
             return initializationVector;
         }
         public static String getKey() {
             return ecnryptionKey;
         }
     }

关于java - 如何在Android上使用3DES算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16336017/

相关文章:

javascript - Node.JS 中的 3des 加密返回无效的 IV 长度

java - 如何清除/重置/打开输入流,以便它可以在 Java 中的 2 种不同方法中使用?

java - JDBC 事务未回滚 - setAutoCommit(false)

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown column 'quantity' in 'field list'

android - TransformException : java. util.zip.ZipException : duplicate entry: android/support/annotation/StyleRes. 类

java - 无法在android中动态添加TextView

android - 聚焦 EditText 时如何使软键盘无效

java - 强制 Tomcat 通过 http 使用安全的 JSESSIONID cookie

.net - .NET 中的 "openssl enc -a -e -salt -des3 -pass pass:abc123"等价物是什么?

c# - TripleDESCryptoServiceProvider 的 openssl 等效项