楼主发表于:2013-03-04 11:24:21从网上找了一段加密解密的代码,没注释,大神给加个注释,至少告诉我: 1. 加密方法的两个参数是什么? 2. KeyGenerator、SecretKey、spec.SecretKeySpec这三个东西是什么? 加密解密是我们在生活中经常用到的一个方法。这个给我们带来了很大" />
您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Android >> 大神给加个注释

大神给加个注释

来源:网络整理     时间:2016/4/20 23:56:31     关键词:注释,大神

关于网友提出的“大神给加个注释”问题疑问,本网通过在网上对“大神给加个注释”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:大神给加个注释
描述:

从网上找了一段加密解密的代码,没注释,大神给加个注释,至少告诉我:
1. 加密方法的两个参数是什么?
2. KeyGenerator、SecretKey、spec.SecretKeySpec这三个东西是什么?
加密解密是我们在生活中经常用到的一个方法。这个给我们带来了很大的方便。因为有了加密我们的文件就会更加安全,这其中我们用到的是KeyGenerator、SecretKey、spec.SecretKeySpec,这三个在加密当中起到了非常大的作用。下面的代码就是讲述了怎么才能实现加密的效果。
源码如下:

package EOE.android.demo;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* Usage:

* String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
* ...
* String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)

* @author ferenc.hechler
*/
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes());
byte[] enc = toByte(encrypted);
byte[] result = decrypt(rawKey, enc);
return new String(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "0123456789ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}

解决方案1:

AES加密 解密嘛 还有des和3des的

解决方案2:

...encrypt加密,decrypt是解密。


以上介绍了“大神给加个注释”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/768740.html

相关图片

相关文章