最简单的,用异或运算。
你也可以自己写个加密方法啊。
比如说:利用unicode字符加密啊。假设一个数字a它的unicode值是1234,你自己设计个函数,比如说y=2x^3+3,得到一个新的unicode字符,然后把这个unicode字符转换为字母,这个字母可能是汉字,但更可能是外国符文,反正一般人不会认出来的。你解密的时候,倒推一下就行了。
⑵ java 可实现的、流行的加密方法有哪些
md5加密,
package util.md5;
public class Md5{
String hex_chr = "0123456789abcdef";
private String rhex(int num)
{
String str = "";
for(int j = 0; j <= 3; j++)
str = str + hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
return str;
}
private int[] str2blks_MD5(String str)
{
int nblk = ((str.length() + 8) >> 6) + 1;
int[] blks = new int[nblk * 16];
int i = 0;
for(i = 0; i < nblk * 16; i++) {
blks[i] = 0;
}
for(i = 0; i < str.length(); i++) {
blks[i >> 2] |= str.charAt(i) << ((i % 4) * 8);
}
blks[i >> 2] |= 0x80 << ((i % 4) * 8);
blks[nblk * 16 - 2] = str.length()*8;
return blks;
}
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}
private int rol(int num, int cnt)
{
return (num << cnt) | (num >>> (32 - cnt));
}
private int cmn(int q, int a, int b, int x, int s, int t)
{
return add(rol(add(add(a, q), add(x, t)), s), b);
}
private int ff(int a, int b, int c, int d, int x, int s, int t)
{
return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
private int gg(int a, int b, int c, int d, int x, int s, int t)
{
return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
private int hh(int a, int b, int c, int d, int x, int s, int t)
{
return cmn(b ^ c ^ d, a, b, x, s, t);
}
private int ii(int a, int b, int c, int d, int x, int s, int t)
{
return cmn(c ^ (b | (~d)), a, b, x, s, t);
}
public String calcMD5(String str)
{
int[] x = str2blks_MD5(str);
int a = 0x67452301;
int b = 0xEFCDAB89;
int c = 0x98BADCFE;
int d = 0x10325476;
for(int i = 0; i < x.length; i += 16)
{
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
a = ff(a, b, c, d, x[i+ 0], 7 , 0xD76AA478);
d = ff(d, a, b, c, x[i+ 1], 12, 0xE8C7B756);
c = ff(c, d, a, b, x[i+ 2], 17, 0x242070DB);
b = ff(b, c, d, a, x[i+ 3], 22, 0xC1BDCEEE);
a = ff(a, b, c, d, x[i+ 4], 7 , 0xF57C0FAF);
d = ff(d, a, b, c, x[i+ 5], 12, 0x4787C62A);
c = ff(c, d, a, b, x[i+ 6], 17, 0xA8304613);
b = ff(b, c, d, a, x[i+ 7], 22, 0xFD469501);
a = ff(a, b, c, d, x[i+ 8], 7 , 0x698098D8);
d = ff(d, a, b, c, x[i+ 9], 12, 0x8B44F7AF);
c = ff(c, d, a, b, x[i+10], 17, 0xFFFF5BB1);
b = ff(b, c, d, a, x[i+11], 22, 0x895CD7BE);
a = ff(a, b, c, d, x[i+12], 7 , 0x6B901122);
d = ff(d, a, b, c, x[i+13], 12, 0xFD987193);
c = ff(c, d, a, b, x[i+14], 17, 0xA679438E);
b = ff(b, c, d, a, x[i+15], 22, 0x49B40821);
a = gg(a, b, c, d, x[i+ 1], 5 , 0xF61E2562);
d = gg(d, a, b, c, x[i+ 6], 9 , 0xC040B340);
c = gg(c, d, a, b, x[i+11], 14, 0x265E5A51);
b = gg(b, c, d, a, x[i+ 0], 20, 0xE9B6C7AA);
a = gg(a, b, c, d, x[i+ 5], 5 , 0xD62F105D);
d = gg(d, a, b, c, x[i+10], 9 , 0x02441453);
c = gg(c, d, a, b, x[i+15], 14, 0xD8A1E681);
b = gg(b, c, d, a, x[i+ 4], 20, 0xE7D3FBC8);
a = gg(a, b, c, d, x[i+ 9], 5 , 0x21E1CDE6);
d = gg(d, a, b, c, x[i+14], 9 , 0xC33707D6);
c = gg(c, d, a, b, x[i+ 3], 14, 0xF4D50D87);
b = gg(b, c, d, a, x[i+ 8], 20, 0x455A14ED);
a = gg(a, b, c, d, x[i+13], 5 , 0xA9E3E905);
d = gg(d, a, b, c, x[i+ 2], 9 , 0xFCEFA3F8);
c = gg(c, d, a, b, x[i+ 7], 14, 0x676F02D9);
b = gg(b, c, d, a, x[i+12], 20, 0x8D2A4C8A);
a = hh(a, b, c, d, x[i+ 5], 4 , 0xFFFA3942);
d = hh(d, a, b, c, x[i+ 8], 11, 0x8771F681);
c = hh(c, d, a, b, x[i+11], 16, 0x6D9D6122);
b = hh(b, c, d, a, x[i+14], 23, 0xFDE5380C);
a = hh(a, b, c, d, x[i+ 1], 4 , 0xA4BEEA44);
d = hh(d, a, b, c, x[i+ 4], 11, 0x4BDECFA9);
c = hh(c, d, a, b, x[i+ 7], 16, 0xF6BB4B60);
b = hh(b, c, d, a, x[i+10], 23, 0xBEBFBC70);
a = hh(a, b, c, d, x[i+13], 4 , 0x289B7EC6);
d = hh(d, a, b, c, x[i+ 0], 11, 0xEAA127FA);
c = hh(c, d, a, b, x[i+ 3], 16, 0xD4EF3085);
b = hh(b, c, d, a, x[i+ 6], 23, 0x04881D05);
a = hh(a, b, c, d, x[i+ 9], 4 , 0xD9D4D039);
d = hh(d, a, b, c, x[i+12], 11, 0xE6DB99E5);
c = hh(c, d, a, b, x[i+15], 16, 0x1FA27CF8);
b = hh(b, c, d, a, x[i+ 2], 23, 0xC4AC5665);
a = ii(a, b, c, d, x[i+ 0], 6 , 0xF4292244);
d = ii(d, a, b, c, x[i+ 7], 10, 0x432AFF97);
c = ii(c, d, a, b, x[i+14], 15, 0xAB9423A7);
b = ii(b, c, d, a, x[i+ 5], 21, 0xFC93A039);
a = ii(a, b, c, d, x[i+12], 6 , 0x655B59C3);
d = ii(d, a, b, c, x[i+ 3], 10, 0x8F0CCC92);
c = ii(c, d, a, b, x[i+10], 15, 0xFFEFF47D);
b = ii(b, c, d, a, x[i+ 1], 21, 0x85845DD1);
a = ii(a, b, c, d, x[i+ 8], 6 , 0x6FA87E4F);
d = ii(d, a, b, c, x[i+15], 10, 0xFE2CE6E0);
c = ii(c, d, a, b, x[i+ 6], 15, 0xA3014314);
b = ii(b, c, d, a, x[i+13], 21, 0x4E0811A1);
a = ii(a, b, c, d, x[i+ 4], 6 , 0xF7537E82);
d = ii(d, a, b, c, x[i+11], 10, 0xBD3AF235);
c = ii(c, d, a, b, x[i+ 2], 15, 0x2AD7D2BB);
b = ii(b, c, d, a, x[i+ 9], 21, 0xEB86D391);
a = add(a, olda);
b = add(b, oldb);
c = add(c, oldc);
d = add(d, oldd);
}
return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}
public static void main(String[] args)
{
Md5 md = new Md5();
String input;
if (args.length==0) input = "璁捐绷鏁?;
else input = args[0];
System.out.println(input);
String str = md.calcMD5(input);
System.out.println(str);
}
}
⑶ 用java编写一个数据加密的程序
import java.util.HashMap;
import java.util.Map;
public class NewJFrame extends javax.swing.JFrame {
public static Map<Character,Integer> charmap = new HashMap<Character,Integer>();
public static char stringmap[] = new char[]{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q' ,'r','s','t','u','v','w','x','y','z'
};
static{
charmap.put('a', 1);
charmap.put('b', 2);
charmap.put('c', 3);
charmap.put('d', 4);
charmap.put('e', 5);
charmap.put('f', 6);
charmap.put('g', 7);
charmap.put('h', 8);
charmap.put('i', 9);
charmap.put('j', 10);
charmap.put('k', 11);
charmap.put('l', 12);
charmap.put('m', 13);
charmap.put('n', 14);
charmap.put('o', 15);
charmap.put('p', 16);
charmap.put('q', 17);
charmap.put('r', 18);
charmap.put('s', 19);
charmap.put('t', 20);
charmap.put('u', 21);
charmap.put('v', 22);
charmap.put('w', 23);
charmap.put('x', 24);
charmap.put('y', 25);
charmap.put('z', 26);
charmap.put('A', 1);
charmap.put('B', 2);
charmap.put('C', 3);
charmap.put('D', 4);
charmap.put('E', 5);
charmap.put('F', 6);
charmap.put('G', 7);
charmap.put('H', 8);
charmap.put('I', 9);
charmap.put('J', 10);
charmap.put('K', 11);
charmap.put('L', 12);
charmap.put('M', 13);
charmap.put('N', 14);
charmap.put('O', 15);
charmap.put('P', 16);
charmap.put('Q', 17);
charmap.put('R', 18);
charmap.put('S', 19);
charmap.put('T', 20);
charmap.put('U', 21);
charmap.put('V', 22);
charmap.put('W', 23);
charmap.put('X', 24);
charmap.put('Y', 25);
charmap.put('Z', 26);
}
public NewJFrame() {
initComponents();
}
private char change(char c) {
int i = charmap.get(c);
int k = 2;
int j;
j=(i+k) % 26 ;
return stringmap[--j];
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
ba = new javax.swing.JButton();
ta = new javax.swing.JTextField();
tb = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
ba.setText("转换");
ba.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
baActionPerformed(evt);
}
});
tb.setEnabled(false);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(66, 66, 66)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tb)
.addComponent(ta, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(147, 147, 147)
.addComponent(ba, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(ta, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(tb, javax.swing.GroupLayout.DEFAULT_SIZE, 38, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(ba)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void baActionPerformed(java.awt.event.ActionEvent evt) {
String aa = ta.getText();
char bb[] = aa.toCharArray();
char cc[] = new char[bb.length];
for(int i = 0; i < bb.length;i++){
cc[i] = change(bb[i]);
}
tb.setText(String.valueOf(cc));
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ba;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField ta;
private javax.swing.JTextField tb;
// End of variables declaration
}
private void initComponents() 这个方法不用太在意,只是个生成界面的方法而已......
看不懂这个方法也没关系
⑷ java密码加密与解密
以下两个类可以很方便的完成字符串的加密和解密
加密 CryptHelper encrypt(password)
解密 CrypHelper decrypt(password)
代码如下
CryptUtils java
[java]
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
* 扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥 +byte hex (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 ( 加密前的二进串 +byte hex (input ))
// System out println ( 加密前的字符串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @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 ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串 +byte hex (clearByte ))
// System out println ( 解密后的字符串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;i<l;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
package gdie lab crypt;
import java io IOException;
import javax crypto Cipher;
import javax crypto KeyGenerator;
import javax crypto SecretKey;
import apache xerces internal impl dv util Base ;
public class CryptUtils {
private static String Algorithm = DES ;
private static byte[] DEFAULT_KEY=new byte[] { };
private static String VALUE_ENCODING= UTF ;
/**
* 生成密钥
*
* @return byte[] 返回生成的密钥
* @throws exception
* 扔出异常
*/
public static byte[] getSecretKey() throws Exception {
KeyGenerator keygen = KeyGenerator getInstance(Algorithm)
SecretKey deskey = keygen generateKey()
// if (debug ) System out println ( 生成密钥 +byte hex (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 ( 加密前的二进串 +byte hex (input ))
// System out println ( 加密前的字符串 +new String (input ))
//
// }
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher ENCRYPT_MODE deskey)
byte[] cipherByte = c doFinal(input)
// if (debug ) System out println ( 加密后的二进串 +byte hex (cipherByte ))
return cipherByte;
}
public static byte[] encryptData(byte[] input) throws Exception {
return encryptData(input DEFAULT_KEY)
}
/**
* 将给定的已加密的数据通过指定的密钥进行解密
*
* @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 ( 解密前的信息 +byte hex (input ))
Cipher c = Cipher getInstance(Algorithm)
c init(Cipher DECRYPT_MODE deskey)
byte[] clearByte = c doFinal(input)
// if (debug )
// {
// System out println ( 解密后的二进串 +byte hex (clearByte ))
// System out println ( 解密后的字符串 +(new String (clearByte )))
//
// }
return clearByte;
}
public static byte[] decryptData(byte[] input) throws Exception {
return decryptData(input DEFAULT_KEY)
}
/**
* 字节码转换成 进制字符串
*
* @param byte[] b 输入要转换的字节码
* @return String 返回转换后的 进制字符串
*/
public static String byte hex(byte[] bytes) {
StringBuilder hs = new StringBuilder()
for(byte b : bytes)
hs append(String format( % $ X b))
return hs toString()
}
public static byte[] hex byte(String content) {
int l=content length()》 ;
byte[] result=new byte[l];
for(int i= ;i<l;i++) {
int j=i《 ;
String s=content substring(j j+ )
result[i]=Integer valueOf(s ) byteValue()
}
return result;
}
/**
* 将字节数组转换为base 编码字符串
* @param buffer
* @return
*/
public static String bytesToBase (byte[] buffer) {
//BASE Encoder en=new BASE Encoder()
return Base encode(buffer)
// return encoder encode(buffer)
}
/**
* 将base 编码的字符串解码为字节数组
* @param value
* @return
* @throws IOException
*/
public static byte[] base ToBytes(String value) throws IOException {
//return Base decodeToByteArray(value)
// System out println(decoder decodeBuffer(value))
// return decoder decodeBuffer(value)
return Base decode(value)
}
/**
* 加密给定的字符串
* @param value
* @return 加密后的base 字符串
*/
public static String encryptString(String value) {
return encryptString(value DEFAULT_KEY)
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 以BASE 形式存在的密钥
* @return 加密后的base 字符串
* @throws IOException
*/
public static String encryptString(String value String key) throws IOException {
return encryptString(value base ToBytes(key))
}
/**
* 根据给定的密钥加密字符串
* @param value 待加密的字符串
* @param key 字节数组形式的密钥
* @return 加密后的base 字符串
*/
public static String encryptString(String value byte[] key) {
try {
byte[] data=value getBytes(VALUE_ENCODING)
data=CryptUtils encryptData(data key)
return bytesToBase (data)
} catch (Exception e) {
// TODO Auto generated catch block
e printStackTrace()
return null;
}
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @return 明文
*/
public static String decryptString(String value) {
return decryptString(value DEFAULT_KEY)
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key base 形式存在的密钥
* @return 明文
* @throws IOException
*/
public static String decryptString(String value String key) throws IOException {
String s=decryptString(value base ToBytes(key))
return s;
}
/**
* 解密字符串
* @param value base 形式存在的密文
* @param key 字节数据形式存在的密钥
* @return 明文
*/
public static String decryptString(String value byte[] key) {
try {
byte[] data=base ToBytes(value)
data=CryptUtils decryptData(data key)
return new String(data VALUE_ENCODING)
}catch(Exception e) {
e printStackTrace()
return null;
}
}
}
CryptHelper java
[java]
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
}
package gdie lab crypt;
import javax crypto Cipher;
import javax crypto SecretKey;
import javax crypto SecretKeyFactory;
import javax crypto spec DESKeySpec;
import javax crypto spec IvParameterSpec;
import springframework util DigestUtils;
public class CryptHelper{
private static String CRYPT_KEY = zhongqian ;
//加密
private static Cipher ecip;
//解密
private static Cipher dcip;
static {
try {
String KEY = DigestUtils md DigestAsHex(CRYPT_KEY getBytes()) toUpperCase()
KEY = KEY substring( )
byte[] bytes = KEY getBytes()
DESKeySpec ks = new DESKeySpec(bytes)
SecretKeyFactory skf = SecretKeyFactory getInstance( DES )
SecretKey sk = skf generateSecret(ks)
IvParameterSpec iv = new IvParameterSpec(bytes)
ecip = Cipher getInstance( DES/CBC/PKCS Padding )
ecip init(Cipher ENCRYPT_MODE sk iv )
dcip = Cipher getInstance( DES/CBC/PKCS Padding )
dcip init(Cipher DECRYPT_MODE sk iv )
}catch(Exception ex) {
ex printStackTrace()
}
}
public static String encrypt(String content) throws Exception {
byte[] bytes = ecip doFinal(content getBytes( ascii ))
return CryptUtils byte hex(bytes)
}
public static String decrypt(String content) throws Exception {
byte[] bytes = CryptUtils hex byte(content)
bytes = dcip doFinal(bytes)
return new String(bytes ascii )
}
//test
public static void main(String[] args) throws Exception {
String password = gly ;
String en = encrypt(password)
System out println(en)
System out println(decrypt(en))
}
lishixin/Article/program/Java/hx/201311/26449
⑸ java加密的几种方式
朋友你好,很高兴为你作答。
首先,Java加密能够应对的风险包括以下几个:
1、核心技术窃取
2、核心业务破解
3、通信模块破解
4、API接口暴露
本人正在使用几维安全Java加密方式,很不错,向你推荐,希望能够帮助到你。
几维安全Java2C针对DEX文件进行加密保护,将DEX文件中标记的Java代码翻译为C代码,编译成加固后的SO文件。默认情况只加密activity中的onCreate函数,如果开发者想加密其它类和方法,只需对相关类或函数添加标记代码,在APK加密时会自动对标记的代码进行加密处理。
与传统的APP加固方案相比,不涉及到自定义修改DEX文件的加载方式,所以其兼容性非常好;其次Java函数被完全转化为C函数,直接在Native层执行,不存在Java层解密执行的步骤,其性能和执行效率更优。
如果操作上有不明白的地方,可以联系技术支持人员帮你完成Java加密。
希望以上解答能够帮助到你。
⑹ java编写数字加密解密
//package wangcai.test;
public interface Endecryption {
public static final byte[] EN={48,49,50,51,52,53,54,55,56,57};
public static final byte[] DE={55,53,57,49,51,54,56,48,50,52};
}
//package wangcai.test;
import java.util.Scanner;
public class Cryption implements Endecryption{
/*
* 原始数字与加密后得到的密文数字之间的对应关系如下:
原始数字:0 1 2 3 4 5 6 7 8 9
密文数字:7 5 9 1 3 6 8 0 2 4
试编写程序把原始数字转换成加密密文或把加密密文转换成原始数字。
输入:
1 6 (第一个数表示加密或解密:1加密,2解密;第二个数表示数字的个数)
1 9 9 7 7 1 (待处理的数字内容)
输出:
5 4 4 0 0 5
*/
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
Cryption c=new Cryption();
System.out.println("请输入是加密还是解密:1加密,2解密");
int ende=sc.nextInt();
if(ende==1)
{
System.out.println("请输入加密的数字个数");
int num=sc.nextInt();
System.out.println("请输入"+num+"个数字");
String temp=new Scanner(System.in).nextLine();
System.out.println(c.Encryption(temp));
}
else if(ende==2)
{
System.out.println("请输入解密的数字个数");
int num=sc.nextInt();
System.out.println("请输入"+num+"个数字");
String temp=new Scanner(System.in).nextLine();
System.out.println(c.Decryption(temp));
}
else
{
System.out.println("输入错误");
}
}
/**
* 加密
* @param temp
* @return
*/
public String Encryption(String temp)
{
String result="";
byte[] temp_byte=temp.getBytes();
for(byte b:temp_byte)
{
int i=0;
for(;i<EN.length;i++)
{
if(b==EN[i])
{
result+=(char)DE[i];
break;
}
}
if(i==EN.length)
{
result+=(char)b;
}
}
return result;
}
/**
* 解密
* @param temp
* @return
*/
public String Decryption(String temp)
{
String result="";
byte[] temp_byte=temp.getBytes();
for(byte b:temp_byte)
{
int i=0;
for(;i<DE.length;i++)
{
if(b==DE[i])
{
result+=(char)EN[i];
break;
}
}
if(i==DE.length)
{
result+=(char)b;
}
}
return result;
}
}
加密解密方面的一般采用byte来实现
⑺ 如何用Java代码段生成四位数字加字母的验证码
不知道你问的是不是生成这种图片验证码?如果只要一个随机四位数 那这行代码就够了(new Random().nextInt(9000) + 1000;),如果是生成页面图片验证码就是下面的了: //设定 响应模式 resp.setContentType("image/jpeg"); // 生成令牌环数据; Integer token = new Random().nextInt(9000) + 1000; // 保存令牌环数据到session中 req.getSession().setAttribute(IMAGE_TOKEN_NAME, token); // 生成令牌环图片 ServletOutputStream out = resp.getOutputStream(); BufferedImage img = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB); Graphics g = img.getGraphics(); g.setColor(Color.YELLOW); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.setColor(Color.BLUE); g.setFont(new Font("", Font.BOLD, 18)); g.drawString(String.valueOf(token), 10, 16); ImageIO.write(img, "jpg", out); out.close();
下面简单的介绍他们的功能和用途,执行效率等。每个都有各自的优缺点看你是做甚什么方面的研究开发用。.net,是网站编程,现在很多都用这个,但是这个语言编程都有统一思路,很好掌握。窒息那个效率不是很高;php 支持跨平台,很容易学会,执行的效率很高;asp是ASP.net的前身,它比较稳定,比.net要弱一点。但是比.net好学。jsp 是网页编程,这个学习大约一周就能搞定,不过这个得多实践,不然的话,时间长了,就容易忘记。
我自己做的系统里面用作验证码的JSP的<%@page contentType="image/jpeg;charset=utf-8"%><%@page import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" %><%@ page import="java.io.OutputStream" %><html> <body> <%! Color getRandColor(int fc,int bc) { Random rd=new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int red=fc+rd.nextInt(bc-fc); int green=fc+rd.nextInt(bc-fc); int blue=fc+rd.nextInt(bc-fc); return new Color(red,green,blue); } %> <% Random r=new Random(); response.addHeader("Pragma","No-cache"); response.addHeader("Cache-Control","no-cache"); response.addDateHeader("expires",0); int width=90; int height=23; BufferedImage pic=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics gc=pic.getGraphics(); gc.setColor(getRandColor(200,250)); gc.fillRect(0,0,width,height); String[] rNum ={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f", "g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w", "x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N", "O","P","Q","R","S","T","U","V","W","X","Y","Z"}; int[] style = {Font.PLAIN,Font.BOLD,Font.ITALIC,Font.PLAIN+Font.BOLD, Font.BOLD+Font.ITALIC,Font.PLAIN+Font.ITALIC,Font.PLAIN+Font.BOLD+Font.ITALIC}; gc.setColor(Color.WHITE); gc.drawLine(0,30,90,10); gc.setColor(getRandColor(160,200)); for (int i=0;i<50;i++) { int x = r.nextInt(width); int y = r.nextInt(height); int xl = r.nextInt(10); int yl = r.nextInt(10); gc.drawLine(x,y,x+xl,y+yl); } gc.setColor(getRandColor(60,150)); String rt = ""; for(int i=0;i<4;i++){ String temp = rNum[r.nextInt(62)]; rt = rt+temp; gc.setFont(new Font("Times New Roman",style[r.nextInt(7)],15)); gc.drawString(temp,5+i*15+r.nextInt(10),10+r.nextInt(10)); } gc.dispose(); session.setAttribute("randNum",rt); OutputStream os=response.getOutputStream(); ImageIO.write(pic,"JPEG",os); System.out.println("当前验证码为:"+session.getAttribute("randNum")); os.flush(); os.close(); os=null; response.flushBuffer(); out.clear(); out = pageContext.pushBody(); %> </body></html>
⑻ 纯数字的加密成4位英文字母的方式(一般用于网站)
做回好人,回答你吧。直接看代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class jiami {
public static void main(String[] args) {
String source=null,target=null;
try {
FileInputStream fileread = new FileInputStream(new File("D:/a.txt"));//路径自己改
int length = fileread.available();
byte[] buffer = new byte[length];
fileread.read(buffer);
source = new String(buffer);//读取
fileread.close();
} catch (Exception e) {
e.printStackTrace();
}
if(source==null)
System.out.println("a.txt为空");
else{
System.out.println(source);
target=zhuanhuan(source);
System.out.println(target);
try {
FileOutputStream out = new FileOutputStream(new File("D:/b.txt"));
out.write(target.getBytes());//写入
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String zhuanhuan(String s){
char []array = s.toCharArray();
for(int i=0;i<array.length;i++){
//字母转换这里用ASCII码来,方便快捷,大写字母是65-90,小写字母是97-122
int j = (int)array[i];
if(j>=65&&j<=90){
if(j==90)
j=65;
else j=j+1;
array[i]=(char)j;
}
if(j>=97&&j<=122){
if(j==122)
j=97;
else j=j+1;
array[i]=(char)j;
}
//数字转换的话由于数字比较少,就直接转换了,不用ASCII码了
if(array[i]=='1')
array[i]='0';
else if(array[i]=='2')
array[i]='9';
else if(array[i]=='3')
array[i]='8';
else if(array[i]=='4')
array[i]='7';
else if(array[i]=='5')
array[i]='6';
else if(array[i]=='6')
array[i]='5';
else if(array[i]=='7')
array[i]='4';
else if(array[i]=='8')
array[i]='3';
else if(array[i]=='9')
array[i]='2';
else if(array[i]=='0')
array[i]='1';
}
return new String(array);
}
}
纯手打。不采纳对不起观众啊!!!