android 자바 3des 암호 ECB/CBC

3962 단어 Android
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

import android.util.Base64;

public class Des3 {
public Des3() throws Exception {
//  
byte[] key=Base64.decode("12345678901234567890123456789000", Base64.DEFAULT);
byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
//    
String mainStr = "  ABCacb123";
//     byte
byte[] data = mainStr.getBytes("UTF-8");
System.out.println("    :" + mainStr);
System.out.println("ECB    ");
//    byte[]
byte[] encodeByte_ECB = des3EncodeECB(key, data);
//    String        byte[] base64
String encodeString_ECB =Base64.encodeToString(encodeByte_ECB, Base64.DEFAULT);
System.out.println("      :" + encodeString_ECB);
//      byte[]       byte[] bass64
byte[] decodeByteMain_ECB=Base64.decode(encodeString_ECB, Base64.DEFAULT);
//    byte[]
byte[] decodeByte_ECB = ees3DecodeECB(key, decodeByteMain_ECB);
//    String
String decodeString_ECB = new String(decodeByte_ECB, "UTF-8");
System.out.println("       :" + decodeString_ECB);
System.out.println("CBC    ");
//    byte[]
byte[] encodeByte_CBC = des3EncodeCBC(key, keyiv,data);
//    String        byte[] base64
String encodeString_CBC =Base64.encodeToString(encodeByte_CBC, Base64.DEFAULT);
System.out.println("      :" + encodeString_CBC);
//      byte[]       byte[] bass64
byte[] decodeByteMain_CBC=Base64.decode(encodeString_CBC, Base64.DEFAULT);
//    byte[]
byte[] decodeByte_CBC = des3DecodeCBC(key, keyiv, decodeByteMain_CBC);
//    String
String decodeString_CBC = new String(decodeByte_CBC, "UTF-8");
System.out.println("       :" + decodeString_CBC);
}

public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}

public static byte[] ees3DecodeECB(byte[] key, byte[] data)
throws Exception {

Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;

}

    public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] bOut = cipher.doFinal(data);

        return bOut;
    }

   
    public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
            throws Exception {

        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(key);
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
        deskey = keyfactory.generateSecret(spec);

        Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(keyiv);

        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

        byte[] bOut = cipher.doFinal(data);

        return bOut;

    }
}

다음으로 전송:http://blog.sina.com.cn/s/blog_677fb16e01018eqw.html

좋은 웹페이지 즐겨찾기