<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>archive</title><link>https://archive-w.netlify.app/doc/advance/crypto/</link><description>Recent content on archive</description><generator>Hugo</generator><language>zh-CN</language><atom:link href="https://archive-w.netlify.app/doc/advance/crypto/index.xml" rel="self" type="application/rss+xml"/><item><title/><link>https://archive-w.netlify.app/doc/advance/crypto/aes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://archive-w.netlify.app/doc/advance/crypto/aes/</guid><description>&lt;h2 id="intro">
 Intro
 &lt;a class="anchor" href="#intro">#&lt;/a>
&lt;/h2>
&lt;h2 id="原理">
 原理
 &lt;a class="anchor" href="#%e5%8e%9f%e7%90%86">#&lt;/a>
&lt;/h2>
&lt;h2 id="实现">
 实现
 &lt;a class="anchor" href="#%e5%ae%9e%e7%8e%b0">#&lt;/a>
&lt;/h2>
&lt;div class="outer yosemite">&lt;div class="dot red">&lt;/div>&lt;div class="dot amber">&lt;/div>&lt;div class="dot green">&lt;/div>&lt;/div>
&lt;div class="code-toolbar">&lt;pre data-lang="java" data-line="" class="language-java line-numbers" style="max-height: none">&lt;code class="language-java">import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;

public class AES {

 //加密方式
 private static final String ALGORITHM = &amp;quot;AES&amp;quot;;
 //数据填充方式
 private static final String CIPHER_ALGORITHM = &amp;quot;AES/CBC/PKCS7Padding&amp;quot;;
 /**
 * 默认编码
 */
 private static final String CHARSET = &amp;quot;utf-8&amp;quot;;

 //避免重复new生成多个BouncyCastleProvider对象，因为GC回收不了，会造成内存溢出
 //只在第一次调用decrypt()方法时才new 对象
 public static boolean initialized = false;

 static { Security.addProvider(new BouncyCastleProvider());}

 public static byte[] generateKey() throws Exception {
 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
 SecureRandom secureRandom = SecureRandom.getInstance(&amp;quot;SHA1PRNG&amp;quot;, &amp;quot;SUN&amp;quot;);
 secureRandom.setSeed((CIPHER_ALGORITHM + &amp;quot;NEW_BRUSH&amp;quot;).getBytes());
 keyGenerator.init(256, secureRandom);//要生成多少位，只需要修改这里即可128, 192或256
 SecretKey originalKey = keyGenerator.generateKey();
 return originalKey.getEncoded();
 }

 public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
 AlgorithmParameters params = AlgorithmParameters.getInstance(&amp;quot;AES&amp;quot;);
 params.init(new IvParameterSpec(iv));
 return params;
 }

 /**
 * 加密
 */
 public static String encrypt(String content, byte[] key, byte[] iv) {
 try {
 byte[] originalContent = content.getBytes(CHARSET);
 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(iv));
 byte[] encrypted = cipher.doFinal(originalContent);
 return Base64.getEncoder().encodeToString(encrypted);
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }

 /**
 * AES解密
 * 填充模式AES/CBC/PKCS7Padding
 * 解密模式128
 * @return 目标密文
 */
 public static String decrypt(String content, byte[] key, byte[] iv) {
 try {
 byte[] originalContent = Base64.getDecoder().decode(content);
 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 SecretKey sKeySpec = new SecretKeySpec(key, ALGORITHM);
 cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(iv));// 初始化
 byte[] result = cipher.doFinal(originalContent);
 return new String(result,CHARSET);
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }
}
&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="reference">
 Reference
 &lt;a class="anchor" href="#reference">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>


 &lt;a href="https://github.com/12302-msb/newbrush-app/blob/main/newbrush-common/src/main/java/com/newbrush/common/utils/AesUtils.java" rel="noopener" target="_blank">https://github.com/12302-msb/newbrush-app/blob/main/newbrush-common/src/main/java/com/newbrush/common/utils/AesUtils.java&lt;/a>&lt;/li>
&lt;li>


 &lt;a href="https://github.com/12302-msb/newbrush-app/commit/02787ada6feb5e4643db196a00f1cd994868a2e1" rel="noopener" target="_blank">https://github.com/12302-msb/newbrush-app/commit/02787ada6feb5e4643db196a00f1cd994868a2e1&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title/><link>https://archive-w.netlify.app/doc/advance/crypto/des/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://archive-w.netlify.app/doc/advance/crypto/des/</guid><description>&lt;h2 id="intro">
 Intro
 &lt;a class="anchor" href="#intro">#&lt;/a>
&lt;/h2>
&lt;h2 id="原理">
 原理
 &lt;a class="anchor" href="#%e5%8e%9f%e7%90%86">#&lt;/a>
&lt;/h2>
&lt;h2 id="实现">
 实现
 &lt;a class="anchor" href="#%e5%ae%9e%e7%8e%b0">#&lt;/a>
&lt;/h2>
&lt;div class="outer yosemite">&lt;div class="dot red">&lt;/div>&lt;div class="dot amber">&lt;/div>&lt;div class="dot green">&lt;/div>&lt;/div>
&lt;div class="code-toolbar">&lt;pre data-lang="java" data-line="" class="language-java line-numbers" style="max-height: none">&lt;code class="language-java">import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Key;
import java.util.Base64;

public class DES {

 /**
 * 偏移变量，固定占8位字节
 */
 private final static String IV_PARAMETER = &amp;quot;12345678&amp;quot;;
 /**
 * 密钥算法
 */
 private static final String ALGORITHM = &amp;quot;DES&amp;quot;;
 /**
 * 加密/解密算法-工作模式-填充模式
 */
 private static final String CIPHER_ALGORITHM = &amp;quot;DES/CBC/PKCS5Padding&amp;quot;;
 /**
 * 默认编码
 */
 private static final String CHARSET = &amp;quot;utf-8&amp;quot;;

 /**
 * 生成key
 *
 * @param key
 * @return
 * @throws Exception
 */
 private static SecretKey generateKey(String key) throws Exception {
 DESKeySpec dks = new DESKeySpec(key.getBytes(CHARSET));
 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
 return keyFactory.generateSecret(dks);
 }

 public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
 AlgorithmParameters params = AlgorithmParameters.getInstance(ALGORITHM);
 params.init(new IvParameterSpec(iv));
 return params;
 }

 /**
 * DES加密字符串
 *
 * @param key 加密密码，长度不能够小于8位
 * @param data 待加密字符串
 * @return 加密后内容
 */
 public static String encrypt(String data, byte[] key, byte[] iv) {
 if (key == null || key.length &amp;lt; 8) {
 throw new RuntimeException(&amp;quot;加密失败，key不能小于8位&amp;quot;);
 }
 if (data == null)
 return null;
 try {
 SecretKey secretKey = generateKey(new String(key));
 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
 cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
 byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));
 return new String(Base64.getEncoder().encode(bytes));

 } catch (Exception e) { e.printStackTrace(); return data;}
 }

 /**
 * DES解密字符串
 *
 * @param key 解密密码，长度不能够小于8位
 * @param data 待解密字符串
 * @return 解密后内容
 */
 public static String decrypt(String data, byte[] key, byte[] iv) {
 if (key== null || key.length &amp;lt; 8) {
 throw new RuntimeException(&amp;quot;加密失败，key不能小于8位&amp;quot;);
 }
 if (data == null)
 return null;
 try {
 SecretKey secretKey = generateKey(new String(key));
 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
 cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
 return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET);
 } catch (Exception e) { e.printStackTrace(); return data;}
 }
}
&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="reference">
 Reference
 &lt;a class="anchor" href="#reference">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>


 &lt;a href="https://blog.csdn.net/gs12software/article/details/83899389" rel="noopener" target="_blank">https://blog.csdn.net/gs12software/article/details/83899389&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title/><link>https://archive-w.netlify.app/doc/advance/crypto/rsa/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://archive-w.netlify.app/doc/advance/crypto/rsa/</guid><description>&lt;ul>
&lt;li>
&lt;h2 id="introrsa">
 Intro(RSA)
 &lt;a class="anchor" href="#introrsa">#&lt;/a>
&lt;/h2>
&lt;ul>
&lt;li>
&lt;h3 id="前情提要">
 前情提要
 &lt;a class="anchor" href="#%e5%89%8d%e6%83%85%e6%8f%90%e8%a6%81">#&lt;/a>
&lt;/h3>
&lt;ul>
&lt;li>
&lt;h4 id="欧拉函数">
 欧拉函数
 &lt;a class="anchor" href="#%e6%ac%a7%e6%8b%89%e5%87%bd%e6%95%b0">#&lt;/a>
&lt;/h4>
&lt;p class="warn">


 &lt;a href="https://zh.wikipedia.org/wiki/欧拉函数" rel="noopener" target="_blank">欧拉函数&lt;/a> &lt;span class="math inline">\(\varphi(n)\)&lt;/span> 是小于等于 n 的正整数中与 n 互质的数的数目。
&lt;br>例如：&lt;span class="math inline">\(\varphi(8) = 4\)&lt;/span>，因为 1，3，5 和 7 均与 8 互质。
&lt;br>&lt;br>有如下性质：
&lt;br>&lt;code>1.&lt;/code> 欧拉函数是积性函数，即是说若 m,n 互质，则 &lt;span class="math inline">\(\varphi(m*n) = \varphi(m) * \varphi(n)\)&lt;/span>。
&lt;br>&lt;code>2.&lt;/code> 如果 n 为质数，则 &lt;span class="math inline">\(\varphi(n) = n - 1\)&lt;/span>。&lt;/p>
&lt;/li>
&lt;li>
&lt;h4 id="同余">
 同余
 &lt;a class="anchor" href="#%e5%90%8c%e4%bd%99">#&lt;/a>
&lt;/h4>
&lt;p class="warn">对某两个整数 a b，若它们除以正整数 m 所得的余数相等，则称 a b 对于模 m 同余，也就是说，存在整数 k 使得 &lt;span class="math inline">\(a - b = km\)&lt;/span>，则称 a,b 对于 m 来说是同余的，一般记作 &lt;span class="math inline">\(a \equiv b \pmod{m}\)&lt;/span>。
&lt;br>比如 &lt;span class="math inline">\(26 - 14 = 12 = 1 * 12\)&lt;/span>
&lt;br>记作 &lt;span class="math inline">\(26 \equiv 14 \pmod{12}\)&lt;/span>&lt;/p></description></item></channel></rss>