java加解密程序源码(java源代码加密)
本文目录一览:
- 1、用java编一个程序(加密解密的)谢谢了,大神帮忙啊
- 2、java加密解密代码
- 3、谁能提供下java中有关加密和解密的代码
- 4、JAVA 文本文件用异或算法进行加密解密的程序代码!
- 5、求java加密源代码(MD5,base64)
用java编一个程序(加密解密的)谢谢了,大神帮忙啊
//楼上的是VB代码?太夸张了!呵呵! import java.util.*; public class Test { public static void main(String args[]) { String str; char[] ch; Scanner reader = new Scanner(System.in); str = reader.nextLine(); ch = str.toCharArray(); for(int i = 0; ich.length; i++) { if(ch[i]=='z') ch[i] = 'a'; else if(ch[i]=='Z') ch[i] = 'A'; else ch[i] += 1; } System.out.println("加密后 " + String.valueOf(ch)); for(int i = 0; ich.length; i++) { if(ch[i]=='a') ch[i] = 'z'; else if(ch[i]=='A') ch[i] = 'Z'; else ch[i] -= 1; } System.out.println("解密后 " + String.valueOf(ch)); } }
java加密解密代码
package com.cube.limail.util;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;/**
* 加密解密类
*/
public class Eryptogram
{
private static String Algorithm ="DES";
private String key="CB7A92E3D3491964";
//定义 加密算法,可用 DES,DESede,Blowfish
static boolean debug = false ;
/**
* 构造子注解.
*/
public Eryptogram ()
{
} /**
* 生成密钥
* @return byte[] 返回生成的密钥
* @throws exception 扔出异常.
*/
public static byte [] getSecretKey () throws Exception
{
KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );
SecretKey deskey = keygen.generateKey ();
System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
if (debug ) System.out.println ("生成密钥:"+bytesToHexString (deskey.getEncoded ()));
return deskey.getEncoded ();
} /**
* 将指定的数据根据提供的密钥进行加密
* @param input 需要加密的数据
* @param key 密钥
* @return byte[] 加密后的数据
* @throws Exception
*/
public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug )
{
System.out.println ("加密前的二进串:"+byte2hex (input ));
System.out.println ("加密前的字符串:"+new String (input ));
} Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.ENCRYPT_MODE ,deskey );
byte [] cipherByte =c1.doFinal (input );
if (debug ) System.out.println ("加密后的二进串:"+byte2hex (cipherByte ));
return cipherByte ;
} /**
* 将给定的已加密的数据通过指定的密钥进行解密
* @param input 待解密的数据
* @param key 密钥
* @return byte[] 解密后的数据
* @throws Exception
*/
public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception
{
SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );
if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));
Cipher c1 = Cipher.getInstance (Algorithm );
c1.init (Cipher.DECRYPT_MODE ,deskey );
byte [] clearByte =c1.doFinal (input );
if (debug )
{
System.out.println ("解密后的二进串:"+byte2hex (clearByte ));
System.out.println ("解密后的字符串:"+(new String (clearByte )));
} return clearByte ;
} /**
* 字节码转换成16进制字符串
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的16进制字符串
*/
public static String byte2hex (byte [] b )
{
String hs ="";
String stmp ="";
for (int n =0 ;n b.length ;n ++)
{
stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));
if (stmp.length ()==1 ) hs =hs +"0"+stmp ;
else hs =hs +stmp ;
if (n b.length -1 ) hs =hs +":";
} return hs.toUpperCase ();
}
/**
* 字符串转成字节数组.
* @param hex 要转化的字符串.
* @return byte[] 返回转化后的字符串.
*/
public static byte[] hexStringToByte(String hex) {
int len = (hex.length() / 2);
byte[] result = new byte[len];
char[] achar = hex.toCharArray();
for (int i = 0; i len; i++) {
int pos = i * 2;
result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));
}
return result;
}
private static byte toByte(char c) {
byte b = (byte) "0123456789ABCDEF".indexOf(c);
return b;
}
/**
* 字节数组转成字符串.
* @param String 要转化的字符串.
* @return 返回转化后的字节数组.
*/
public static final String bytesToHexString(byte[] bArray) {
StringBuffer sb = new StringBuffer(bArray.length);
String sTemp;
for (int i = 0; i bArray.length; i++) {
sTemp = Integer.toHexString(0xFF bArray[i]);
if (sTemp.length() 2)
sb.append(0);
sb.append(sTemp.toUpperCase());
}
return sb.toString();
}
/**
* 从数据库中获取密钥.
* @param deptid 企业id.
* @return 要返回的字节数组.
* @throws Exception 可能抛出的异常.
*/
public static byte[] getSecretKey(long deptid) throws Exception {
byte[] key=null;
String value=null;
//CommDao dao=new CommDao();
// List list=dao.getRecordList("from Key k where k.deptid="+deptid);
//if(list.size()0){
//value=((com.csc.sale.bean.Key)list.get(0)).getKey();
value = "CB7A92E3D3491964";
key=hexStringToByte(value);
//}
if (debug)
System.out.println("密钥:" + value);
return key;
}
public String encryptData2(String data) {
String en = null;
try {
byte[] key=hexStringToByte(this.key);
en = bytesToHexString(encryptData(data.getBytes(),key));
} catch (Exception e) {
e.printStackTrace();
}
return en;
}
public String decryptData2(String data) {
String de = null;
try {
byte[] key=hexStringToByte(this.key);
de = new String(decryptData(hexStringToByte(data),key));
} catch (Exception e) {
e.printStackTrace();
}
return de;
}
} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)
byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码
password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多
谁能提供下java中有关加密和解密的代码
public static void main(String[] args) throws Exception {
String data = "itxxz";
System.out.println("字符串:itxxz");
System.err.println("加密:"+encrypt(data));
System.err.println("解密:"+decrypt(encrypt(data)));
}
运行结果:
由于代码太多,可到 itxxz.com/a/javashili/2014/1217/encrypt_decrypt.html 查看,注释也比较完整,清晰易懂
JAVA 文本文件用异或算法进行加密解密的程序代码!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class XorExample {
public static final byte XOR_CONST = 0X12;
public static void main(String[] args) throws Exception {
File src = new File("level1.txt");
File dest = new File("level2.txt");
File dest2 = new File("level3.txt");
xorEn(src, dest);
xorEn(dest, dest2);
}
/**
* 异或的一个特点: a^b = c c^b = a
* 所以简单点,这里的加解密都用一个函数就行了
* @param src
* @param dest
* @throws Exception
*/
public static void xorEn(File src, File dest) throws Exception {
// 文件不存在或为文件夹就不判断了
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] bs = new byte[1024];
int len = 0;
while ((len = fis.read(bs)) != -1) {
for (int i = 0; i len; i++) {
bs[i] ^= XOR_CONST;
}
fos.write(bs, 0, len);
}
fos.close();
fis.close();
}
}
求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();
}
}
}