当前位置:首页 » 编程语言 » md5算法java

md5算法java

发布时间: 2024-05-10 09:24:56

1. java中有没有提供MD5算法的包啊

有,在java.security包的MessageDigest类。
例子:
import java.security.MessageDigest;
public class Test2 {
public static void main(String[] args) {
Test2 t = new Test2();
System.out.println(t.bytesToMD5("a".getBytes()));
}
//把字节数组转成16进位制数
public String bytesToHex(byte[] bytes) {
StringBuffer md5str = new StringBuffer();
//把数组每一字节换成16进制连成md5字符串
int digital;
for (int i = 0; i < bytes.length; i++) {
digital = bytes[i];
if(digital < 0) {
digital += 256;
}
if(digital < 16){
md5str.append("0");
}
md5str.append(Integer.toHexString(digital));
}
return md5str.toString();
}
//把字节数组转换成md5
public String bytesToMD5(byte[] input) {
String md5str = null;
try {
//创建一个提供信息摘要算法的对象,初始化为md5算法对象
MessageDigest md = MessageDigest.getInstance("MD5");
//计算后获得字节数组
byte[] buff = md.digest(input);
//把数组每一字节换成16进制连成md5字符串
md5str = bytesToHex(buff);
} catch (Exception e) {
e.printStackTrace();
}
return md5str;
}
}

2. java算法 md5加密方法

根据MD5算法的特点,我们可以把MD5加密过程看作是一个函数调用过程,建议必须做如下方式修改,这样可以保证一定程度上你的网站用户和数据安全:
1、修改MD5算法中的4个常数,这是最捷径的作法,其特点是加密后的数据和加密前非常类似,但是不会被破解
2、多次加密,对MD5加密过的数据进行二次或三次加密,或者在每次加密后从重抽取部分值进行在加密,比如“我爱你”,加密后“”,我们可以取任意一部分进行再加密,比如取前18位“1E6986ACEC7BAE541”进行再加密得到“”,这种做法修改很简单,比如asp中调用是md5("password")那么你可以改成md5(left(md5("password"),16)),这样以来就很安全了,就是你的数据被下载,破解的话也是不可能的
3、仿MD5加密,顾名思义,我们不采用MD5加密,而采用其他算法,然后取其中的部分散列,比如用SHA1或SHA64得到加密结果,然后取其中的32位或16位,很像MD5算法加密的结果,可以保证不被破解
方法有很多,我这里只是抛砖引玉,希望你在做网站的时候自己修改,可以确保万无一失,不管你用的是什么软件,希望大家谨慎一下,我们把这种改法称为MD5的私有算法或私有MD5算法。

3. MD5算法原理及实现

散列函数,也称作哈希函数,消息摘要函数,单向函数或者杂凑函数。散列函数主要用于验证数据的完整性。通过散列函数,可以创建消息的“数字指纹”,消息接收方可以通过校验消息的哈希值来验证消息的完整性,防止消息被篡改。散列函数具有以下特性:

任何消息经过散列函数处理后,都会产生一个唯一的散列值,这个散列值可以用来验证消息的完整性。计算消息散列值的过程被称为“消息摘要”,计算消息散列值的算法被称为消息摘要算法。常使用的消息摘要算法有:MD—消息摘要算法,SHA—安全散列算法,MAC—消息认证码算法。本文主要来了解MD算法。

MD5算法是典型的消息摘要算法,它是由MD4,MD3和MD2算法演变而来。无论是哪一种MD算法,其原理都是接受一个任意长度的消息并产生一个128位的消息摘要。如果把得到的消息摘要转换成十六进制字符串,则会得到一个32字节长度的字符串,我们平常见到的大部分MD数字指纹就是一个长度为32的十六进制字符串。

假设原始消息长度是b(以bit为单位),注意这里b可以是任意长度,并不一定要是8的整数倍。计算该消息MD5值的过程如下:

在计算消息的MD5值之前,首先对原始信息进行填充,这里的信息填充分为两步。
第一步,对原始信息进行填充,填充之后,要求信息的长度对512取余等于448。填充的规则如下:假设原始信息长度为b bit,那么在信息的b+1 bit位填充1,剩余的位填充0,直到信息长度对512取余为448。这里有一点需要注意,如果原始信息长度对512取余正好等于448,这种情况仍然要进行填充,很明显,在这时我们要填充的信息长度是512位,直到信息长度对512取余再次等于448。所以,填充的位数最少为1,最大为512。
第二步,填充信息长度,我们需要把原始信息长度转换成以bit为单位,然后在第一步操作的结果后面填充64bit的数据表示原始信息长度。第一步对原始信息进行填充之后,信息长度对512取余结果为448,这里再填充64bit的长度信息,整个信息恰好可以被512整除。其实从后续过程可以看到,计算MD5时,是将信息分为若干个分组进行处理的,每个信息分组的长度是512bit。

在进行MD5值计算之前,我们先来做一些定义。

下面就是最核心的信息处理过程,计算MD5的过程实际上就是轮流处理每个信息分组的过程。

MD5算法实现如下所示。

这里也和Java提供的标准MD5算法进行了对比,通过测试可以看到该MD5计算的结果和Java标准MD5算法的计算结果是一样的。

4. Java计算md5时字段格式有影响吗

Java中使用MD5算法计算消息摘要时,输入字符串的格式是有影响的。为了得到正确的结果,你需要确保输入字符串的格式符合一定的要求。比如,如果你使用了多余的空格或其他非法字符,那么你得到的消息摘要可能会与预期不符。
需要注意的是,不同的编码方式可能会导致相同的字符串在计算消息摘要时得到不同的结果。因此,在使用Java的MD5算法时,你应该确保输入字符串的编码方式与你预期的一致。
总之,在Java中使用MD5算法时,输入字符串的格式是有影响的,你需要注意字符串的格式以及编码方式,以确保得到正确的结果。
回答不易,望请采纳

5. java的MD5withRSA算法可以看到解密的内容么

您好,
<一>. MD5加密算法:
? ? ? ?消息摘要算法第五版(Message Digest Algorithm),是一种单向加密算法,只能加密、无法解密。然而MD5加密算法已经被中国山东大学王小云教授成功破译,但是在安全性要求不高的场景下,MD5加密算法仍然具有应用价值。
?1. 创建md5对象:?
<pre name="code" class="java">MessageDigest md5 = MessageDigest.getInstance("md5");
?2. ?进行加密操作:?
byte[] cipherData = md5.digest(plainText.getBytes());

?3. ?将其中的每个字节转成十六进制字符串:byte类型的数据最高位是符号位,通过和0xff进行与操作,转换为int类型的正整数。?
String toHexStr = Integer.toHexString(cipher & 0xff);

?4. 如果该正数小于16(长度为1个字符),前面拼接0占位:确保最后生成的是32位字符串。?
builder.append(toHexStr.length() == 1 ? "0" + toHexStr : toHexStr);

?5.?加密转换之后的字符串为:?
?6. 完整的MD5算法应用如下所示:?
/**
* 功能简述: 测试MD5单向加密.
* @throws Exception
*/
@Test
public void test01() throws Exception {
String plainText = "Hello , world !";
MessageDigest md5 = MessageDigest.getInstance("md5");
byte[] cipherData = md5.digest(plainText.getBytes());
StringBuilder builder = new StringBuilder();
for(byte cipher : cipherData) {
String toHexStr = Integer.toHexString(cipher & 0xff);
builder.append(toHexStr.length() == 1 ? "0" + toHexStr : toHexStr);
}
System.out.println(builder.toString());
//
}

??
<二>. 使用BASE64进行加密/解密:
? ? ? ? 使用BASE64算法通常用作对二进制数据进行加密,加密之后的数据不易被肉眼识别。严格来说,经过BASE64加密的数据其实没有安全性可言,因为它的加密解密算法都是公开的,典型的防菜鸟不防程序猿的呀。?经过标准的BASE64算法加密后的数据,?通常包含/、+、=等特殊符号,不适合作为url参数传递,幸运的是Apache的Commons Codec模块提供了对BASE64的进一步封装。? (参见最后一部分的说明)
?1.?使用BASE64加密:?
BASE64Encoder encoder = new BASE64Encoder();
String cipherText = encoder.encode(plainText.getBytes());

? 2.?使用BASE64解密:?
BASE64Decoder decoder = new BASE64Decoder();
plainText = new String(decoder.decodeBuffer(cipherText));

? 3. 完整代码示例:?
/**
* 功能简述: 使用BASE64进行双向加密/解密.
* @throws Exception
*/
@Test
public void test02() throws Exception {
BASE64Encoder encoder = new BASE64Encoder();
BASE64Decoder decoder = new BASE64Decoder();
String plainText = "Hello , world !";
String cipherText = encoder.encode(plainText.getBytes());
System.out.println("cipherText : " + cipherText);
//cipherText : SGVsbG8gLCB3b3JsZCAh
System.out.println("plainText : " +
new String(decoder.decodeBuffer(cipherText)));
//plainText : Hello , world !
}

??
<三>. 使用DES对称加密/解密:
? ? ? ? ?数据加密标准算法(Data Encryption Standard),和BASE64最明显的区别就是有一个工作密钥,该密钥既用于加密、也用于解密,并且要求密钥是一个长度至少大于8位的字符串。使用DES加密、解密的核心是确保工作密钥的安全性。
?1.?根据key生成密钥:?
DESKeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
SecretKey secretKey = keyFactory.generateSecret(keySpec);

? 2.?加密操作:?
Cipher cipher = Cipher.getInstance("des");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
byte[] cipherData = cipher.doFinal(plainText.getBytes());

? 3.?为了便于观察生成的加密数据,使用BASE64再次加密:?
String cipherText = new BASE64Encoder().encode(cipherData);

? ? ?生成密文如下:PtRYi3sp7TOR69UrKEIicA==?
? 4.?解密操作:?
cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom());
byte[] plainData = cipher.doFinal(cipherData);
String plainText = new String(plainData);

? 5. 完整的代码demo:?
/**
* 功能简述: 使用DES对称加密/解密.
* @throws Exception
*/
@Test
public void test03() throws Exception {
String plainText = "Hello , world !";
String key = "12345678"; //要求key至少长度为8个字符

SecureRandom random = new SecureRandom();
DESKeySpec keySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("des");
SecretKey secretKey = keyFactory.generateSecret(keySpec);

Cipher cipher = Cipher.getInstance("des");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, random);
byte[] cipherData = cipher.doFinal(plainText.getBytes());
System.out.println("cipherText : " + new BASE64Encoder().encode(cipherData));
//PtRYi3sp7TOR69UrKEIicA==

cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
byte[] plainData = cipher.doFinal(cipherData);
System.out.println("plainText : " + new String(plainData));
//Hello , world !
}

??
<四>. 使用RSA非对称加密/解密:
? ? ? ? RSA算法是非对称加密算法的典型代表,既能加密、又能解密。和对称加密算法比如DES的明显区别在于用于加密、解密的密钥是不同的。使用RSA算法,只要密钥足够长(一般要求1024bit),加密的信息是不能被破解的。用户通过https协议访问服务器时,就是使用非对称加密算法进行数据的加密、解密操作的。
? ? ? ?服务器发送数据给客户端时使用私钥(private key)进行加密,并且使用加密之后的数据和私钥生成数字签名(digital signature)并发送给客户端。客户端接收到服务器发送的数据会使用公钥(public key)对数据来进行解密,并且根据加密数据和公钥验证数字签名的有效性,防止加密数据在传输过程中被第三方进行了修改。
? ? ? ?客户端发送数据给服务器时使用公钥进行加密,服务器接收到加密数据之后使用私钥进行解密。
?1.?创建密钥对KeyPair:
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
keyPairGenerator.initialize(1024); //密钥长度推荐为1024位.
KeyPair keyPair = keyPairGenerator.generateKeyPair();

? 2.?获取公钥/私钥:
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

? 3.?服务器数据使用私钥加密:
Cipher cipher = Cipher.getInstance("rsa");
cipher.init(Cipher.ENCRYPT_MODE, privateKey, new SecureRandom());
byte[] cipherData = cipher.doFinal(plainText.getBytes());

? 4.?用户使用公钥解密:
cipher.init(Cipher.DECRYPT_MODE, publicKey, new SecureRandom());
byte[] plainData = cipher.doFinal(cipherData);

? 5.?服务器根据私钥和加密数据生成数字签名:
Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(cipherData);
byte[] signData = signature.sign();

? 6.?用户根据公钥、加密数据验证数据是否被修改过:
signature.initVerify(publicKey);
signature.update(cipherData);
boolean status = signature.verify(signData);

? 7. RSA算法代码demo:<img src="http://www.cxyclub.cn/Upload/Images/2014081321/99A5FC9C0C628374.gif" alt="尴尬" title="尴尬" border="0">
/**
* 功能简述: 使用RSA非对称加密/解密.
* @throws Exception
*/
@Test
public void test04() throws Exception {
String plainText = "Hello , world !";

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("rsa");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();

PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

Cipher cipher = Cipher.getInstance("rsa");
SecureRandom random = new SecureRandom();

cipher.init(Cipher.ENCRYPT_MODE, privateKey, random);
byte[] cipherData = cipher.doFinal(plainText.getBytes());
System.out.println("cipherText : " + new BASE64Encoder().encode(cipherData));
//gDsJxZM98U2GzHUtUTyZ/Ir/
///ONFOD0fnJoGtIk+T/+3yybVL8M+RI+HzbE/jdYa/+
//yQ+vHwHqXhuzZ/N8iNg=

cipher.init(Cipher.DECRYPT_MODE, publicKey, random);
byte[] plainData = cipher.doFinal(cipherData);
System.out.println("plainText : " + new String(plainData));
//Hello , world !

Signature signature = Signature.getInstance("MD5withRSA");
signature.initSign(privateKey);
signature.update(cipherData);
byte[] signData = signature.sign();
System.out.println("signature : " + new BASE64Encoder().encode(signData));
//+
//co64p6Sq3kVt84wnRsQw5mucZnY+/+vKKXZ3pbJMNT/4
///t9ewo+KYCWKOgvu5QQ=

signature.initVerify(publicKey);
signature.update(cipherData);
boolean status = signature.verify(signData);
System.out.println("status : " + status);
//true
}

6. 如何使用Java生成MD5代码

这是我以前做的一个小项目时用到md5写的


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

//将用户密码进行md5加密 并返回加密后的32位十六进制密码

public class MD5Util {
public static String md5(String password) {

try {
// 获取md5对象
MessageDigest md = MessageDigest.getInstance("md5");
// 获取加密后的密码并返回十进制字节数组
byte[] bytes = md.digest(password.getBytes());

// 遍历数组得到每个十进制数并转换成十六进制

StringBuffer sb = new StringBuffer();
for (byte b : bytes) {

// 把每个数转成十六进制 存进字符中
sb.append(toHex(b));
}
String finish = sb.toString();
return finish;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

}

// 十进制转十六进制方法
private static String toHex(byte b) {

int target = 0;

if (b < 0) {
target = 255 + b;
} else {
target = b;
}

int first = target / 16;
int second = target % 16;

return Hex[first] + Hex[second];
}

static String[] Hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" };

/*public static void main(String[] args) {
String a = MD5Util.md5("1234");
System.out.println(a);
}*/
}

7. java项目如何加密

Java基本的单向加密算法:

1.BASE64 严格地说,属于编码格式,而非加密算法
2.MD5(Message Digest algorithm 5,信息摘要算法)
3.SHA(Secure Hash Algorithm,安全散列算法)
4.HMAC(Hash Message Authentication Code,散列消息鉴别码)
按 照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常见于邮件、http加密,截取http信息,你就会发现登录操作的用户名、密码字段通过BASE64加密的。
主要就是BASE64Encoder、BASE64Decoder两个类,我们只需要知道使用对应的方法即可。另,BASE加密后产生的字节位数是8的倍数,如果不够位数以=符号填充。
MD5
MD5 -- message-digest algorithm 5 (信息-摘要算法)缩写,广泛用于加密和解密技术,常用于文件校验。校验?不管文件多大,经过MD5后都能生成唯一的MD5值。好比现在的ISO校验,都 是MD5校验。怎么用?当然是把ISO经过MD5后产生MD5的值。一般下载linux-ISO的朋友都见过下载链接旁边放着MD5的串。就是用来验证文 件是否一致的。

HMAC
HMAC(Hash Message Authentication Code,散列消息鉴别码,基于密钥的Hash算法的认证协议。消息鉴别码实现鉴别的原理是,用公开函数和密钥产生一个固定长度的值作为认证标识,用这个 标识鉴别消息的完整性。使用一个密钥生成一个固定大小的小数据块,即MAC,并将其加入到消息中,然后传输。接收方利用与发送方共享的密钥进行鉴别认证 等。

8. 可变MD5加密(Java实现)

可变在这里含义很简单 就是最终的加密结果是可变的 而非必需按标准MD 加密实现 Java类库security中的MessageDigest类就提供了MD 加密的支持 实现起来非常方便 为了实现更多效果 我们可以如下设计MD 工具类

Java代码

package ** ** util;

import java security MessageDigest;

/**

* 标准MD 加密方法 使用java类库的security包的MessageDigest类处理

* @author Sarin

*/

public class MD {

/**

* 获得MD 加密密码的方法

*/

public static String getMD ofStr(String origString) {

String origMD = null;

try {

MessageDigest md = MessageDigest getInstance( MD );

byte[] result = md digest(origString getBytes());

origMD = byteArray HexStr(result);

} catch (Exception e) {

e printStackTrace();

}

return origMD ;

}

/**

* 处理字节数组得到MD 密码的方法

*/

private static String byteArray HexStr(byte[] bs) {

StringBuffer *** = new StringBuffer();

for (byte b : bs) {

*** append(byte HexStr(b));

}

return *** toString();

}

/**

* 字节标准移位转十六进制方法

*/

private static String byte HexStr(byte b) {

String hexStr = null;

int n = b;

if (n < ) {

//若需要自定义加密 请修改这个移位算法即可

n = b & x F + ;

}

hexStr = Integer toHexString(n / ) + Integer toHexString(n % );

return hexStr toUpperCase();

}

/**

* 提供一个MD 多次加密方法

*/

public static String getMD ofStr(String origString int times) {

String md = getMD ofStr(origString);

for (int i = ; i < times ; i++) {

md = getMD ofStr(md );

}

return getMD ofStr(md );

}

/**

* 密码验证方法

*/

public static boolean verifyPassword(String inputStr String MD Code) {

return getMD ofStr(inputStr) equals(MD Code);

}

/**

* 重载一个多次加密时的密码验证方法

*/

public static boolean verifyPassword(String inputStr String MD Code int times) {

return getMD ofStr(inputStr times) equals(MD Code);

}

/**

* 提供一个测试的主函数

*/

public static void main(String[] args) {

System out println( : + getMD ofStr( ));

System out println( : + getMD ofStr( ));

System out println( sarin: + getMD ofStr( sarin ));

System out println( : + getMD ofStr( ));

}

}

可以看出实现的过程非常简单 因为由java类库提供了处理支持 但是要清楚的是这种方式产生的密码不是标准的MD 码 它需要进行移位处理才能得到标准MD 码 这个程序的关键之处也在这了 怎么可变?调整移位算法不就可变了么!不进行移位 也能够得到 位的密码 这就不是标准加密了 只要加密和验证过程使用相同的算法就可以了

MD 加密还是很安全的 像CMD 那些穷举破解的只是针对标准MD 加密的结果进行的 如果自定义移位算法后 它还有效么?可以说是无解的了 所以MD 非常安全可靠

为了更可变 还提供了多次加密的方法 可以在MD 基础之上继续MD 就是对 位的第一次加密结果再MD 恩 这样去破解?没有任何意义

这样在MIS系统中使用 安全可靠 欢迎交流 希望对使用者有用

我们最后看看由MD 加密算法实现的类 那是非常庞大的

Java代码

import java lang reflect *;

/**

* **********************************************

* md 类实现了RSA Data Security Inc 在提交给IETF

* 的RFC 中的MD message digest 算法

* ***********************************************

*/

public class MD {

/* 下面这些S S 实际上是一个 * 的矩阵 在原始的C实现中是用#define 实现的

这里把它们实现成为static final是表示了只读 切能在同一个进程空间内的多个

Instance间共享*/

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final byte[] PADDING = {

};

/* 下面的三个成员是MD 计算过程中用到的 个核心数据 在原始的C实现中

被定义到MD _CTX结构中

*/

private long[] state = new long[ ]; // state (ABCD)

private long[] count = new long[ ]; // number of bits molo ^ (l *** first)

private byte[] buffer = new byte[ ]; // input buffer

/* digestHexStr是MD 的唯一一个公共成员 是最新一次计算结果的

进制ASCII表示

*/

public String digestHexStr;

/* digest 是最新一次计算结果的 进制内部表示 表示 bit的MD 值

*/

private byte[] digest = new byte[ ];

/*

getMD ofStr是类MD 最主要的公共方法 入口参数是你想要进行MD 变换的字符串

返回的是变换完的结果 这个结果是从公共成员digestHexStr取得的.

*/

public String getMD ofStr(String inbuf) {

md Init();

md Update(inbuf getBytes() inbuf length());

md Final();

digestHexStr = ;

for (int i = ; i < ; i++) {

digestHexStr += byteHEX(digest[i]);

}

return digestHexStr;

}

// 这是MD 这个类的标准构造函数 JavaBean要求有一个public的并且没有参数的构造函数

public MD () {

md Init();

return;

}

/* md Init是一个初始化函数 初始化核心变量 装入标准的幻数 */

private void md Init() {

count[ ] = L;

count[ ] = L;

///* Load magic initialization constants

state[ ] = x L;

state[ ] = xefcdab L;

state[ ] = x badcfeL;

state[ ] = x L;

return;

}

/* F G H I 是 个基本的MD 函数 在原始的MD 的C实现中 由于它们是

简单的位运算 可能出于效率的考虑把它们实现成了宏 在java中 我们把它们

实现成了private方法 名字保持了原来C中的 */

private long F(long x long y long z) {

return (x & y) | ((~x) & z);

}

private long G(long x long y long z) {

return (x & z) | (y & (~z));

}

private long H(long x long y long z) {

return x ^ y ^ z;

}

private long I(long x long y long z) {

return y ^ (x | (~z));

}

/*

FF GG HH和II将调用F G H I进行近一步变换

FF GG HH and II transformations for rounds and

Rotation is separate from addition to prevent reputation

*/

private long FF(long a long b long c long d long x long s long ac) {

a += F(b c d) + x + ac;

a = ((int) a << s) | ((int) a >>> ( s));

a += b;

return a;

}

private long GG(long a long b long c long d long x long s long ac) {

a += G(b c d) + x + ac;

a = ((int) a << s) | ((int) a >>> ( s));

a += b;

return a;

}

private long HH(long a long b long c long d long x long s long ac) {

a += H(b c d) + x + ac;

a = ((int) a << s) | ((int) a >>> ( s));

a += b;

return a;

}

private long II(long a long b long c long d long x long s long ac) {

a += I(b c d) + x + ac;

a = ((int) a << s) | ((int) a >>> ( s));

a += b;

return a;

}

/*

md Update是MD 的主计算过程 inbuf是要变换的字节串 inputlen是长度 这个

函数由getMD ofStr调用 调用之前需要调用md init 因此把它设计成private的

*/

private void md Update(byte[] inbuf int inputLen) {

int i index partLen;

byte[] block = new byte[ ];

index = (int) (count[ ] >>> ) & x F;

// /* Update number of bits */

if ((count[ ] += (inputLen << )) < (inputLen << ))

count[ ]++;

count[ ] += (inputLen >>> );

partLen = index;

// Transform as many times as possible

if (inputLen >= partLen) {

md Memcpy(buffer inbuf index partLen);

md Transform(buffer);

for (i = partLen; i + < inputLen; i += ) {

md Memcpy(block inbuf i );

md Transform(block);

}

index = ;

} else

i = ;

///* Buffer remaining input */

md Memcpy(buffer inbuf index i inputLen i);

}

/*

md Final整理和填写输出结果

*/

private void md Final() {

byte[] bits = new byte[ ];

int index padLen;

///* Save number of bits */

Encode(bits count );

///* Pad out to mod

index = (int) (count[ ] >>> ) & x f;

padLen = (index < ) ? ( index) : ( index);

md Update(PADDING padLen);

///* Append length (before padding) */

md Update(bits );

///* Store state in digest */

Encode(digest state );

}

/* md Memcpy是一个内部使用的byte数组的块拷贝函数 从input的inpos开始把len长度的

字节拷贝到output的outpos位置开始

*/

private void md Memcpy(byte[] output byte[] input int outpos int inpos int len) {

int i;

for (i = ; i < len; i++)

output[outpos + i] = input[inpos + i];

}

/*

md Transform是MD 核心变换程序 有md Update调用 block是分块的原始字节

*/

private void md Transform(byte block[]) {

long a = state[ ] b = state[ ] c = state[ ] d = state[ ];

long[] x = new long[ ];

Decode(x block );

/* Round */

a = FF(a b c d x[ ] S xd aa L); /* */

d = FF(d a b c x[ ] S xe c b L); /* */

c = FF(c d a b x[ ] S x dbL); /* */

b = FF(b c d a x[ ] S xc bdceeeL); /* */

a = FF(a b c d x[ ] S xf c fafL); /* */

d = FF(d a b c x[ ] S x c aL); /* */

c = FF(c d a b x[ ] S xa L); /* */

b = FF(b c d a x[ ] S xfd L); /* */

a = FF(a b c d x[ ] S x d L); /* */

d = FF(d a b c x[ ] S x b f afL); /* */

c = FF(c d a b x[ ] S xffff bb L); /* */

b = FF(b c d a x[ ] S x cd beL); /* */

a = FF(a b c d x[ ] S x b L); /* */

d = FF(d a b c x[ ] S xfd L); /* */

c = FF(c d a b x[ ] S xa eL); /* */

b = FF(b c d a x[ ] S x b L); /* */

/* Round */

a = GG(a b c d x[ ] S xf e L); /* */

d = GG(d a b c x[ ] S xc b L); /* */

c = GG(c d a b x[ ] S x e a L); /* */

b = GG(b c d a x[ ] S xe b c aaL); /* */

a = GG(a b c d x[ ] S xd f dL); /* */

d = GG(d a b c x[ ] S x L); /* */

c = GG(c d a b x[ ] S xd a e L); /* */

b = GG(b c d a x[ ] S xe d fbc L); /* */

a = GG(a b c d x[ ] S x e cde L); /* */

d = GG(d a b c x[ ] S xc d L); /* */

c = GG(c d a b x[ ] S xf d d L); /* */

b = GG(b c d a x[ ] S x a edL); /* */

a = GG(a b c d x[ ] S xa e e L); /* */

d = GG(d a b c x[ ] S xfcefa f L); /* */

c = GG(c d a b x[ ] S x f d L); /* */

b = GG(b c d a x[ ] S x d a c aL); /* */

/* Round */

a = HH(a b c d x[ ] S xfffa L); /* */

d = HH(d a b c x[ ] S x f L); /* */

c = HH(c d a b x[ ] S x d d L); /* */

b = HH(b c d a x[ ] S xfde cL); /* */

a = HH(a b c d x[ ] S xa beea L); /* */

d = HH(d a b c x[ ] S x bdecfa L); /* */

c = HH(c d a b x[ ] S xf bb b L); /* */

b = HH(b c d a x[ ] S xbebfbc L); /* */

a = HH(a b c d x[ ] S x b ec L); /* */

d = HH(d a b c x[ ] S xeaa faL); /* */

c = HH(c d a b x[ ] S xd ef L); /* */

b = HH(b c d a x[ ] S x d L); /* */

a = HH(a b c d x[ ] S xd d d L); /* */

d = HH(d a b c x[ ] S xe db e L); /* */

c = HH(c d a b x[ ] S x fa cf L); /* */

b = HH(b c d a x[ ] S xc ac L); /* */

/* Round */

a = II(a b c d x[ ] S xf L); /* */

d = II(d a b c x[ ] S x aff L); /* */

c = II(c d a b x[ ] S xab a L); /* */

b = II(b c d a x[ ] S xfc a L); /* */

a = II(a b c d x[ ] S x b c L); /* */

d = II(d a b c x[ ] S x f ccc L); /* */

c = II(c d a b x[ ] S xffeff dL); /* */

b = II(b c d a x[ ] S x dd L); /* */

a = II(a b c d x[ ] S x fa e fL); /* */

d = II(d a b c x[ ] S xfe ce e L); /* */

c = II(c d a b x[ ] S xa L); /* */

b = II(b c d a x[ ] S x e a L); /* */

a = II(a b c d x[ ] S xf e L); /* */

d = II(d a b c x[ ] S xbd af L); /* */

c = II(c d a b x[ ] S x ad d bbL); /* */

b = II(b c d a x[ ] S xeb d L); /* */

state[ ] += a;

state[ ] += b;

state[ ] += c;

state[ ] += d;

}

/*Encode把long数组按顺序拆成byte数组 因为java的long类型是 bit的

只拆低 bit 以适应原始C实现的用途

*/

private void Encode(byte[] output long[] input int len) {

int i j;

for (i = j = ; j < len; i++ j += ) {

output[j] = (byte) (input[i] & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

}

}

/*Decode把byte数组按顺序合成成long数组 因为java的long类型是 bit的

只合成低 bit 高 bit清零 以适应原始C实现的用途

*/

private void Decode(long[] output byte[] input int len) {

int i j;

for (i = j = ; j < len; i++ j += )

output[i] = b iu(input[j]) | (b iu(input[j + ]) << ) | (b iu(input[j + ]) << )

| (b iu(input[j + ]) << );

return;

}

/*

b iu是我写的一个把byte按照不考虑正负号的原则的"升位"程序 因为java没有unsigned运算

*/

public static long b iu(byte b) {

return b < ? b & x F + : b;

}

/*byteHEX() 用来把一个byte类型的数转换成十六进制的ASCII表示

因为java中的byte的toString无法实现这一点 我们又没有C语言中的

sprintf(outbuf % X ib)

*/

public static String byteHEX(byte ib) {

char[] Digit = { A B C D E F };

char[] ob = new char[ ];

ob[ ] = Digit[(ib >>> ) & X F];

ob[ ] = Digit[ib & X F];

String s = new String(ob);

return s;

}

public static void main(String args[]) {

MD m = new MD ();

if (Array getLength(args) == ) { //如果没有参数 执行标准的Test Suite

System out println( MD Test suite: );

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( a ): + m getMD ofStr( a ));

System out println( MD ( abc ): + m getMD ofStr( abc ));

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( message digest ): + m getMD ofStr( message digest ));

System out println( MD ( abcdefghijklmnopqrstuvwxyz ): + m getMD ofStr( abcdefghijklmnopqrstuvwxyz ));

System out println( MD ( ):

+ m getMD ofStr( ));

} else

System out println( MD ( + args[ ] + )= + m getMD ofStr(args[ ]));

}

lishixin/Article/program/Java/hx/201311/26604

9. java加密的几种方式

朋友你好,很高兴为你作答。

首先,Java加密能够应对的风险包括以下几个:

1、核心技术窃取

2、核心业务破解

3、通信模块破解

4、API接口暴露

本人正在使用几维安全Java加密方式,很不错,向你推荐,希望能够帮助到你。

几维安全Java2C针对DEX文件进行加密保护,将DEX文件中标记的Java代码翻译为C代码,编译成加固后的SO文件。默认情况只加密activity中的onCreate函数,如果开发者想加密其它类和方法,只需对相关类或函数添加标记代码,在APK加密时会自动对标记的代码进行加密处理。

与传统的APP加固方案相比,不涉及到自定义修改DEX文件的加载方式,所以其兼容性非常好;其次Java函数被完全转化为C函数,直接在Native层执行,不存在Java层解密执行的步骤,其性能和执行效率更优。

如果操作上有不明白的地方,可以联系技术支持人员帮你完成Java加密。

希望以上解答能够帮助到你。

热点内容
phpcgi启动 发布:2024-05-20 22:38:57 浏览:578
嵌入式存储服务器 发布:2024-05-20 22:14:55 浏览:395
sql分组条件 发布:2024-05-20 22:08:49 浏览:16
配网web服务器一个IP地址 发布:2024-05-20 22:07:16 浏览:725
电脑板服务器地址175 发布:2024-05-20 22:03:30 浏览:959
编译静态函数时 发布:2024-05-20 21:51:20 浏览:351
如何在别人的服务器加模组 发布:2024-05-20 21:28:29 浏览:61
服务器的bios芯片电脑店有吗 发布:2024-05-20 21:28:26 浏览:224
剪辑电影什么配置 发布:2024-05-20 21:25:17 浏览:818
解压神器中的诡异事件 发布:2024-05-20 21:17:59 浏览:7