10 个解决方案
#1
我用的是GBK的编码
#2
不懂,帮顶。祝楼主好运
#3
这个问题纠结我一天了 高手你在哪啊
#4
这里有一个加密算法,也许能激发你的灵感
好像是火龙果大侠的杰作:
好像是火龙果大侠的杰作:
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import test.temp23;
//http://topic.csdn.net/u/20101112/11/906A2C5D-5F3B-4284-9DE8-DDB3A18B0BA0.html
public class Des {
public static void main(String[] args) throws Exception {
byte[] key=new BASE64Decoder().decodeBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4");
byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] data="中国ABCabc123".getBytes("UTF-8");
System.out.println("ECB加密解密");
byte[] str3 = des3EncodeECB(key,data );
byte[] str4 = ees3DecodeECB(key, str3);
System.out.println(new BASE64Encoder().encode(str3));
System.out.println(new String(str4, "UTF-8"));
System.out.println();
System.out.println("CBC加密解密");
byte[] str5 = des3EncodeCBC(key, keyiv, data);
byte[] str6 = des3DecodeCBC(key, keyiv, str5);
System.out.println(new BASE64Encoder().encode(str5));
System.out.println(new String(str6, "UTF-8"));
}
/**
* ECB加密,不要IV
* @param key 密钥
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* ECB解密,不要IV
* @param key 密钥
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] ees3DecodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC加密
* @param key 密钥
* @param keyiv IV
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC解密
* @param key 密钥
* @param keyiv IV
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
}
#5
好像这个才是火龙果大侠的
import java.security.Key;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class BcTest {
// 3DES key = 00 01 02 03 04 05 06 07 01 02 03 04 05 06 07 08 02 03 04 05 06 07 08 09
// data ("01234567") = 30 31 32 33 34 35 36 37
// 3des cipher = 5a b3 fd 7b 2a ca b2 95
// cipher mode = DESede/ECB/NoPadding
public static void main(String[] args) throws Exception {
byte[] key1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
byte[] key2 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] key3 = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] data = "你好01234567".getBytes(); // 由于未采用填充模式,因此原文长度必须为 8 的倍数
// 3DES ciphertext = EK3(DK2(EK1(plaintext)))
byte[] crypt = encrypt(decrypt(encrypt(data, key1), key2), key3);
// 3DES plaintext = DK1(EK2(DK3(ciphertext)))
byte[] plain = decrypt(encrypt(decrypt(crypt, key3), key2), key1);
System.out.println(" key: " + ByteUtil.bytes2HexSpace(key1) + " "
+ ByteUtil.bytes2HexSpace(key2) + " "
+ ByteUtil.bytes2HexSpace(key3));
System.out.println(" data: " + ByteUtil.bytes2HexSpace(data));
System.out.println("crypt: " + ByteUtil.bytes2HexSpace(crypt));
System.out.println("plain: " + ByteUtil.bytes2HexSpace(plain));
}
public static byte[] decrypt(byte[] crypt, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(crypt);
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static SecretKey toKey(byte[] key) throws Exception {
KeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(dks);
}
}
class ByteUtil {
private static final char HEX[] = "0123456789abcdef".toCharArray();
public static String bytes2HexSpace(byte bys[]) {
char chs[] = new char[(bys.length * 2 + bys.length) - 1];
int i = 0;
int offset = 0;
for (; i < bys.length; i++) {
if (i > 0)
chs[offset++] = ' ';
chs[offset++] = HEX[bys[i] >> 4 & 15];
chs[offset++] = HEX[bys[i] & 15];
}
return new String(chs);
}
}
#6
public class MD5
{
/*
* A Java implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999 - 2000.
* See http://pajhome.org.uk/site/legal.html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/
/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
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;
}
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
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;
}
/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}
/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt)
{
return (num < < cnt) | (num > > > (32 - cnt));
}
/*
* These functions implement the basic operation for each round of the
* algorithm.
*/
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);
}
/*
* Take a string and return the hex representation of its MD5.
*/
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);
}
}
{
/*
* A Java implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999 - 2000.
* See http://pajhome.org.uk/site/legal.html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/
/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
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;
}
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
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;
}
/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}
/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt)
{
return (num < < cnt) | (num > > > (32 - cnt));
}
/*
* These functions implement the basic operation for each round of the
* algorithm.
*/
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);
}
/*
* Take a string and return the hex representation of its MD5.
*/
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);
}
}
#7
加密算法之md5算法java源程序
#8
加密算法都是一次性加密很多字节的呀
你把中文拆开,顶多就8位(一个字节) 然后破解它比破解你的qq登录密码还容易
你只要将它转为字节数组就可以了,
把精力放在如何对 字节数组 加密 <---> 解密
最后还原成字节数组
再次还原成字符串
你把中文拆开,顶多就8位(一个字节) 然后破解它比破解你的qq登录密码还容易
你只要将它转为字节数组就可以了,
把精力放在如何对 字节数组 加密 <---> 解密
最后还原成字节数组
再次还原成字符串
#9
下面的程序是 网络上流传的面试题,他就是对字节数组加密和解密的(根据自己的方法)
我对他进行了简单解释
把精力放在如何对 字节数组 加密 <---> 解密
可以仿照下面的做法
我对他进行了简单解释
把精力放在如何对 字节数组 加密 <---> 解密
可以仿照下面的做法
public class TestEncryptAndDecrypt {
// 原理:a^(a^b) = b
public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xb1c0796;
System.out.println(seed);
for (int i = 0; i < len; ++i) {
// in[i]与seed异或后,向右移动3位,高三位右移变成低3位,有效位范围:第0位到第2位
//总的看来,只剩下原来字节的第5位到第7位,放到现在字节a的第0位到第2位
byte a = (byte) ((in[i] ^ seed) >>> 5); // in[i]与seed异或后,向右移动3位,高三位右移变成低3位,把原来的高5位置0
// in[i]与seed的第21位开始异或后,把异或结果右移17位,即把异或后in[i]的低五位变成了高五位
//--------------------------------------------------------------------------------------------------
//1.先将8个二进制位扩展为32个二进制位,高24位补0:有效位范围:第0位到第7位
//2.将32个二进制位向左移动二十位,低位补0,有效位范围:第20位到第27位
//3.异或,有效位范围:第20位到第27位
//4.向右移动17位高位补0,有效位范围:第3位到第10位
//5.先将32个二进制位向下强制转为8个二进制位,只剩下低8位,有效位范围:第3位到第7位
//总的看来,只剩下原来字节的第0位到第4位,放到现在字节b的第3位到第7位
byte b = (byte) (((((int) in[i]) << 20) ^ seed) >>> (20 - 3));
//---------------------------------------------------------------------------------------------------
//16进制的0x7相当于二进制的00000111,与a中的8个二进制为进行"按位与"操作,相当于将a中的第3位到第7位置0
//a中的第3位到第7位存放的是无效位,也就是将无效位置0, 有效位范围:第0位到第2位不受影响
a &= 0x7; //
//----------------------------------------------------------------------------------------------------
//16进制的0xf8相当于二进制的11111000,与b中的8个二进制为进行"按位与"操作,相当于将b中的第0位到第2位置0
//b中的第0位到第2位存放的是无效位,也就是将无效位置0 有效位范围:第3位到第7位不受影响
b &= 0xf8;
//---------------------------------------------------------------------------------------------------
//a与b进行"按位或"操作 ,
//a中的无效位(第3位到第7位)已经为0,所以结果的第3位到第7位是由b中的第3位到第7位决定
//b中的无效位(第0位到第2位)已经为0,所以结果的第0位到第2位是由a中的第0位到第2位决定
//总的看来;又是将a中5位有效位,和b中3位有效位组合在一起,组成8位有效位
out[i] = (byte) (a | b);
//--------------------------------------------------------------------------------------------------------
//---原来的in[i]与seed先异或再循环右移5位。变为out[i],----------
//每一次都要改变seed,改变后的seed只依赖于前一个in[i]和常量48475829(最前头seed是依赖于0xb1c07965和password)
seed = ((seed ^ in[i]) * 48475829 + in[i]);
}
}
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xb1c07965;//最前头seed是依赖于0xb1c07965和password
System.out.println("hhhh:" + seed);
for (int i = 0; i < len; ++i) {
// fill the code here
//--------------------------------------------------------------------------------------------
//
//可以利用字节变量a中第5位到第7位临时存放第0位到第2位,b中第0位到第4位临时存放第3位到第7位
//第3位到第7位置0,左移5位,a中第5位到第7位是有效位
byte a = (byte) ((in[i] & 0x07) << 5); // 把密文的第三位变成高三位
//第0位到第2位置0,右移3位,b中第0位到第4位是有效位
byte b = (byte) ((in[i] & 0xf8) >>> 3); // 把密文的高五位变成低五位
//---------------------------------------------------------------------------------------------
a = (byte) (a ^ seed); // 还原
a &= 0xe0; //无效位再次置0,因为有可能,经过异或之后变成1了
b = (byte) (((((int) b) << 20) ^ seed) >>> 20); // 还原
b &= 0x1f; ////无效位再次置0,因为有可能,经过异或之后变成1了
out[i] = (byte)(a | b); //将临时变量中的数据串接,保存到out[i],这是真实的数据来了,
//每一次循环seed是不一样的,利用真实数据out[i]和常量48475829求出seed
seed = ((seed ^ out[i]) * 48475829 + out[i]);
}
}
public static void main(String[] args) throws Exception {
String string = "123\r\n456";
//System.out.println(string);
//string = string.replaceAll("\\r\\n","");
//string = string.replaceAll("\\r","");
//string = string.replaceAll("\\n","");
string = string.substring(0,2);
char [] tempArray = string.toCharArray();
for(int i= 0; i<tempArray.length; i++){
System.out.print((int)tempArray[i] + " ");
}
int password = 0x38431c82;
byte[] buf1 = { 29, 11, 94, -88, -120, -59, -45, 124, 64, -109, -44,
87, 12, -92, -18, 76, -11, -88, 126, -44, 72, 85, -16, -84, 73,
-34, -15, 104, -114, 112, -4, -47, 25, 52, 6, -80, 124, 41, 34,
-55, -99, 31, 12, -1, -28, 49, 124, -7, -18, 105, -42, -105, 1,
50, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2));
}
}
/*output:
搜狗浏览器是目前速度最快、最稳定、最安全、功能最强大的“双核”浏览器!!!!!
*/
#10
中文在java里面是两个字节,并不是一个字节,楼上的搞错了
#1
我用的是GBK的编码
#2
不懂,帮顶。祝楼主好运
#3
这个问题纠结我一天了 高手你在哪啊
#4
这里有一个加密算法,也许能激发你的灵感
好像是火龙果大侠的杰作:
好像是火龙果大侠的杰作:
import java.security.Key;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import test.temp23;
//http://topic.csdn.net/u/20101112/11/906A2C5D-5F3B-4284-9DE8-DDB3A18B0BA0.html
public class Des {
public static void main(String[] args) throws Exception {
byte[] key=new BASE64Decoder().decodeBuffer("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4");
byte[] keyiv = { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] data="中国ABCabc123".getBytes("UTF-8");
System.out.println("ECB加密解密");
byte[] str3 = des3EncodeECB(key,data );
byte[] str4 = ees3DecodeECB(key, str3);
System.out.println(new BASE64Encoder().encode(str3));
System.out.println(new String(str4, "UTF-8"));
System.out.println();
System.out.println("CBC加密解密");
byte[] str5 = des3EncodeCBC(key, keyiv, data);
byte[] str6 = des3DecodeCBC(key, keyiv, str5);
System.out.println(new BASE64Encoder().encode(str5));
System.out.println(new String(str6, "UTF-8"));
}
/**
* ECB加密,不要IV
* @param key 密钥
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* ECB解密,不要IV
* @param key 密钥
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] ees3DecodeECB(byte[] key, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, deskey);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC加密
* @param key 密钥
* @param keyiv IV
* @param data 明文
* @return Base64编码的密文
* @throws Exception
*/
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
/**
* CBC解密
* @param key 密钥
* @param keyiv IV
* @param data Base64编码的密文
* @return 明文
* @throws Exception
*/
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data)
throws Exception {
Key deskey = null;
DESedeKeySpec spec = new DESedeKeySpec(key);
SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
deskey = keyfactory.generateSecret(spec);
Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
IvParameterSpec ips = new IvParameterSpec(keyiv);
cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
byte[] bOut = cipher.doFinal(data);
return bOut;
}
}
#5
好像这个才是火龙果大侠的
import java.security.Key;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class BcTest {
// 3DES key = 00 01 02 03 04 05 06 07 01 02 03 04 05 06 07 08 02 03 04 05 06 07 08 09
// data ("01234567") = 30 31 32 33 34 35 36 37
// 3des cipher = 5a b3 fd 7b 2a ca b2 95
// cipher mode = DESede/ECB/NoPadding
public static void main(String[] args) throws Exception {
byte[] key1 = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
byte[] key2 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] key3 = new byte[] { 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] data = "你好01234567".getBytes(); // 由于未采用填充模式,因此原文长度必须为 8 的倍数
// 3DES ciphertext = EK3(DK2(EK1(plaintext)))
byte[] crypt = encrypt(decrypt(encrypt(data, key1), key2), key3);
// 3DES plaintext = DK1(EK2(DK3(ciphertext)))
byte[] plain = decrypt(encrypt(decrypt(crypt, key3), key2), key1);
System.out.println(" key: " + ByteUtil.bytes2HexSpace(key1) + " "
+ ByteUtil.bytes2HexSpace(key2) + " "
+ ByteUtil.bytes2HexSpace(key3));
System.out.println(" data: " + ByteUtil.bytes2HexSpace(data));
System.out.println("crypt: " + ByteUtil.bytes2HexSpace(crypt));
System.out.println("plain: " + ByteUtil.bytes2HexSpace(plain));
}
public static byte[] decrypt(byte[] crypt, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, k);
return cipher.doFinal(crypt);
}
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
Key k = toKey(key);
Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, k);
return cipher.doFinal(data);
}
public static SecretKey toKey(byte[] key) throws Exception {
KeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
return keyFactory.generateSecret(dks);
}
}
class ByteUtil {
private static final char HEX[] = "0123456789abcdef".toCharArray();
public static String bytes2HexSpace(byte bys[]) {
char chs[] = new char[(bys.length * 2 + bys.length) - 1];
int i = 0;
int offset = 0;
for (; i < bys.length; i++) {
if (i > 0)
chs[offset++] = ' ';
chs[offset++] = HEX[bys[i] >> 4 & 15];
chs[offset++] = HEX[bys[i] & 15];
}
return new String(chs);
}
}
#6
public class MD5
{
/*
* A Java implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999 - 2000.
* See http://pajhome.org.uk/site/legal.html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/
/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
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;
}
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
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;
}
/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}
/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt)
{
return (num < < cnt) | (num > > > (32 - cnt));
}
/*
* These functions implement the basic operation for each round of the
* algorithm.
*/
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);
}
/*
* Take a string and return the hex representation of its MD5.
*/
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);
}
}
{
/*
* A Java implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Based on the JavaScript implementation of Paul Johnston
* Copyright (C) Paul Johnston 1999 - 2000.
* See http://pajhome.org.uk/site/legal.html for details.
* Java Version by Thomas Weber (Orange Interactive GmbH)
*/
/*
* Convert a 32-bit number to a hex string with ls-byte first
*/
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;
}
/*
* Convert a string to a sequence of 16-word blocks, stored as an array.
* Append padding bits and the length, as described in the MD5 standard.
*/
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;
}
/*
* Add integers, wrapping at 2^32
*/
private int add(int x, int y)
{
return ((x&0x7FFFFFFF) + (y&0x7FFFFFFF)) ^ (x&0x80000000) ^ (y&0x80000000);
}
/*
* Bitwise rotate a 32-bit number to the left
*/
private int rol(int num, int cnt)
{
return (num < < cnt) | (num > > > (32 - cnt));
}
/*
* These functions implement the basic operation for each round of the
* algorithm.
*/
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);
}
/*
* Take a string and return the hex representation of its MD5.
*/
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);
}
}
#7
加密算法之md5算法java源程序
#8
加密算法都是一次性加密很多字节的呀
你把中文拆开,顶多就8位(一个字节) 然后破解它比破解你的qq登录密码还容易
你只要将它转为字节数组就可以了,
把精力放在如何对 字节数组 加密 <---> 解密
最后还原成字节数组
再次还原成字符串
你把中文拆开,顶多就8位(一个字节) 然后破解它比破解你的qq登录密码还容易
你只要将它转为字节数组就可以了,
把精力放在如何对 字节数组 加密 <---> 解密
最后还原成字节数组
再次还原成字符串
#9
下面的程序是 网络上流传的面试题,他就是对字节数组加密和解密的(根据自己的方法)
我对他进行了简单解释
把精力放在如何对 字节数组 加密 <---> 解密
可以仿照下面的做法
我对他进行了简单解释
把精力放在如何对 字节数组 加密 <---> 解密
可以仿照下面的做法
public class TestEncryptAndDecrypt {
// 原理:a^(a^b) = b
public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xb1c0796;
System.out.println(seed);
for (int i = 0; i < len; ++i) {
// in[i]与seed异或后,向右移动3位,高三位右移变成低3位,有效位范围:第0位到第2位
//总的看来,只剩下原来字节的第5位到第7位,放到现在字节a的第0位到第2位
byte a = (byte) ((in[i] ^ seed) >>> 5); // in[i]与seed异或后,向右移动3位,高三位右移变成低3位,把原来的高5位置0
// in[i]与seed的第21位开始异或后,把异或结果右移17位,即把异或后in[i]的低五位变成了高五位
//--------------------------------------------------------------------------------------------------
//1.先将8个二进制位扩展为32个二进制位,高24位补0:有效位范围:第0位到第7位
//2.将32个二进制位向左移动二十位,低位补0,有效位范围:第20位到第27位
//3.异或,有效位范围:第20位到第27位
//4.向右移动17位高位补0,有效位范围:第3位到第10位
//5.先将32个二进制位向下强制转为8个二进制位,只剩下低8位,有效位范围:第3位到第7位
//总的看来,只剩下原来字节的第0位到第4位,放到现在字节b的第3位到第7位
byte b = (byte) (((((int) in[i]) << 20) ^ seed) >>> (20 - 3));
//---------------------------------------------------------------------------------------------------
//16进制的0x7相当于二进制的00000111,与a中的8个二进制为进行"按位与"操作,相当于将a中的第3位到第7位置0
//a中的第3位到第7位存放的是无效位,也就是将无效位置0, 有效位范围:第0位到第2位不受影响
a &= 0x7; //
//----------------------------------------------------------------------------------------------------
//16进制的0xf8相当于二进制的11111000,与b中的8个二进制为进行"按位与"操作,相当于将b中的第0位到第2位置0
//b中的第0位到第2位存放的是无效位,也就是将无效位置0 有效位范围:第3位到第7位不受影响
b &= 0xf8;
//---------------------------------------------------------------------------------------------------
//a与b进行"按位或"操作 ,
//a中的无效位(第3位到第7位)已经为0,所以结果的第3位到第7位是由b中的第3位到第7位决定
//b中的无效位(第0位到第2位)已经为0,所以结果的第0位到第2位是由a中的第0位到第2位决定
//总的看来;又是将a中5位有效位,和b中3位有效位组合在一起,组成8位有效位
out[i] = (byte) (a | b);
//--------------------------------------------------------------------------------------------------------
//---原来的in[i]与seed先异或再循环右移5位。变为out[i],----------
//每一次都要改变seed,改变后的seed只依赖于前一个in[i]和常量48475829(最前头seed是依赖于0xb1c07965和password)
seed = ((seed ^ in[i]) * 48475829 + in[i]);
}
}
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0xb1c07965;//最前头seed是依赖于0xb1c07965和password
System.out.println("hhhh:" + seed);
for (int i = 0; i < len; ++i) {
// fill the code here
//--------------------------------------------------------------------------------------------
//
//可以利用字节变量a中第5位到第7位临时存放第0位到第2位,b中第0位到第4位临时存放第3位到第7位
//第3位到第7位置0,左移5位,a中第5位到第7位是有效位
byte a = (byte) ((in[i] & 0x07) << 5); // 把密文的第三位变成高三位
//第0位到第2位置0,右移3位,b中第0位到第4位是有效位
byte b = (byte) ((in[i] & 0xf8) >>> 3); // 把密文的高五位变成低五位
//---------------------------------------------------------------------------------------------
a = (byte) (a ^ seed); // 还原
a &= 0xe0; //无效位再次置0,因为有可能,经过异或之后变成1了
b = (byte) (((((int) b) << 20) ^ seed) >>> 20); // 还原
b &= 0x1f; ////无效位再次置0,因为有可能,经过异或之后变成1了
out[i] = (byte)(a | b); //将临时变量中的数据串接,保存到out[i],这是真实的数据来了,
//每一次循环seed是不一样的,利用真实数据out[i]和常量48475829求出seed
seed = ((seed ^ out[i]) * 48475829 + out[i]);
}
}
public static void main(String[] args) throws Exception {
String string = "123\r\n456";
//System.out.println(string);
//string = string.replaceAll("\\r\\n","");
//string = string.replaceAll("\\r","");
//string = string.replaceAll("\\n","");
string = string.substring(0,2);
char [] tempArray = string.toCharArray();
for(int i= 0; i<tempArray.length; i++){
System.out.print((int)tempArray[i] + " ");
}
int password = 0x38431c82;
byte[] buf1 = { 29, 11, 94, -88, -120, -59, -45, 124, 64, -109, -44,
87, 12, -92, -18, 76, -11, -88, 126, -44, 72, 85, -16, -84, 73,
-34, -15, 104, -114, 112, -4, -47, 25, 52, 6, -80, 124, 41, 34,
-55, -99, 31, 12, -1, -28, 49, 124, -7, -18, 105, -42, -105, 1,
50, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2));
}
}
/*output:
搜狗浏览器是目前速度最快、最稳定、最安全、功能最强大的“双核”浏览器!!!!!
*/
#10
中文在java里面是两个字节,并不是一个字节,楼上的搞错了