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))));