Java 加密

Base64

Android有提供,Java没有提供

加密

Base64.encode(text.getBytes(), Base64.DEFAULT);

解密

Base64.decode(text.getBytes(),Base64.DEFAULT);

消息摘要算法

MD

MD5
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] encodeSrc = messageDigest.digest(src.getBytes());

应用:保护密码,保存密码摘要在数据库,然后服务器也使用摘要验证密码的正确性或者信息比较敏感的地方

SHA

SHA-1,SHA-2(SHA-224,SHA-256,SHA-384,SHA-512)

MessageDigest messageDigest = MessageDigest.getInstance("SHA");
byte[] shaCode = messageDigest.digest(src.getBytes());

应用:验证数据是否完整

MAC
HMAC
KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
SecretKey secretKey = keyGenerator.generateKey();
//the key is created by java
byte[] defaultKey = secretKey.getEncoded();
Log.e("Security","default = " + printByteArray(defaultKey));
//the key is created by you
byte[] key = ByteToHexString.decodeHex(new char[]{'c','a','3','9'});
Log.e("Security","define the key = " + printByteArray(key));
SecretKey restore = new SecretKeySpec(key,"HmacMD5");
Mac mac = Mac.getInstance(restore.getAlgorithm());
mac.init(restore);
byte[] hmacMd5Byte = mac.doFinal(src.getBytes());
return ByteToHexString.encodeHexString(hmacMd5Byte);

对称加密

DES

安全不够,已经很少使用了

// produce key
KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
keyGenerator.init(56);
SecretKey secretKey = keyGenerator.generateKey();
byte[] bytesKey = secretKey.getEncoded();

// change key
DESKeySpec dks = new DESKeySpec(bytesKey);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
Key key = skf.generateSecret(dks);

//encode
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] desByte = cipher.doFinal(src.getBytes());

//decode
Cipher decodeCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
decodeCipher.init(Cipher.DECRYPT_MODE,key);
byte[] decodeSrc = decodeCipher.doFinal(desByte);
return new String(decodeSrc);
3DES

用法和DES一样,只是更改算法名称和长度

AES

DES替代者

    try {
            // produce key
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            //会根据相应的算法使用相应的长度
            keyGenerator.init(new SecureRandom());
            SecretKey secretKey = keyGenerator.generateKey();
            byte[] bytesKey = secretKey.getEncoded();

            // change key
            Key key = new SecretKeySpec(bytesKey,"AES");

            //encode
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE,key);
            byte[] desByte = cipher.doFinal(src.getBytes());

            //decode
            Cipher decodeCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            decodeCipher.init(Cipher.DECRYPT_MODE,key);
            byte[] decodeSrc = decodeCipher.doFinal(desByte);
            return new String(decodeSrc);
        } catch (Exception e) {
            e.printStackTrace();
        }
PBE

PBE算法结合了摘要算法和DES算法的优点

    try {
            //初始化盐
            SecureRandom random = new SecureRandom();
            byte[] salt = random.generateSeed(8);

            //口令与密钥
            String password = "love zgx";
            PBEKeySpec pks = new PBEKeySpec(password.toCharArray());
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWITHMD5andDES");
            Key key = factory.generateSecret(pks);

            //加密
            PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt,100);
            Cipher cipher = Cipher.getInstance("PBEWITHMD5andDES");
            cipher.init(Cipher.ENCRYPT_MODE,key,pbeParameterSpec);
            byte[] pbeTarget = cipher.doFinal(src.getBytes());

            //解密
            cipher.init(Cipher.DECRYPT_MODE,key,pbeParameterSpec);
            byte[] pbeSrc = cipher.doFinal(pbeTarget);
            return new String(pbeSrc);
        }catch(Exception e){

        }

非对称加密

分公匙和私匙

DH密匙算法
RSA-基于因子分解
ELGamal-基于离散对数

数字签名算法

带有密钥(公钥,私钥)的消息摘要算法,验证数据完整性,认证数据来源,抗否认,私钥签名,公钥验证

RSA
DSA
ECDSA

你可能感兴趣的:(Java 加密)