導航:首頁 > 文檔加密 > java四位數加密

java四位數加密

發布時間:2023-08-24 19:18:33

java編程如何給數字加密

最簡單的,用異或運算。
你也可以自己寫個加密方法啊。
比如說:利用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);
}
}
純手打。不採納對不起觀眾啊!!!

閱讀全文

與java四位數加密相關的資料

熱點內容
潭州學院python 瀏覽:814
下載文件夾不用卸載 瀏覽:233
怎樣刪除手機內不用的英文文件夾 瀏覽:81
android獲得屏幕寬度 瀏覽:302
單片機根據波形寫代碼 瀏覽:669
應屆生程序員怎麼投簡歷 瀏覽:721
數學建模演算法與應用ppt 瀏覽:99
遠程怎麼訪問端游伺服器 瀏覽:106
打電話定位置的源碼 瀏覽:642
即時通訊平台源碼 瀏覽:457
安卓自助app怎麼轉到蘋果手機 瀏覽:328
雅馬哈迴音壁不能識別源碼 瀏覽:730
python如何移植到安卓 瀏覽:29
黃柱選股公式源碼 瀏覽:639
教育系統源碼達標 瀏覽:888
音效卡驅動安裝程序在哪個文件夾 瀏覽:62
錢還完了銀行不給解壓 瀏覽:171
linux的系統調用表 瀏覽:754
php怎麼轉換頁面 瀏覽:548
我的世界買了伺服器之後怎麼開服 瀏覽:830