Java如何加密源代码 java加密文件

JAVA简单文件加密 求JAVA源代码

md5加密:

目前创新互联建站已为近千家的企业提供了网站建设、域名、网络空间、网站托管维护、企业网站设计、仓山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

package com.ncs.pki.util;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

public class MD5Test {

private static MessageDigest digest = null;

public synchronized static final String hash(String data) {

if (digest == null) {

try {

digest = MessageDigest.getInstance("MD5");

} catch (NoSuchAlgorithmException nsae) {

System.err.println(

"Failed to load the MD5 MessageDigest. "

+ "Jive will be unable to function normally.");

nsae.printStackTrace();

}

}

// Now, compute hash.

digest.update(data.getBytes());

return encodeHex(digest.digest());

}

public static final String encodeHex(byte[] bytes) {

StringBuffer buf = new StringBuffer(bytes.length * 2);

int i;

for (i = 0; i bytes.length; i++) {

if (((int) bytes[i] 0xff) 0x10) {

buf.append("0");

}

buf.append(Long.toString((int) bytes[i] 0xff, 16));

}

return buf.toString();

}

public static String test(){

return null;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

System.out.println(MD5Test.hash("123456"));

}

}

3des加密:

package com.ncs.pki.util;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class DesEncrypt {

/**

*

* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.

*

* 方法: void getKey(String strKey)从strKey的字条生成一个Key

*

* String getEncString(String strMing)对strMing进行加密,返回String密文 String

* getDesString(String strMi)对strMin进行解密,返回String明文

*

*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]

* byteD)byte[]型的解密

*/

Key key;

/**

* 根据参数生成KEY

*

* @param strKey

*/

public void getKey(String strKey) {

try {

KeyGenerator _generator = KeyGenerator.getInstance("DES");

_generator.init(new SecureRandom(strKey.getBytes()));

this.key = _generator.generateKey();

_generator = null;

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 加密String明文输入,String密文输出

*

* @param strMing

* @return

*/

public String getEncString(String strMing) {

byte[] byteMi = null;

byte[] byteMing = null;

String strMi = "";

BASE64Encoder base64en = new BASE64Encoder();

try {

byteMing = strMing.getBytes("UTF8");

byteMi = this.getEncCode(byteMing);

strMi = base64en.encode(byteMi);

} catch (Exception e) {

e.printStackTrace();

} finally {

base64en = null;

byteMing = null;

byteMi = null;

}

return strMi;

}

/**

* 解密 以String密文输入,String明文输出

*

* @param strMi

* @return

*/

public String getDesString(String strMi) {

BASE64Decoder base64De = new BASE64Decoder();

byte[] byteMing = null;

byte[] byteMi = null;

String strMing = "";

try {

byteMi = base64De.decodeBuffer(strMi);

byteMing = this.getDesCode(byteMi);

strMing = new String(byteMing, "UTF8");

} catch (Exception e) {

e.printStackTrace();

} finally {

base64De = null;

byteMing = null;

byteMi = null;

}

return strMing;

}

/**

* 加密以byte[]明文输入,byte[]密文输出

*

* @param byteS

* @return

*/

private byte[] getEncCode(byte[] byteS) {

byte[] byteFina = null;

Cipher cipher;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.ENCRYPT_MODE, key);

byteFina = cipher.doFinal(byteS);

} catch (Exception e) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

/**

* 解密以byte[]密文输入,以byte[]明文输出

*

* @param byteD

* @return

*/

private byte[] getDesCode(byte[] byteD) {

Cipher cipher;

byte[] byteFina = null;

try {

cipher = Cipher.getInstance("DES");

cipher.init(Cipher.DECRYPT_MODE, key);

byteFina = cipher.doFinal(byteD);

} catch (Exception e) {

e.printStackTrace();

} finally {

cipher = null;

}

return byteFina;

}

public static void main(String[] args) {

System.out.println("des demo");

DesEncrypt des = new DesEncrypt();// 实例化一个对像

des.getKey("MYKEY");// 生成密匙

System.out.println("key=MYKEY");

String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文

System.out.println("密文=" + strEnc);

String strDes = des.getDesString(strEnc);// 把String 类型的密文解密

System.out.println("明文=" + strDes);

}

}

求java加密源代码(MD5,base64)

import java.security.*;

import javax.crypto.*;

/**

* 本例解释如何利用DES私钥加密算法加解密

*

* @author Devon

* @version 1.0 04/03/10

*/

public class SingleKeyExample {

public static void main(String[] args) {

try {

String algorithm = "DES"; //定义加密算法,可用 DES,DESede,Blowfish

String message = "Hello World. 这是待加密的信息";

// 生成个DES密钥

KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);

keyGenerator.init(56); //选择DES算法,密钥长度必须为56位

Key key = keyGenerator.generateKey(); //生成密钥

// 生成Cipher对象

Cipher cipher = Cipher.getInstance("DES");

//用密钥加密明文(message),生成密文(cipherText)

cipher.init(Cipher.ENCRYPT_MODE, key); //操作模式为加密(Cipher.ENCRYPT_MODE),key为密钥

byte[] cipherText = cipher.doFinal(message.getBytes()); //得到加密后的字节数组

System.out.println("加密后的信息: " + new String(cipherText));

//用密钥加密明文(plainText),生成密文(cipherByte)

cipher.init(Cipher.DECRYPT_MODE, key); //操作模式为解密,key为密钥

byte[] sourceText = cipher.doFinal(cipherText); //获得解密后字节数组

System.out.println("解密后的信息: " + new String(sourceText));

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

/**

* @author Devon

*/

import java.security.*;

import java.security.spec.*;

import javax.crypto.*;

public class PairKeyExample {

public static void main(String argv[]) {

try {

String algorithm = "RSA"; //定义加密算法,可用 DES,DESede,Blowfish

String message = "张三,你好,我是李四";

//产生张三的密钥对(keyPairZhang)

KeyPairGenerator keyGeneratorZhang =

KeyPairGenerator.getInstance(algorithm); //指定采用的算法

keyGeneratorZhang.initialize(1024); //指定密钥长度为1024位

KeyPair keyPairZhang = keyGeneratorZhang.generateKeyPair(); //产生密钥对

System.out.println("生成张三的公钥对");

// 张三生成公钥(publicKeyZhang)并发送给李四,这里发送的是公钥的数组字节

byte[] publicKeyZhangEncode = keyPairZhang.getPublic().getEncoded();

//通过网络或磁盘等方式,把公钥编码传送给李四

//李四接收到张三编码后的公钥,将其解码

KeyFactory keyFacoryLi = KeyFactory.getInstance(algorithm); //得到KeyFactory对象

X509EncodedKeySpec x509KeySpec =

new X509EncodedKeySpec(publicKeyZhangEncode); //公钥采用X.509编码

PublicKey publicKeyZhang = keyFacoryLi.generatePublic(x509KeySpec); //将公钥的KeySpec对象转换为公钥

System.out.println("李四成功解码,得到张三的公钥");

//李四用张三的公钥加密信息,并发送给李四

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); //得到Cipher对象

cipher.init(Cipher.ENCRYPT_MODE, publicKeyZhang); //用张三的公钥初始化Cipher对象

byte[] cipherMessage = cipher.doFinal(message.getBytes()); //得到加密信息

System.out.println("加密后信息:" + new String(cipherMessage));

System.out.println("加密完成,发送给李四...");

//张三用自己的私钥解密从李四处收到的信息

cipher.init(Cipher.DECRYPT_MODE, keyPairZhang.getPrivate()); //张三用其私钥初始化Cipher对象

byte[] originalMessage = cipher.doFinal(cipherMessage); //得到解密后信息

System.out.println("张三收到信息,解密后为:" + new String(originalMessage));

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

java代码想加密怎么处理?

如果你说的是文本加密,有很多方法,自己也可以写个字符变换程序

如果是代码加密,没用的,java就是开源。

你藏再厉害,编译+反编译,干净的源码就出来了

java程序加密

都看不到的,

java程序分为:

编辑

编译

解释

运行

运行的都是.class字节码文件,看不到源文件的,你不用操心

采纳即可

怎样为一个java程序加密? 谢谢

只给编译后的.jar文件,不给.java文件

不过要说明的是,java因为是字节码,所以没有办法防止被反编译。

最多也就是做一下代码混淆,比如把方法或变量名改成无意义的名称,或者加一些完全无用的代码进去,让恶意攻击的人难以看懂


当前标题:Java如何加密源代码 java加密文件
浏览路径:http://ybzwz.com/article/doseoig.html