javabyte加密
⑵ 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地址欄加密
軟體開發中,當在不同頁面之間傳遞參數時,為了系統的安全,常將地址欄中的信息加密處理,由於是通過表單傳遞數據,野弊陪因此我們不能用Java代碼對其加密,只能通過js函數對數據信息加密,下面是我在網上找的js的加密代碼(採用base64加密方式):<script type="text/javascript"><!--var keyStr = "ABCDEFGHIJKLMNOP" +
"QRSTUVWXYZabcdef" +
"ghijklmnopqrstuv" +
"wxyz0123456789+/" +
"=";//頌蠢加密函數function encode64(input) {
input = escape(input);//注意escape()函數
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}output = output +
keyStr.charAt(enc1) +
keyStr.charAt(enc2) +
keyStr.charAt(enc3) +
keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);return output;
}//卜圓解密函數function decode64(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
alert("There were invalid base64 characters in the input text.\n" +
"Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
"Expect errors in decoding.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;output = output + String.fromCharCode(chr1);if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";} while (i < input.length);return unescape(output);//注意unescape()函數
}
//--></script>當我們採用encode64(input)函數對數據加密之後,當要在Java代碼中對數據解密時,我們不能調用js的decoder(input)函數,必須使用Java語言編寫的函數。apache公司提供的commons-codec-1.3.jar類庫中的org.apache.commons.codec.binary.Base64包提供了encodeBase64(byte[] bts)和decodeBase64(byte[] bts)方法實現了數據的Base64()加密,但與上面的js代碼的加密和解密函數並不一一對應,為例實現用js代碼加密,用java函數解密,我們必須調用另外一個java函數,unescape(String src),其代碼如下:private static String unescape(String src) {
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length());
int lastPos = 0, pos = 0;
char ch;
while (lastPos < src.length()) {
pos = src.indexOf("%", lastPos);
if (pos == lastPos) {
if (src.charAt(pos + 1) == 'u') {
ch = (char) Integer.parseInt(src
.substring(pos + 2, pos + 6), 16);
tmp.append(ch);
lastPos = pos + 6;
} else {
ch = (char) Integer.parseInt(src
.substring(pos + 1, pos + 3), 16);
tmp.append(ch);
lastPos = pos + 3;
}
} else {
if (pos == -1) {
tmp.append(src.substring(lastPos));
lastPos = src.length();
} else {
tmp.append(src.substring(lastPos, pos));
lastPos = pos;
}
}
}
return tmp.toString();
}與js代碼中的unescape() 函數對應,才能正確的對數據信息解密,解密方法為:public static String decode64(String encode){
//調用org.apache.commons.codec.binary.Base64包,在commons-codec-1.3.jar中
Base64 base64 = new Base64();
byte[] byteOfEncode = encode.getBytes();
byte[] byteOfDecode = Base64.decodeBase64(byteOfEncode);//調用decodeBase64方法
String decode = new String(byteOfDecode);
return unescape(decode);//調用unescape(String src)方法
}附:在網上找到的java編寫的escape()方法:public class EscapeUnescape {
public static String escape(String src) {
int i;
char j;
StringBuffer tmp = new StringBuffer();
tmp.ensureCapacity(src.length() * 6);
for (i = 0; i < src.length(); i++) {
j = src.charAt(i);
if (Character.isDigit(j) || Character.isLowerCase(j)
|| Character.isUpperCase(j))
tmp.append(j);
else if (j < 256) {
tmp.append("%");
if (j < 16)
tmp.append("0");
tmp.append(Integer.toString(j, 16));
} else {
tmp.append("%u");
tmp.append(Integer.toString(j, 16));
}
}
return tmp.toString();
}
⑸ 深入Java位元組碼加密
問 如果我把我的class文件加密 在運行時用指定的類載入器(class loader)裝入並解密它 這樣子能防止被反編譯嗎? 答 防止JAVA位元組碼反編譯這個問題在java語言雛形期就有了 盡管市面上存在一些反編譯的工具可以利用 但是JAVA程序員還是不斷的努力尋找新的更有效的方法來保護他們的智慧結晶 在此 我將詳細給大家解釋這一直來在論壇上有爭議的話題 Class文件能被很輕松的重構生成JAVA源文件與最初JAVA位元組碼的設計目的和商業交易有緊密地聯系 另外 JAVA位元組碼被設計成簡潔 平台獨立性 網路靈活性 並且易於被位元組碼解釋器和JIT (just in time)/HotSpot 編譯器所分析 可以清楚地了解程序員的目的 Class文件要比JAVA源文件更易於分析 如配差指果不能阻止被反編譯的話 至少可以通過一些方法來增加它的困難性 例如: 在慶絕一個分步編譯里 你可以打亂Class文件的數據以使其難讀或者難以被反編譯成正確的JAVA源文件 前者可以採用極端函數重載 後者用操作控制流建立控制結構使其難以恢復正常次序 有更多成功的商業困惑者採用這些或其他的技術來保護自己的代碼 不幸的是 哪種方法都必須改變JVM運行的代碼 並且許多用戶害怕這種轉化會給他們的程序帶來新的Bug 而且 方法和欄位重命名會調用反射從而使程序停止工作 改變類和包的名字會破壞其他的JAVA APIS(JNDI URL providers etc) 除了改變名字 如果位元組碼偏移量和源代碼行數之間的關系改變了 在恢復這有異常的堆棧將很困難 於是就有了一些打亂JAVA源代碼的選項 但是這將從本質上導致一系列問題的產生 加密而不打亂 或許上述可能會使你問 假如我把位元組碼加密而不是處理位元組碼 並且JVM運行時自動將它解密並裝入類載入器 然後JVM運行解密後的位元組碼文件 這樣就不會被反編譯了對嗎?考慮到你是第一個提出這種想法的並且它又能正常運行 我表示遺憾和不幸 這種想法是錯誤的 下面是一個簡單的類編碼器 為了闡明這種思想 我採用了一個實例和一個很通用的類載入器來運行它 該程序包括兩個類 public class Main{public static void main (final String [] args){ System out println ( secret result = + MySecretClass mySecretAlgorithm ());}} // End of classpackage de;import java util Random;public class MySecretClass{/** * Guess what the secret algorithm just uses a random number generator */public static int mySecretAlgorithm (){return (int) s_random nextInt ();}private static final Random s_random = new Random (System currentTimeMillis ());} // End of class我想通過加密相關的培配class文件並在運行期解密來隱藏de MySecretClass的執行 用下面這個工具可以達到效果(你可以到這里下載Resources) public class EncryptedClassLoader extends URLClassLoader{public static void main (final String [] args)throws Exception{if ( run equals (args [ ]) && (args length >= )){// Create a custom loader that will use the current loader as// delegation parent:final ClassLoader appLoader =new EncryptedClassLoader (EncryptedClassLoader class getClassLoader () new File (args [ ]));// Thread context loader must be adjusted as well:Thread currentThread () setContextClassLoader (appLoader);final Class app = appLoader loadClass (args [ ]);final Method appmain = app getMethod ( main new Class [] {String [] class});final String [] appargs = new String [args length ];System array (args appargs appargs length);appmain invoke (null new Object [] {appargs});}else if ( encrypt equals (args [ ]) && (args length >= )){ encrypt specified classes }elsethrow new IllegalArgumentException (USAGE);}/** * Overrides java lang ClassLoader loadClass() to change the usual parent child * delegation rules just enough to be able to snatch application classes * from under system classloader s nose */public Class loadClass (final String name final boolean resolve)throws ClassNotFoundException{if (TRACE) System out println ( loadClass ( + name + + resolve + ) );Class c = null;// First check if this class has already been defined by this classloader// instance:c = findLoadedClass (name);if (c == null){Class parentsVersion = null;try{// This is slightly unorthodox: do a trial load via the// parent loader and note whether the parent delegated or not;// what this acplishes is proper delegation for all core// and extension classes without my having to filter on class name: parentsVersion = getParent () loadClass (name);if (parentsVersion getClassLoader () != getParent ())c = parentsVersion;}catch (ClassNotFoundException ignore) {}catch (ClassFormatError ignore) {}if (c == null){try{// OK either c was loaded by the system (not the bootstrap// or extension) loader (in which case I want to ignore that// definition) or the parent failed altogether; either way I// attempt to define my own version:c = findClass (name);}catch (ClassNotFoundException ignore){// If that failed fall back on the parent s version// [which could be null at this point]:c = parentsVersion;}}}if (c == null)throw new ClassNotFoundException (name);if (resolve)resolveClass (c);return c;}/** * Overrides java new URLClassLoader defineClass() to be able to call * crypt() before defining a class */protected Class findClass (final String name)throws ClassNotFoundException{if (TRACE) System out println ( findClass ( + name + ) );// class files are not guaranteed to be loadable as resources;// but if Sun s code does it so perhaps can mine final String classResource = name replace ( / ) + class ;final URL classURL = getResource (classResource);if (classURL == null)throw new ClassNotFoundException (name);else{InputStream in = null;try{in = classURL openStream ();final byte [] classBytes = readFully (in); lishixin/Article/program/Java/hx/201311/25555
⑹ 寫一個java加密程序
publicclass_Test2
{
publicstaticvoidmain(String[]args)throwsFileNotFoundException,IOException
{
//為了便於理解,所以有的部分為了通俗寫得不夠好
Scannersc=newScanner(System.in);
Stringline=sc.nextLine();
line=encrypt(line);
System.out.println(line);
newFileOutputStream("d:\word.txt").write((line+" ").getBytes());
}
//該方法是將一行數據簡單加密(愷撒加密)
privatestaticStringencrypt(Stringline){
char[]chars=line.toCharArray();
for(inti=0;i<chars.length;i++)
{
//由ASCII碼表得知大寫字母是65-90,小寫字母是97-122
if((chars[i]>=65&&chars[i]<90)||(chars[i]>=97&&chars[i]<122))
{
chars[i]=(char)(chars[i]+1);
}
if(chars[i]==90)
{
chars[i]='a';
}
if(chars[i]==122)
{
chars[i]='A';
}
}
returnnewString(chars);
}
}
⑺ 在java快速和簡單的字元串加密/解密問題,怎麼解決
publicclassTest{
publicstaticvoidmain(String[]args)throwsException{
Stringa="在java快速和簡單的字元串加密/解密問題,怎麼解決";
System.out.println("原字元串:"+a);
Stringb=deal(a,(byte)88);//88為加密密鑰
System.out.println("加密後字元串:"+b);
Stringc=deal(b,(byte)88);//88為解密密鑰,要和加密一致,否則無法解密
System.out.println("解密後字元串:"+c);}
/**
*簡單加密加密解密字元串<br/>
*加密解密思路:先將字元串變成byte數組,再將數組每位與key做位運算,得到新的數組就是加密或解密後的byte數組.<br/>
*知識:^是java位運算,可以網路了解下,a=b^skey反之也成立,即b=a^skey
*@paramstr解密/加密字元串
*@paramskey解密/加密密鑰(加密解密為同一個密鑰才能解密,否則是亂碼)
*@return
*@throwsException
*/
staticStringdeal(Stringstr,byteskey)throwsException{
byte[]bytes=str.getBytes("GBK");
for(inti=0;i<bytes.length;i++){
bytes[i]=(byte)(bytes[i]^skey);
}
returnnewString(bytes,"GBK");
}
}
⑻ 如何利用JAVA對文檔進行加密和解密處理,完整的java類
我以前上密碼學課寫過一個DES加解密的程序,是自己實現的,不是通過調用java庫函數,代碼有點長,帶有用戶界面。需要的話聯系我
⑼ java中如何對中文字元串進行加密
簡單的加密 ?
按char轉byte,然後固定長度保存起來就是了
⑽ Java軟體如何加密
Cipher c=Cipher.getInstance("AES");
c.init(c.ENCRYPT_MODE,new SecretKeySpec("1111111111111111".getBytes(),"AES"));
FileOutputStream fos=new FileOutputStream("./1.dat");
fos.write(c.doFinal("我神不是人啊~~".getBytes()));
c.init(c.DECRYPT_MODE,new SecretKeySpec("1111111111111111".getBytes(),"AES"));
FileInputStream fin=new FileInputStream("./1.dat");
byte b[]=new byte[1024];
System.out.println(new String(c.doFinal(b,0,fin.read(b))));