数据传输加密技术

6 篇文章 0 订阅

转载:http://www.2cto.com/kf/201208/147460.html

数据传输加密技术


目的是对传输中的数据流加密, 常用的方针有线路加密和端对端加密两种。前者侧重
在线路上而不考虑信源与信宿, 是对保密信息通过各线路采用不同的加密密钥提供安全
保护。后者则指信息由发送者端通过专用的加密软件,采用某种加密技术对所发送文件进
行加密,把明文(也即原文)加密成密文(加密后的文件,这些文件内容是一些看不懂的代
码), 然后进入TCP/IP数据包封装穿过互联网, 当这些信息一旦到达目的地, 将由
收件人运用相应的密钥进行解密, 使密文恢复成为可读数据明文。目前最常用的加密技
术有对称加密技术和非对称加密技术,对称加密技术是指同时运用一个密钥进行加密和解
密,非对称加密方式就是加密和解密所用的密钥不一样,它有一对密钥,称为“公钥”和
“私钥”两个,这两上密钥必须配对使用,也就是说用公钥加密的文件必须用相应人的么
钥才能解密,反之亦然。
package com.cyh.util;

import java.security.MessageDigest;

public class MD5 {

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

// 十六进制下数字到字符的映射数组

/** 把inputString加密*/
public static String createPassword(String inputString) {
return encodeByMD5(inputString);
}

/**
* 验证输入的密码是否正确
*
* @param password
* 真正的密码(加密后的真密码)
* @param inputString
* 输入的字符串
* @return 验证结果,boolean类型
*/
public static boolean authenticatePassword(String password,
String inputString) {
if (password.equals(encodeByMD5(inputString))) {
return true;
} else {
return false;
}
}

/** 对字符串进行MD5编码*/
private static String encodeByMD5(String originString) {
if (originString != null) {
try {
// 创建具有指定算法名称的信息摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] results = md.digest(originString.getBytes());
// 将得到的字节数组变成字符串返回
String resultString = byteArrayToHexString(results);
return resultString.toUpperCase();
} catch (Exception ex) {
ex.printStackTrace();
}
}
return null;
}

/**
* 轮换字节数组为十六进制字符串
*
* @param b
* 字节数组 www.2cto.com
* @return 十六进制字符串
*/
private static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}

/**
* 将一个字节转化成十六进制形式的字符串
*/
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}

public static void main(String[] args) {
String password = MD5.createPassword("yaowenchao");
System.out.println("对yaowenchao用MD5摘要后的字符串:" + password);
String inputString = "tianshi0253";
System.out.println("tianshi0253与密码匹配?"
+ MD5.authenticatePassword(password, inputString));
inputString = "yaowenchao";
System.out.println("yaowenchao与密码匹配?"
+ MD5.authenticatePassword(password, inputString));
}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用加密技术来保护数据传输的安全性。下面是一个简单的示例: 1. 选择一个加密算法,例如 AES。 2. 生成一个密钥,可以使用 SecureRandom 类来生成一个随机密钥。 3. 使用密钥初始化加密器和解密器,可以使用 Cipher 类来实现。 4. 将明文数据加密并发送到接收方。 5. 接收方使用相同的密钥初始化解密器并将密文数据解密成明文数据。 下面是一个示例代码: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; public class Encryption { public static void main(String[] args) throws Exception { String plainText = "Hello, world!"; String secretKey = "mysecretkey"; // Generate a secret key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey key = new SecretKeySpec(secretKey.getBytes(), "AES"); // Initialize the cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); // Encrypt the data byte[] encryptedData = cipher.doFinal(plainText.getBytes()); // Send the encrypted data to the receiver // Receiver side // Initialize the cipher cipher.init(Cipher.DECRYPT_MODE, key); // Decrypt the data byte[] decryptedData = cipher.doFinal(encryptedData); // Print the decrypted data System.out.println(new String(decryptedData)); } } ``` 注意:在实际应用,要使用更严格的加密算法和密钥管理方案来保护数据传输的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值