?? cryptcode.java
字號:
package com.software.util;
import java.util.*;
import java.text.*;
import java.security.*;
public class CryptCode {
private final static String sMappingList = "$6kl!784mx~HIp?qr/s@tu;v[bw%JKL:oMV^cyz{eWX&gY>fZ-01#23.ST(Un5<AB)9NO+PQ]RC}DEF|G_da,hi*j";
private final static int nMappingLength = 89;
// This function uses to encrypt a string to an encrypted string.
public CryptCode(){
}
public static String Encrypt( String sInputText ) {
String sBeforeMapping = "";
String sAfterMapping = "";
String sEncrypted_string = "";
int nRotation = getRotationNo();
int i = 0, j = 0, k = 0;
sBeforeMapping = sInputText; //.toUpperCase();
int nEndIndex = sBeforeMapping.length();
char a;
char b[] = new char[nEndIndex];
// Search matching patterns.
while (i < nEndIndex){
a = sBeforeMapping.charAt(i);
j = 0;
while (j < nMappingLength) {
if (a == sMappingList.charAt(j)) {
k = j + nRotation;
if (k >= nMappingLength) {
a = sMappingList.charAt(k - nMappingLength);
} else {
a = sMappingList.charAt(k);
}
break;
}
j++;
}
b[i] = a;
i++;
}
String x = sMappingList;
String y = x.substring(nRotation, nRotation + 1);
int nTemp = nRotation + 5;
String z = x.substring(nTemp, nTemp + 1);
sEncrypted_string = sEncrypted_string.concat(y).concat(z);
for ( int m = 0; m < nEndIndex; m++ ) {
sEncrypted_string = sEncrypted_string.concat(sAfterMapping.valueOf(b[m]) );
}
return sEncrypted_string;
}
// This function uses to decrypt an encrypted string to a decrypted string.
public static String Decrypt( String sInputText ) {
String sBeforeMapping = "";
String sAfterMapping = "";
String sDecrypted_string = "";
int i = 0, j = 0, k = 0;
sBeforeMapping = sInputText; //.toUpperCase();
int nEndIndex = sBeforeMapping.length();
String x = sMappingList;
int nRotation = x.indexOf(sBeforeMapping.substring(0, 1));
char a;
char b[] = new char[nEndIndex];
// Search matching patterns.
while (i < nEndIndex){
a = sBeforeMapping.charAt(i);
j = 0;
while (j < nMappingLength) {
if (a == sMappingList.charAt(j)) {
k = j - nRotation;
if (k >= 0) {
a = sMappingList.charAt(k);
} else {
a = sMappingList.charAt(k + nMappingLength);
}
break;
}
j++;
if (j == nMappingLength) {
a = ' ';
}
}
b[i] = a;
i++;
}
for ( int m = 2; m < nEndIndex; m++ ) {
sDecrypted_string = sDecrypted_string.concat(sAfterMapping.valueOf(b[m]) );
}
return sDecrypted_string;
}
// This function uses to get a rotation number randomly (Between 1 to 30).
private static int getRotationNo() {
Date dDate = new Date();
SimpleDateFormat dDateTime = new SimpleDateFormat("ss");
dDateTime.setTimeZone(TimeZone.getTimeZone("CTT"));
int nSec = Integer.parseInt(dDateTime.format(dDate));
return Math.abs((nSec / 2) + 1);
}
public static String encrypt2(String password) {
password = password.toUpperCase();
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException("Unable to use MD5 algorithm for encryption.");
}
byte[] digest = md5.digest(password.getBytes());
char[] hex = new char[digest.length * 2];
for (int i = 0; i < digest.length; i++) {
char c;
c = (char)((digest[i] >> 4) & 0xf);
if (c > 9) {
c = (char)((c - 10) + 'a');
} else {
c = (char)(c + '0');
}
hex[2 * i] = c;
c = (char)(digest[i] & 0xf);
if (c > 9) {
c = (char)((c - 10) + 'a');
} else {
c = (char)(c + '0');
}
hex[2 * i + 1] = c;
}
return String.valueOf(hex);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -