java加密方法

Paillier,同态加密算法Java版。

直接上链接。

http://www.csee.umbc.edu/~kunliu1/research/Paillier.html

稍微修改一下,代码如下。

[java]  view plain  copy
  1. /** 
  2. * This program is free software: you can redistribute it and/or modify it 
  3. * under the terms of the GNU General Public License as published by the Free 
  4. * Software Foundation, either version 3 of the License, or (at your option) 
  5. * any later version. 
  6. * 
  7. * This program is distributed in the hope that it will be useful, but WITHOUT 
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  9. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 
  10. * more details. 
  11. * 
  12. * You should have received a copy of the GNU General Public License along with 
  13. * this program. If not, see <http://www.gnu.org/licenses/>. 
  14. */  
  15.   
  16. import java.math.*;  
  17. import java.util.*;  
  18.   
  19. /** 
  20.  * Paillier Cryptosystem <br> 
  21.  * <br> 
  22.  * References: <br> 
  23.  * [1] Pascal Paillier, 
  24.  * "Public-Key Cryptosystems Based on Composite Degree Residuosity Classes," 
  25.  * EUROCRYPT'99. URL: 
  26.  * <a href="http://www.gemplus.com/smart/rd/publications/pdf/Pai99pai.pdf">http: 
  27.  * //www.gemplus.com/smart/rd/publications/pdf/Pai99pai.pdf</a><br> 
  28.  * 
  29.  * [2] Paillier cryptosystem from Wikipedia. URL: 
  30.  * <a href="http://en.wikipedia.org/wiki/Paillier_cryptosystem">http://en. 
  31.  * wikipedia.org/wiki/Paillier_cryptosystem</a> 
  32.  *  
  33.  * @author Kun Liu (kunliu1@cs.umbc.edu) 
  34.  * @version 1.0 
  35.  */  
  36. public class Paillier {  
  37.   
  38.     /** 
  39.      * p and q are two large primes. lambda = lcm(p-1, q-1) = 
  40.      * (p-1)*(q-1)/gcd(p-1, q-1). 
  41.      */  
  42.     private BigInteger p, q, lambda;  
  43.     /** 
  44.      * n = p*q, where p and q are two large primes. 
  45.      */  
  46.     public BigInteger n;  
  47.     /** 
  48.      * nsquare = n*n 
  49.      */  
  50.     public BigInteger nsquare;  
  51.     /** 
  52.      * a random integer in Z*_{n^2} where gcd (L(g^lambda mod n^2), n) = 1. 
  53.      */  
  54.     private BigInteger g;  
  55.     /** 
  56.      * number of bits of modulus 
  57.      */  
  58.     private int bitLength;  
  59.   
  60.     /** 
  61.      * Constructs an instance of the Paillier cryptosystem. 
  62.      *  
  63.      * @param bitLengthVal 
  64.      *            number of bits of modulus 
  65.      * @param certainty 
  66.      *            The probability that the new BigInteger represents a prime 
  67.      *            number will exceed (1 - 2^(-certainty)). The execution time of 
  68.      *            this constructor is proportional to the value of this 
  69.      *            parameter. 
  70.      */  
  71.     public Paillier(int bitLengthVal, int certainty) {  
  72.         KeyGeneration(bitLengthVal, certainty);  
  73.     }  
  74.   
  75.     /** 
  76.      * Constructs an instance of the Paillier cryptosystem with 512 bits of 
  77.      * modulus and at least 1-2^(-64) certainty of primes generation. 
  78.      */  
  79.     public Paillier() {  
  80.         KeyGeneration(51264);  
  81.     }  
  82.   
  83.     /** 
  84.      * Sets up the public key and private key. 
  85.      *  
  86.      * @param bitLengthVal 
  87.      *            number of bits of modulus. 
  88.      * @param certainty 
  89.      *            The probability that the new BigInteger represents a prime 
  90.      *            number will exceed (1 - 2^(-certainty)). The execution time of 
  91.      *            this constructor is proportional to the value of this 
  92.      *            parameter. 
  93.      */  
  94.     public void KeyGeneration(int bitLengthVal, int certainty) {  
  95.         bitLength = bitLengthVal;  
  96.         /* 
  97.          * Constructs two randomly generated positive BigIntegers that are 
  98.          * probably prime, with the specified bitLength and certainty. 
  99.          */  
  100.         p = new BigInteger(bitLength / 2, certainty, new Random());  
  101.         q = new BigInteger(bitLength / 2, certainty, new Random());  
  102.   
  103.         n = p.multiply(q);  
  104.         nsquare = n.multiply(n);  
  105.   
  106.         g = new BigInteger("2");  
  107.         lambda = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE))  
  108.                 .divide(p.subtract(BigInteger.ONE).gcd(q.subtract(BigInteger.ONE)));  
  109.         /* check whether g is good. */  
  110.         if (g.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).gcd(n).intValue() != 1) {  
  111.             System.out.println("g is not good. Choose g again.");  
  112.             System.exit(1);  
  113.         }  
  114.     }  
  115.   
  116.     /** 
  117.      * Encrypts plaintext m. ciphertext c = g^m * r^n mod n^2. This function 
  118.      * explicitly requires random input r to help with encryption. 
  119.      *  
  120.      * @param m 
  121.      *            plaintext as a BigInteger 
  122.      * @param r 
  123.      *            random plaintext to help with encryption 
  124.      * @return ciphertext as a BigInteger 
  125.      */  
  126.     public BigInteger Encryption(BigInteger m, BigInteger r) {  
  127.         return g.modPow(m, nsquare).multiply(r.modPow(n, nsquare)).mod(nsquare);  
  128.     }  
  129.   
  130.     /** 
  131.      * Encrypts plaintext m. ciphertext c = g^m * r^n mod n^2. This function 
  132.      * automatically generates random input r (to help with encryption). 
  133.      *  
  134.      * @param m 
  135.      *            plaintext as a BigInteger 
  136.      * @return ciphertext as a BigInteger 
  137.      */  
  138.     public BigInteger Encryption(BigInteger m) {  
  139.         BigInteger r = new BigInteger(bitLength, new Random());  
  140.         return g.modPow(m, nsquare).multiply(r.modPow(n, nsquare)).mod(nsquare);  
  141.   
  142.     }  
  143.   
  144.     /** 
  145.      * Decrypts ciphertext c. plaintext m = L(c^lambda mod n^2) * u mod n, where 
  146.      * u = (L(g^lambda mod n^2))^(-1) mod n. 
  147.      *  
  148.      * @param c 
  149.      *            ciphertext as a BigInteger 
  150.      * @return plaintext as a BigInteger 
  151.      */  
  152.     public BigInteger Decryption(BigInteger c) {  
  153.         BigInteger u = g.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).modInverse(n);  
  154.         return c.modPow(lambda, nsquare).subtract(BigInteger.ONE).divide(n).multiply(u).mod(n);  
  155.     }  
  156.   
  157.     /** 
  158.      * sum of (cipher) em1 and em2 
  159.      *  
  160.      * @param em1 
  161.      * @param em2 
  162.      * @return 
  163.      */  
  164.     public BigInteger cipher_add(BigInteger em1, BigInteger em2) {  
  165.         return em1.multiply(em2).mod(nsquare);  
  166.     }  
  167.   
  168.     /** 
  169.      * main function 
  170.      *  
  171.      * @param str 
  172.      *            intput string 
  173.      */  
  174.     public static void main(String[] str) {  
  175.         /* instantiating an object of Paillier cryptosystem */  
  176.         Paillier paillier = new Paillier();  
  177.         /* instantiating two plaintext msgs */  
  178.         BigInteger m1 = new BigInteger("20");  
  179.         BigInteger m2 = new BigInteger("60");  
  180.         /* encryption */  
  181.         BigInteger em1 = paillier.Encryption(m1);  
  182.         BigInteger em2 = paillier.Encryption(m2);  
  183.         /* printout encrypted text */  
  184.         System.out.println(em1);  
  185.         System.out.println(em2);  
  186.         /* printout decrypted text */  
  187.         System.out.println(paillier.Decryption(em1).toString());  
  188.         System.out.println(paillier.Decryption(em2).toString());  
  189.   
  190.         /* 
  191.          * test homomorphic properties -> D(E(m1)*E(m2) mod n^2) = (m1 + m2) mod 
  192.          * n 
  193.          */  
  194.         // m1+m2,求明文数值的和  
  195.         BigInteger sum_m1m2 = m1.add(m2).mod(paillier.n);  
  196.         System.out.println("original sum: " + sum_m1m2.toString());  
  197.         // em1+em2,求密文数值的乘  
  198.         BigInteger product_em1em2 = em1.multiply(em2).mod(paillier.nsquare);  
  199.         System.out.println("encrypted sum: " + product_em1em2.toString());  
  200.         System.out.println("decrypted sum: " + paillier.Decryption(product_em1em2).toString());  
  201.   
  202.         /* test homomorphic properties -> D(E(m1)^m2 mod n^2) = (m1*m2) mod n */  
  203.         // m1*m2,求明文数值的乘  
  204.         BigInteger prod_m1m2 = m1.multiply(m2).mod(paillier.n);  
  205.         System.out.println("original product: " + prod_m1m2.toString());  
  206.         // em1的m2次方,再mod paillier.nsquare  
  207.         BigInteger expo_em1m2 = em1.modPow(m2, paillier.nsquare);  
  208.         System.out.println("encrypted product: " + expo_em1m2.toString());  
  209.         System.out.println("decrypted product: " + paillier.Decryption(expo_em1m2).toString());  
  210.   
  211.         //sum test  
  212.         System.out.println("--------------------------------");  
  213.         Paillier p = new Paillier();  
  214.         BigInteger t1 = new BigInteger("21");System.out.println(t1.toString());  
  215.         BigInteger t2 = new BigInteger("50");System.out.println(t2.toString());  
  216.         BigInteger t3 = new BigInteger("50");System.out.println(t3.toString());  
  217.         BigInteger et1 = p.Encryption(t1);System.out.println(et1.toString());  
  218.         BigInteger et2 = p.Encryption(t2);System.out.println(et2.toString());  
  219.         BigInteger et3 = p.Encryption(t3);System.out.println(et3.toString());  
  220.         BigInteger sum = new BigInteger("1");  
  221.         sum = p.cipher_add(sum, et1);  
  222.         sum = p.cipher_add(sum, et2);  
  223.         sum = p.cipher_add(sum, et3);  
  224.         System.out.println("sum: "+sum.toString());  
  225.         System.out.println("decrypted sum: "+p.Decryption(sum).toString());  
  226.         System.out.println("--------------------------------");  
  227.     }  
  228. }  

0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值