当前位置: 首页 > news >正文

门户网站建设厂商名录阿里云做的网站

门户网站建设厂商名录,阿里云做的网站,西安商城网站开发制作,常德网站建设渠道目录 对称加密算法 AES (ECB模式) AES(CBC 模式)。 非对称加密 对称加密算法 对称加密算法,是使用相同的密钥进行加密和解密。使用对称加密算法来加密双方的通信的话,双方需要先约定一个密钥,加密方才能加密&#…

目录

对称加密算法

AES (ECB模式)

AES(CBC 模式)。

非对称加密


对称加密算法

对称加密算法,是使用相同的密钥进行加密和解密。使用对称加密算法来加密双方的通信的话,双方需要先约定一个密钥,加密方才能加密,接收方才能 解密。常用的加密算法,有 DES、3DES 和 AES,国密算法包括SM1,SM4和SM7。 目前,使用 DES 来加密数据非常不安全。因此,在业务代码中要避免使用 DES 加密。而 3DES 算法,是使用不同的密钥进行三次 DES 串联调用,虽然解决 了 DES 不够安全的问题,但是比 AES 慢,也不太推荐。我们来看看AES的算法,AES 算法有ECB、CBC、 CFB、OFB、CTR 模式

AES (ECB模式)

private static final String KEY = "secretkey1234567"; //密钥//测试ECB模式@GetMapping("ecb")public void ecb() throws Exception {Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");test(cipher, null);}//获取加密秘钥帮助方法private static SecretKeySpec setKey(String secret) {return new SecretKeySpec(secret.getBytes(), "AES");}//测试逻辑private static void test(Cipher cipher, AlgorithmParameterSpec parameterSpec) throws Exception {//初始化Ciphercipher.init(Cipher.ENCRYPT_MODE, setKey(KEY), parameterSpec);//加密测试文本System.out.println("一次:" + Hex.encodeHexString(cipher.doFinal("abcdefghijklmnop".getBytes())));//加密重复一次的测试文本System.out.println("两次:" + Hex.encodeHexString(cipher.doFinal("abcdefghijklmnopabcdefghijklmnop".getBytes())));//下面测试是否可以通过操纵密文来操纵明文 //发送方账号byte[] sender = "1000000000012345".getBytes();//接收方账号byte[] receiver = "1000000000034567".getBytes();//转账金额byte[] money = "0000000010000000".getBytes();//加密发送方账号System.out.println("发送方账号:" + Hex.encodeHexString(cipher.doFinal(sender)));//加密接收方账号System.out.println("接收方账号:" + Hex.encodeHexString(cipher.doFinal(receiver)));//加密金额System.out.println("金额:" + Hex.encodeHexString(cipher.doFinal(money)));//加密完整的转账信息byte[] result = cipher.doFinal(ByteUtils.concatAll(sender, receiver, money));System.out.println("完整数据:" + Hex.encodeHexString(result));//用于操纵密文的临时字节数组byte[] hack = new byte[result.length];//把密文前两段交换System.arraycopy(result, 16, hack, 0, 16);System.arraycopy(result, 0, hack, 16, 16);System.arraycopy(result, 32, hack, 32, 16);cipher.init(Cipher.DECRYPT_MODE, setKey(KEY), parameterSpec);分区 业务常见问题 的第 16 页  cipher.init(Cipher.DECRYPT_MODE, setKey(KEY), parameterSpec);//尝试解密System.out.println("原始明文:" + new String(ByteUtils.concatAll(sender, receiver, money)));System.out.println("操纵密文:" + new String(cipher.doFinal(hack)));}

 两个相同明文分组产生的密文,就是两个相同的密文分组叠在一起。在不知道密钥的情况下,我们操纵密文实现了对明文数据的修改,对调了发送方账号 和接收方账号。所以说,ECB 模式虽然简单,但是不安全,不推荐使用。

AES(CBC 模式)。

private static final String initVector = "abcdefghijklmnop"; //初始化向量
@GetMapping("cbc")
public void cbc() throws Exception {Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));test(cipher, iv);
}

可以看到,相同的明文字符串复制一遍得到的密文并不是重复两个密文分组,并且调换密文分组的顺序无法操纵明文。 不要在代码中写死一个固定的密钥和初始化向量,最好和之前提到的盐一样,是唯一、独立并且每次都变化的。推荐使用独立的加密服务来管控密钥、做 加密操作,千万不要把密钥和密文存在一个数据库,加密服务需要设置非常高的管控标准。数据库中不能保存明文的敏感信息,但可以保存脱敏的信息。 普通查询的时候,直接查脱敏信息即可。下面举个例子:

@Data
@Entity
public class UserData {@Idprivate Long id;private String idcard;//脱敏的身份证private Long idcardCipherId;//身份证加密IDprivate String idcardCipherText;//身份证密文private String name;//脱敏的姓名private Long nameCipherId;//姓名加密IDprivate String nameCipherText;//姓名密文
}
@Data
@Entity
public class CipherData {@Id@GeneratedValue(strategy = AUTO)private Long id;private String iv;//初始化向量private String secureKey;//密钥
}

 加密服务使用 GCM 模式( Galois/Counter Mode)的 AES-256 对称加密算法,也就是 AES-256-GCM,这是一种AEAD(Authenticated Encryption with Associated Data)认证加密算法,除了能实现普通加密算法提供的保密性之外,还能实现可认证性和密文完整性,是目前最推荐的 AES 模式。使用类似 GCM 的 AEAD 算法进行加解密,除了需要提供初始化向量和密钥之外,还可以提供一个 AAD(附加认证数据,additional authenticated data),用于验证未 包含在明文中的附加信息,解密时不使用加密时的 AAD 将解密失败。其实,GCM 模式的内部使用的就是 CTR 模式,只不过还使用了 GMAC 签名算法,对 密文进行签名实现完整性校验。

我们实现基于 AES-256-GCM 的加密服务,包含下面的主要逻辑:加密时允许外部传入一个 AAD 用于认证,加密服务每次都会使用新生成的随机值作为密 钥和初始化向量。在加密后,加密服务密钥和初始化向量保存到数据库中,返回加密 ID 作为本次加密的标识。应用解密时,需要提供加密 ID、密文和加 密时的 AAD 来解密。加密服务使用加密 ID,从数据库查询出密钥和初始化向量。


@Service
public class CipherService {//密钥长度public static final int AES_KEY_SIZE = 256;//初始化向量长度public static final int GCM_IV_LENGTH = 12;//GCM身份认证Tag长度public static final int GCM_TAG_LENGTH = 16;@Autowiredprivate CipherRepository cipherRepository;//内部加密方法public static byte[] doEncrypt(byte[] plaintext, SecretKey key, byte[] iv, byte[] aad) throws Exception {//加密算法Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");//Key规范SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");//GCM参数规范GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);//加密模式cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);//设置aadif (aad != null)cipher.updateAAD(aad);//加密byte[] cipherText = cipher.doFinal(plaintext);return cipherText;}//内部解密方法public static String doDecrypt(byte[] cipherText, SecretKey key, byte[] iv, byte[] aad) throws Exception {//加密算法Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");//Key规范SecretKeySpec keySpec = new SecretKeySpec(key.getEncoded(), "AES");//GCM参数规范GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, iv);//解密模式cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);//设置aadif (aad != null)cipher.updateAAD(aad);//解密byte[] decryptedText = cipher.doFinal(cipherText);return new String(decryptedText);}//加密入口public CipherResult encrypt(String data, String aad) throws Exception {//加密结果CipherResult encryptResult = new CipherResult();//密钥生成器KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");//生成密钥keyGenerator.init(AES_KEY_SIZE);SecretKey key = keyGenerator.generateKey();//IV数据byte[] iv = new byte[GCM_IV_LENGTH];//随机生成IVSecureRandom random = new SecureRandom();random.nextBytes(iv);//处理aadbyte[] aaddata = null;if (!StringUtils.isEmpty(aad))aaddata = aad.getBytes();aaddata = aad.getBytes();//获得密文encryptResult.setCipherText(Base64.getEncoder().encodeToString(doEncrypt(data.getBytes(), key, iv, aaddata)));//加密上下文数据CipherData cipherData = new CipherData();//保存IVcipherData.setIv(Base64.getEncoder().encodeToString(iv));//保存密钥cipherData.setSecureKey(Base64.getEncoder().encodeToString(key.getEncoded()));cipherRepository.save(cipherData);//返回本地加密IDencryptResult.setId(cipherData.getId());return encryptResult;}//解密入口public String decrypt(long cipherId, String cipherText, String aad) throws Exception {//使用加密ID找到加密上下文数据CipherData cipherData = cipherRepository.findById(cipherId).orElseThrow(() -> new IllegalArgumentException("invlaid cipherId"));//加载密钥byte[] decodedKey = Base64.getDecoder().decode(cipherData.getSecureKey());//初始化密钥SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");//加载IVbyte[] decodedIv = Base64.getDecoder().decode(cipherData.getIv());//处理aadbyte[] aaddata = null;if (!StringUtils.isEmpty(aad))aaddata = aad.getBytes();//解密return doDecrypt(Base64.getDecoder().decode(cipherText.getBytes()), originalKey, decodedIv, aaddata);}
}

我们可以让用户选择,如果需要保护二要素的话,就自己输入一个查询密码作为 AAD。系统需要读取用户敏感信息的时候,还需要用户提供这个密码,否 则无法解密。这样一来,即使黑客拿到了用户数据库的密文、加密服务的密钥和 IV,也会因为缺少 AAD 无法解密。

    @Autowiredprivate CipherService cipherService;//加密@GetMapping("right")public UserData right(@RequestParam(value = "name", defaultValue = "test") String name,@RequestParam(value = "idcard", defaultValue = "300000000000001234") String idCard,@RequestParam(value = "aad", required = false)String aad) throws Exception {UserData userData = new UserData();userData.setId(1L);//脱敏姓名userData.setName(chineseName(name));//脱敏身份证userData.setIdcard(idCard(idCard));//加密姓名CipherResult cipherResultName = cipherService.encrypt(name,aad);userData.setNameCipherId(cipherResultName.getId());userData.setNameCipherText(cipherResultName.getCipherText());//加密身份证CipherResult cipherResultIdCard = cipherService.encrypt(idCard,aad);userData.setIdcardCipherId(cipherResultIdCard.getId());userData.setIdcardCipherText(cipherResultIdCard.getCipherText());return userRepository.save(userData);}//解密@GetMapping("read")public void read(@RequestParam(value = "aad", required = false)String aad) throws Exception {//查询用户信息UserData userData = userRepository.findById(1L).get();//使用AAD来解密姓名和身份证log.info("name : {} idcard : {}",log.info("name : {} idcard : {}",cipherService.decrypt(userData.getNameCipherId(), userData.getNameCipherText(),aad),cipherService.decrypt(userData.getIdcardCipherId(), userData.getIdcardCipherText(),aad));}//脱敏身份证private static String idCard(String idCard) {String num = StringUtils.right(idCard, 4);return StringUtils.leftPad(num, StringUtils.length(idCard), "*");}//脱敏姓名public static String chineseName(String chineseName) {String name = StringUtils.left(chineseName, 1);return StringUtils.rightPad(name, StringUtils.length(chineseName), "*");

{"id":1,"name":"朱*","idcard":"************** 1234","idcardCipherId":26346,"idcardCipherText":"t/wIh1XTj00wJP1Lt3aGzSvn9GcqQWEwthN58KKU4KZ4Tw==","nameCipherId":26347,"name CipherText":"+gHrk1 mWmveBMVUo+CYon8Zjj9QAtw=="} [21:46:00.079] [http-nio-45678-exec-6] [INFO ] [o.g.t.c.s.s.StoreIdCardController:102 ] - name : test idcard : 300000000000001234

错误的aad会抛出异常 javax.crypto.AEADBadTagException: Tag mismatch! at com.sun.crypto.provider.GaloisCounterMode.decryptFinal(GaloisCounterMode.java:578) at com.sun.crypto.provider.CipherCore.finalNoPadding(CipherCore.java:1116) at com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:1053) at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:853) at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446) at javax.crypto.Cipher.doFinal(Cipher.java:2164)

非对称加密

公钥密码算法。公钥密码是由一对密钥对构成的,使用公钥或者说加密密钥来加密,使用私钥或者说解密密钥来解密,公钥可以任意公开,私钥不能公 开。使用非对称加密的话,通信双方可以仅分享公钥用于加密,加密后的数据没有私钥无法解密,国密算法包括SM2,SM9。

http://www.yayakq.cn/news/914748/

相关文章:

  • 西安营销型网站湛江做网站设计
  • 想建设个人网站去那里建设吴桥网站建设公司
  • 域名购买后如何建设网站网页版设计
  • 丽水北京网站建设台州网站建设外包
  • 网站域名注册规则百度网站地址提交
  • 用vs做网站的教程国外无版权素材网站
  • 导视设计网站wordpress文章加密
  • 怎么做电影流量网站吗北京公司摇号中签率
  • 怎么在网站添加关键词旅行社网站开发 论文
  • 网站首页流程图国外企业网址
  • 建好了网站怎么做外贸专业的官网设计公司
  • 响应式旅游网站模板登封郑州网站建设
  • 前端旅游网站行程怎么做wordpress有广告
  • 济宁亿峰科技做网站一年多少费用惠州自动seo
  • 长沙专业外贸网站建设商标 做网站 是几类
  • 网站关键词重复网络建设的流程网站
  • 三合一网站模板深圳平面设计培训
  • 网站链接收费怎么做的腾讯云域名查询
  • 不同网站建设报价单创建一个新的公司网站
  • 用react做的网站移动应用开发技术学什么
  • 信用网站建设意见抖音代运营直播
  • 中国古建筑网站html5视频网站模板
  • 网站设计网络推广优化企业注册信息
  • 手机网站排名定制网站制作公司怎么样
  • 哪个网站建设服务器是在国外的广告联盟平台入口
  • 邢台做网站公司用脚手架如何搭建项目做网站
  • 聊城制作手机网站桂林生活网新闻
  • wordpress 托管主机沈阳关键词优化费用
  • 山东省建设科技协会网站房地产政策
  • 南通市网站建设我的完黑龙江住房城乡建设厅网站