文档库

最新最全的文档下载
当前位置:文档库 > java版3des加密程序

java版3des加密程序

java版3des加密程序,可与php兼容

代码: import java.io.UnsupportedEncodingException; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.Se

代码:

import java.io.UnsupportedEncodingException;

import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESedeKeySpec;

import javax.crypto.spec.IvParameterSpec;

import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DESCoder

{

private static BASE64Encoder base64 = new BASE64Encoder();

private static byte[] myIV = { 50, 51, 52, 53, 54, 55, 56, 57 };

//private static String strkey = "W9qPIzjaVGKUp7CKRk/qpCkg/SCMkQRu"; // 字节数必须是8的倍数

private static String strkey = "01234567890123456789012345678912";

public static String desEncrypt(String input) throws Exception

{

BASE64Decoder base64d = new BASE64Decoder();

DESedeKeySpec p8ksp = null;

p8ksp = new DESedeKeySpec(base64d.decodeBuffer(strkey));

Key key = null;

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

input = padding(input);

byte[] plainBytes = (byte[])null;

Cipher cipher = null;

byte[] cipherText = (byte[])null;

plainBytes = input.getBytes("UTF8");

cipher = Cipher.getInstance("DESede/CBC/NoPadding");

java版3des加密程序

(共4页)