如何使用特殊算法對字符串進行加密編碼成為另一個字符串? MD5是不可逆算法,用DES加密然后用BASE64对加密后的二进制数据进行编码http://www.javayou.com 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //java.security.MessageDigest alg=java.security.MessageDigest.getInstance("MD5"); java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1"); alga.update(myinfo.getBytes()); byte[] digesta=alga.digest(); //通过某中方式传给其他人你的信息(myinfo)和摘要(digesta) 对方可以判断是否更改或传输正常 java.security.MessageDigest algb=java.security.MessageDigest.getInstance("SHA-1"); algb.update(myinfo.getBytes()); if (algb.isEqual(digesta,algb.digest())) { System.out.println("信息检查正常"); } else { System.out.println("摘要不相同"); } 可以考虑http://community.csdn.net/Expert/topic/3270/3270409.xml?temp=.5324671所回复的恺撒算法思想 to:ntzls(三星堆) 謝謝你的代碼,但是為什麼每次 "Hello World"加密後的字符串會不一樣呢,另外卻不能將加密後的字符串重新轉成為 "Hello World"?---------------------------------------------------------------------------package natoni;import java.util.Random;public class SimpleEncoder { private static Random random = new Random(); public static void main(String[] args){ String s = "hello world"; System.out.println(s+'\n'); byte[] key = getRandomKey(5); byte[] s1 = encrypt(key,s.getBytes()); System.out.println(new String(s1)+'\n'); byte[] s2 = decrypt(key,s1); System.out.println(new String(s2)+'\n'); } public static byte[] getRandomKey(int length) { if(length <= 0){ throw new IllegalArgumentException(); } int rand, index=0; byte[] result = new byte[length]; for(int i=0; i<(length/5); i++){ rand = random.nextInt(); for(int j=0; j<5; j++) { result[index++] = (byte)(rand&63); rand >>= 6; } } rand = random.nextInt(); for(int i=0; i<(length%5); i++) { result[index++] = (byte)(rand&63); rand >>= 6; } return result; } public static byte[] encrypt(byte[] key, byte[] b) { if(key==null || b==null){ throw new IllegalArgumentException(); } int index = 0; byte[] result = (byte[])b.clone(); for(int i=0; i<(b.length/key.length); i++){ for(int j=0; j<key.length; j++){ result[index++] += key[j]; } } for(int i=0; i<(b.length%key.length); i++){ result[index++] += key[i]; } return result; } public static byte[] decrypt(byte[] key, byte[] b) { if(key==null || b==null){ throw new IllegalArgumentException(); } int index = 0; byte[] result = (byte[])b.clone(); for(int i=0; i<(b.length/key.length); i++){ for(int j=0; j<key.length; j++){ result[index++] -= key[j]; } } for(int i=0; i<(b.length%key.length); i++){ result[index++] -= key[i]; } return result; }} 这个问题简单,看看加解密api吧 public static javax.crypto.SecretKey genDESKey(byte[] key_byte) throws Exception { SecretKey k = null; k = new SecretKeySpec(key_byte, "DESede"); return k; } public static byte[] desDecrypt(javax.crypto.SecretKey key, byte[] crypt) throws Exception { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance( "DESede"); cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key); return cipher.doFinal(crypt); } public static byte[] desEncrypt(javax.crypto.SecretKey key, byte[] src) throws Exception { javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance( "DESede"); cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key); return cipher.doFinal(src); } 加密後的字符串會不一樣呢程序中演示的是随机密码。byte[] key = getRandomKey(5); to: ntzls(三星堆)我是想對加密後的字符串進行解密,這樣就可以得到原來的字符串了 有了加密时的byte[] key 不就可以解密吗? ./ .--... .-------- 0 .-- / ....... ' .. '........ ---- . --.. . .---.. - 分 o . ' -. 分 / ' / ' ' ' \-分--/....... . ' ' ' ' \ __/ - - -' ' ' ' ' ' ' ' o ' ' ' ' ' ' ' -..'.----------. I ---------- /////////. Java的多线程问题 求高人帮忙解决算法 Spring 事务管理拦截器疑问 我来征集一些关于系统部分的设计意见 如何在JComboBox里得到正在编辑的字符串??? Eclipse添加JavaMail+JAF出现的怪问题! 请教高手高手高高手 真的就没有人能解决吗??????????? 如何把 string 数转成 int 类 树节点的复制粘贴功能 问小问题,up者送分!100分纪念几个重要日子 如何知道中文乱码的内容
java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1");
alga.update(myinfo.getBytes());
byte[] digesta=alga.digest(); //通过某中方式传给其他人你的信息(myinfo)和摘要(digesta) 对方可以判断是否更改或传输正常
java.security.MessageDigest algb=java.security.MessageDigest.getInstance("SHA-1");
algb.update(myinfo.getBytes());
if (algb.isEqual(digesta,algb.digest())) {
System.out.println("信息检查正常");
}
else
{
System.out.println("摘要不相同");
}
package natoni;import java.util.Random;public class SimpleEncoder {
private static Random random = new Random();
public static void main(String[] args){
String s = "hello world";
System.out.println(s+'\n');
byte[] key = getRandomKey(5);
byte[] s1 = encrypt(key,s.getBytes());
System.out.println(new String(s1)+'\n');
byte[] s2 = decrypt(key,s1);
System.out.println(new String(s2)+'\n');
}
public static byte[] getRandomKey(int length) {
if(length <= 0){
throw new IllegalArgumentException();
}
int rand, index=0;
byte[] result = new byte[length];
for(int i=0; i<(length/5); i++){
rand = random.nextInt();
for(int j=0; j<5; j++) {
result[index++] = (byte)(rand&63);
rand >>= 6;
}
}
rand = random.nextInt();
for(int i=0; i<(length%5); i++) {
result[index++] = (byte)(rand&63);
rand >>= 6;
}
return result;
}
public static byte[] encrypt(byte[] key, byte[] b) {
if(key==null || b==null){
throw new IllegalArgumentException();
}
int index = 0;
byte[] result = (byte[])b.clone();
for(int i=0; i<(b.length/key.length); i++){
for(int j=0; j<key.length; j++){
result[index++] += key[j];
}
}
for(int i=0; i<(b.length%key.length); i++){
result[index++] += key[i];
}
return result;
}
public static byte[] decrypt(byte[] key, byte[] b) {
if(key==null || b==null){
throw new IllegalArgumentException();
}
int index = 0;
byte[] result = (byte[])b.clone();
for(int i=0; i<(b.length/key.length); i++){
for(int j=0; j<key.length; j++){
result[index++] -= key[j];
}
}
for(int i=0; i<(b.length%key.length); i++){
result[index++] -= key[i];
}
return result;
}
}
Exception
{
SecretKey k = null;
k = new SecretKeySpec(key_byte, "DESede");
return k;
} public static byte[] desDecrypt(javax.crypto.SecretKey key, byte[] crypt) throws
Exception
{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(
"DESede");
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, key);
return cipher.doFinal(crypt);
} public static byte[] desEncrypt(javax.crypto.SecretKey key, byte[] src) throws
Exception
{
javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance(
"DESede");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(src);
}
.-------- 0 .--
/ .......
' ..
'........ ----
. --..
. .---.. -
分 o . ' -.
分 / '
/ ' ' '
\-分--/....... . ' ' ' '
\ __/ - - -' ' ' '
' ' ' '
o ' ' ' '
' ' '
-..'.----------.
I ----------
/////////.