• ADADADADAD

    字符数据如何用DES加密[ 网络知识 ]

    网络知识 时间:2024-11-20 12:07:22

    作者:文/会员上传

    简介:

    在Java中使用DES加密算法对字符数据进行加密,具体方法如下:public class DesTool {private static final String PASSKEY = "afasdf";private static final String DESKEY = "

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    在Java中使用DES加密算法对字符数据进行加密,具体方法如下:

    public class DesTool {

    private static final String PASSKEY = "afasdf";

    private static final String DESKEY = "asfsdfsdf";

    public static String encoderOrdecoder( String src, int mode) {

    String tag="";

    InputStream is = null;

    OutputStream out = null;

    CipherInputStream cis = null;

    try {

    SecureRandom sr = new SecureRandom();

    DESKeySpec dks = new DESKeySpec(DESKEY.getBytes());

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");

    SecretKey securekey = keyFactory.generateSecret(dks);

    IvParameterSpec iv = new IvParameterSpec(PASSKEY.getBytes());

    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

    cipher.init(mode, securekey, iv, sr);

    cis = new CipherInputStream(new ByteArrayInputStream(src.getBytes()) , cipher);

    out=new ByteArrayOutputStream();

    byte[] buffer = new byte[1024];

    int r;

    while ((r = cis.read(buffer)) > 0) {

    out.write(buffer, 0, r);

    }

    tag=out.toString();

    } catch (Exception e) {

    e.printStackTrace();

    } finally {

    try {

    if (is != null) {

    is.close();

    }

    if (cis != null) {

    cis.close();

    }

    if (out != null) {

    out.close();

    }

    } catch (Exception e1){

    }

    }

    return tag;

    }

    public static void main(String[] args) {

    System.out.println("aaa");

    String t=encoderOrdecoder("aaa", Cipher.ENCRYPT_MODE );

    System.out.println(t);

    System.out.println(encoderOrdecoder(t, Cipher.DECRYPT_MODE ));

    }

    }

    字符数据如何用DES加密.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: 数据加密DES加密