xml加密java
java DES 加密 解密 生成随机密钥
举例说明:
//保存生成的key
public static void saveDesKey() {
try {
SecureRandom sr = new SecureRandom();
// 选择的DES算法生成一个KeyGenerator对象
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(sr);
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// 绝对路径
String fileName = "d:/DesKey.xml";
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// 生成密钥
Key key = kg.generateKey();
oos.writeObject(key);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//获取生成的key
public static Key getKey() {
Key kp = null;
try {
// 相对路径 需要新建 conf 文件夹
// String fileName = "conf/DesKey.xml";
// InputStream is = Encrypt.class.getClassLoader().getResourceAsStream(fileName);
// 绝对路径
String fileName = "d:/DesKey.xml";
FileInputStream is = new FileInputStream(fileName);
ObjectInputStream oos = new ObjectInputStream(is);
kp = (Key) oos.readObject();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
return kp;
}
//加密开始
public static void encrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
}
//解密开始
public static void decrypt(String file, String dest) throws Exception {
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, getKey());
InputStream is = new FileInputStream(file);
OutputStream out = new FileOutputStream(dest);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
out.close();
is.close();
}
}
//des加密主方法
public class DES {
public static void main(String[] args) throws Exception {
Encrypt.saveDesKey();
System.out.println("生成key");
Encrypt.getKey();
System.out.println("获取key");
Encrypt.encrypt("d:\\hello.txt", "d:\\encrypt.txt");
System.out.println("加密");
Encrypt.decrypt("d:\\encrypt.txt", "d:\\decrypt.txt");
System.out.println("解密");
}
以上方法亲测可用。
B. java项目中一般需要用到xml技术的哪些方面
XML的主要作用有两个方面:数据交换和信息配置。在做数据交换时,XML将数据用标签组装成起来,然后压缩打包加密后通过网络传送给接收者,接收解密与解压缩后再从XML文件中还原相关信息进行处理,XML曾经是异构系统间交换数据的事实标准,但此项功能几乎已经被JSON(JavaScript Object Notation)取而代之。当然,目前很多软件仍然使用XML来存储配置信息,我们在很多项目中通常也会将作为配置信息的硬代码写在XML文件中,Java的很多框架也是这么做的,而且这些框架都选择了dom4j作为处理XML的工具,因为Sun公司的官方API实在不怎么好用。
补充:现在有很多时髦的软件(如Sublime)已经开始将配置文件书写成JSON格式,我们已经强烈的感受到XML的另一项功能也将逐渐被业界抛弃。
C. 求大神用java实现RC4的加密,解密功能,高分悬赏.
importjavax.crypto.Cipher;
importjavax.crypto.spec.SecretKeySpec;
importjavax.xml.bind.DatatypeConverter;
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Ciphercipher=Cipher.getInstance("RC4");
Stringpwd="123456";
Stringptext="HelloWorld你好";
SecretKeySpeckey=newSecretKeySpec(pwd.getBytes("UTF-8"),"RC4");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[]cdata=cipher.update(ptext.getBytes("UTF-8"));
//解密
cipher.init(Cipher.DECRYPT_MODE,key);
byte[]ddata=cipher.update(cdata);
System.out.println("密码:"+pwd);
System.out.println("明文:"晌隐+ptext);
System.out.println("密文:"+DatatypeConverter.printHexBinary(cdata));
System.out.println("解密文:"+newString(ddata,"UTF-8"));
}
}
密码:123456
明文:HelloWorld你镇戚好
密文:
解密文御谨陵:HelloWorld你好
RC4已经不太安全,只能用于一般加密,不能用于金融等紧要场合。
D. 鍦╦ava涓浠涔堟槸api,jdk,jdbc,xml,瀹冧滑鍏蜂綋鏄浠涔,鎴戝硅繖浜涗笉鏄寰堜简瑙,鑳芥棦鑳界亩鍗曞张鑳芥槑锏界殑璇濅粙缁嶅悧璋㈣阿.
鎭ㄥソ锛屾彁闂钥咃细
API锛氭槸sun鍏鍙哥殑绫绘柟娉曘佹帴鍙g瓑镄勮В璇达纴鎴戜滑绋嫔簭锻樼粺绉板府锷╂枃妗c
JDK锛氭槸java镄勫畨瑁呯▼搴忥纴JDK閲岄溃鍖呭惈jre銆
JDBC锛氭槸涓绉岖敤浜庢墽琛孲QL璇鍙ョ殑Java API锛屽彲浠ヤ负澶氱嶅叧绯绘暟鎹搴撴彁渚涚粺涓璁块梾锛屽畠鐢变竴缁勭敤 Java璇瑷缂栧啓镄勭被鍜屾帴鍙g粍鎴愩侸DBC 鍙锅氢笁浠朵簨锛氢笌鏁版嵁搴揿缓绔嬭繛鎺ャ佸彂阃 SQL 璇鍙ュ苟澶勭悊缁撴灉銆
XML锛歑ML鏄鍙镓╁𪾢镙囱瘑璇瑷锛孲GML銆丠TML鏄疿ML镄勫厛椹便