常见的加密算法

1.对称加密

AES 加密

对称密钥加密中最流行的算法之一,Advanced Encryption Standard,安全级别不高的自动登录一般用AES加密,AES加密是比较快的。

动画演示:http://coolshell.cn/articles/3161.html

public class AES {


	/**
	 * 填充字符串,总长为16的倍数,AES加密算法要求 -- 此方法设为私有即可
	 * @param str
	 * @return
	 */
	private static String paddingString(String str) {
		int len = str.length();
		if (0 == len % 16) {
			return str;
		}
		byte[] bytes = new byte[(16 * (len / 16)) + 16];
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = '\0';
		}
		try {
			byte[] strbytes = str.getBytes("UTF-8");
			for (int i = 0; i < len; i++) {
				bytes[i] = strbytes[i];
			}
			return new String(bytes);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 加密
	 * @param sSrc
	 * @param sKey
	 * @return
	 * @throws Exception
	 */
	public static byte[] encrypt(String sSrc, String sKey) throws Exception {
		if (sKey == null) {
			return null;
		}
		
		if (sKey.length() != 16) {
			return null;
		}
		
		String encryptstr = paddingString(sSrc);
		
		byte[] raw = sKey.getBytes();
		SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
		Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
		cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
		byte[] encrypted = cipher.doFinal(encryptstr.getBytes());

		return encrypted;
	}

	/**
	 * 解密
	 * @param sSrc
	 * @param sKey
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(byte[] sSrc, String sKey) throws Exception {
		try {
			if (sKey == null) {
				return null;
			}
			if (sKey.length() != 16) {
				return null;
			}
			byte[] raw = sKey.getBytes("UTF-8");
			SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
			
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			try {
				byte[] original = cipher.doFinal(sSrc);
				String originalString = new String(original);
				return originalString;
			} catch (Exception e) {
				return null;
			}
		} catch (Exception ex) {
			return null;
		}
	}
}


2.非对称加密

1). RSA 非对称加密,公开密钥加密电子商业中RSA被广泛使用,如github 传输到服务器的代码用的ssl 就是 rsa加密,RSA加密是比较慢的。

公钥:加密

私钥:解密

http://blog.csdn.net/bbld_/article/details/38777491


2). MD5 散列码

public static String md5(String string) {
    byte[] hash;
    try {
        hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Huh, MD5 should be supported?", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Huh, UTF-8 should be supported?", e);
    }

    StringBuilder hex = new StringBuilder(hash.length * 2);
    for (byte b : hash) {
        if ((b & 0xFF) < 0x10) hex.append("0");
        hex.append(Integer.toHexString(b & 0xFF));
    }
    return hex.toString();
}


3) BASE 64

android Base64加密解密,androidbase64

// 加密传入的数据是byte类型的,并非使用decode方法将原始数据转二进制,String类型的数据 使用 str.getBytes()即可
String str = "Hello!";
// 在这里使用的是encode方式,返回的是byte类型加密数据,可使用new String转为String类型
String strBase64 = new String(Base64.encode(str.getBytes(), Base64.DEFAULT));
Log.i("Test", "encode >>>" + strBase64);
		
// 这里 encodeToString 则直接将返回String类型的加密数据
String enToStr = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
Log.i("Test", "encodeToString >>> " + enToStr);
		
// 对base64加密后的数据进行解密
Log.i("Test", "decode >>>" + new String(Base64.decode(strBase64.getBytes(), Base64.DEFAULT)));


java base64解密乱码问题 代码如下:
private String getPictureString() { String upload = ""; try { FileInputStream in = new FileInputStream(fileName); byte[] ba = new byte[in.available()]; in.read(ba); in.close(); upload = new String(android.util.Base64.encode(ba, android.util.Base64.DEFAULT)); } catch (FileNotFoundException e) { LogUtil.e(e.getMessage(), e); } catch (IOException e) { LogUtil.e(e.getMessage(), e); } return upload; }
这个是加密
解密就是
encode换成decode
upload = new String(android.util.Base64.decode(ba,
android.util.Base64.DEFAULT));

 
对于base64、md5等加密解密问题
base64的作用不是加密,而是用来避免“字节”中不能转换成可显示字符的数值。
比如0-32的控制字符,空格,制表符都不能被打印在纸上,base64只使用大写小写数字标点。
可以打印在纸上,数据可以在传统平面媒介上携带。

md5是散列函数,提取数据的特征,输出是不可逆的散列值,用于代表某信息A而又不暴露信息A的内容。不直接用于加密文件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值