当前位置: 技术问答>java相关
有没有使用java.security类来加密文本(不是数字签名)的程序?可否给我看看.我不会用那个类.最好是用RSA和DES算法的.
来源: 互联网 发布时间:2015-07-23
本文导语: 是不是一定要用到1.4的javax.crypto类..我见过一些例程是用到那个类的...不用怎么做呢? | 我发给你一个吧,你的e-mail是? | 确实要用到javax.crypto.*, 我这里有一些例程,但愿对你有帮助: i...
是不是一定要用到1.4的javax.crypto类..我见过一些例程是用到那个类的...不用怎么做呢?
|
我发给你一个吧,你的e-mail是?
|
确实要用到javax.crypto.*, 我这里有一些例程,但愿对你有帮助:
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
public class DESCipherGenerator {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
Cipher cipher = Cipher.getInstance("DES");
System.out.println("Cipher provider: " + cipher.getProvider());
System.out.println("Cipher algorithm: " + cipher.getAlgorithm());
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
}
//--------------------------------------
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
public class DESKeyGenerator {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
System.out.println("Key format: " + key.getFormat());
System.out.println("Key algorithm: " + key.getAlgorithm());
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
//-----------------------------------------
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import java.security.Key;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
public class DESCryptoTest {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
byte[] data = "Hello World!".getBytes();
System.out.println("Original data : " + new String(data));
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(data);
System.out.println("Encrypted data: " + new String(result));
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] original = cipher.doFinal(result);
System.out.println("Decrypted data: " + new String(original));
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
}
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
public class DESCipherGenerator {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
Cipher cipher = Cipher.getInstance("DES");
System.out.println("Cipher provider: " + cipher.getProvider());
System.out.println("Cipher algorithm: " + cipher.getAlgorithm());
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
}
//--------------------------------------
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
public class DESKeyGenerator {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
System.out.println("Key format: " + key.getFormat());
System.out.println("Key algorithm: " + key.getAlgorithm());
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
}
//-----------------------------------------
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import java.security.Key;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
public class DESCryptoTest {
public static void main(String[] args) {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
byte[] data = "Hello World!".getBytes();
System.out.println("Original data : " + new String(data));
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(data);
System.out.println("Encrypted data: " + new String(result));
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] original = cipher.doFinal(result);
System.out.println("Decrypted data: " + new String(original));
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
catch (NoSuchPaddingException e) {
e.printStackTrace();
}
catch (InvalidKeyException e) {
e.printStackTrace();
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IllegalBlockSizeException e) {
e.printStackTrace();
}
catch (BadPaddingException e) {
e.printStackTrace();
}
}
}
|
用java.security包是不能进行加密操作的,只能用 javax.crypto 的。
至于例子,你可以看一个jce的文档和例子,(jce在jdk1.3以前是单独的扩展包,在jdk1.4才被加入,就是那个crypto包了)。可以在java.sun.com找到相关资料,有文档、教程和例子。
至于例子,你可以看一个jce的文档和例子,(jce在jdk1.3以前是单独的扩展包,在jdk1.4才被加入,就是那个crypto包了)。可以在java.sun.com找到相关资料,有文档、教程和例子。