Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97871430
PaillierScheme.java
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Jan 7, 00:36
Size
5 KB
Mime Type
text/x-java
Expires
Thu, Jan 9, 00:36 (2 d)
Engine
blob
Format
Raw Data
Handle
23424598
Attached To
R3229 Genome Privacy - SHCS App
PaillierScheme.java
View Options
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package
crypto.paillier
;
import
java.math.BigInteger
;
import
java.util.Random
;
import
crypto.CryptoException
;
/**
*
* @author raisaro
*/
public
class
PaillierScheme
{
private
boolean
canEncrypt
;
// Whether encryption can take place.
private
boolean
canDecrypt
;
// Whether decryption can take place.
private
BigInteger
x
;
// Private Key a random integer € [1,n^2/2]
private
BigInteger
g
;
// Encryption base.
private
BigInteger
h
;
// Encryption base.
private
BigInteger
n
;
// Public key.
private
BigInteger
nsqr
;
// The value n^2 (n squared).
private
Random
prng
;
// Random number generator.
public
PaillierScheme
(
BigInteger
N
,
BigInteger
G
,
BigInteger
H
,
BigInteger
X
){
canEncrypt
=
true
;
canDecrypt
=
true
;
n
=
N
;
g
=
G
;
h
=
H
;
x
=
X
;
nsqr
=
n
.
multiply
(
n
);
prng
=
new
Random
(
System
.
currentTimeMillis
()
);
}
public
PaillierScheme
(
BigInteger
N
,
BigInteger
G
,
BigInteger
H
){
canEncrypt
=
true
;
canDecrypt
=
false
;
n
=
N
;
g
=
G
;
h
=
H
;
nsqr
=
n
.
multiply
(
n
);
prng
=
new
Random
(
System
.
currentTimeMillis
()
);
}
/**
* Encrypts the given plaintext to produce ciphertext.
*
* @param plain
*
* @param gPOWr
*
* @param hPOWr
*
* @return The encrypted value of the plaintext, modulo n^2.
*
* @throws CryptoException CryptoException If the encryption cannot be performed or
* there is an error while encrypting
* the plaintext.
*/
public
BigInteger
[]
encrypt
(
BigInteger
plain
,
BigInteger
gPOWr
,
BigInteger
hPOWr
)
throws
CryptoException
{
BigInteger
[]
cipher
=
new
BigInteger
[
2
];
BigInteger
cipherA
;
// The ciphertext A.
BigInteger
cipherB
;
// The ciphertext B.
cipherA
=
gPOWr
;
cipherB
=
hPOWr
.
multiply
(((
plain
.
multiply
(
n
).
mod
(
nsqr
)).
add
(
BigInteger
.
ONE
)).
mod
(
nsqr
)).
mod
(
nsqr
);
cipher
[
0
]
=
cipherA
;
cipher
[
1
]
=
cipherB
;
return
cipher
;
}
/**
* Encrypts the given plaintext to produce ciphertext.
*
* @param plain The plaintext.
*
* @return The encrypted value of the plaintext, modulo n^2.
*
* @exception CryptoException If the encryption cannot be performed or
* there is an error while encrypting
* the plaintext.
*/
public
BigInteger
[]
encrypt
(
BigInteger
plain
)
throws
CryptoException
{
BigInteger
[]
cipher
=
new
BigInteger
[
2
];
BigInteger
cipherA
;
// The ciphertext A.
BigInteger
cipherB
;
// The ciphertext B.
long
rand
;
// Value generated by PRNG.
BigInteger
r
;
// Random value used to hide plaintext.
if
(
!
canEncrypt
)
{
throw
new
CryptoException
(
"Cannot encrypt value "
+
plain
+
" because no public key was defined."
);
}
do
{
r
=
new
BigInteger
(
1024
,
new
Random
());
r
=
r
.
mod
(
n
.
divide
(
BigInteger
.
valueOf
(
4
)));
r
=
r
.
add
(
BigInteger
.
ONE
);
cipherA
=
g
.
modPow
(
r
,
nsqr
);
}
while
((
crypto
.
Utils
.
gcd
(
cipherA
.
longValue
(),
nsqr
.
longValue
())
!=
1
)||(
r
.
equals
(
BigInteger
.
ZERO
)));
cipherB
=
(
h
.
modPow
(
r
,
nsqr
)).
multiply
(((
plain
.
multiply
(
n
).
mod
(
nsqr
)).
add
(
BigInteger
.
ONE
)).
mod
(
nsqr
)).
mod
(
nsqr
);
// System.out.println("A = "+cipherA);
// System.out.println("B = "+cipherB);
cipher
[
0
]
=
cipherA
;
cipher
[
1
]
=
cipherB
;
return
cipher
;
}
/**
* Decrypts the given ciphertext, which was previously encrypted using
* the public key given to this instance of the class. This method will
* throw an exception if no decryption keys were defined.
*
* @param cipher The ciphertext.
*
* @return The decrypted value of the plaintext, modulo n.
*
* @exception CryptoException If the decryption fails.
*/
public
BigInteger
decrypt
(
BigInteger
[]
cipher
)
throws
CryptoException
{
BigInteger
plain
;
// The plaintext.
BigInteger
cipherA
=
cipher
[
0
];
BigInteger
cipherB
=
cipher
[
1
];
BigInteger
Ainv
;
// Intermediate result during decryption.
if
(
!
canDecrypt
)
{
throw
new
CryptoException
(
"Cannot decrypt value "
+
cipherB
.
longValue
()
+
" because no private key was defined."
);
}
Ainv
=
(
cipherA
.
modPow
(
x
,
nsqr
)).
modInverse
(
nsqr
);
plain
=
(((
cipherB
.
multiply
(
Ainv
).
mod
(
nsqr
)).
subtract
(
BigInteger
.
ONE
)).
mod
(
nsqr
)).
divide
(
n
);
return
plain
;
}
/**
* Partially Decrypts the given ciphertext, which was previously encrypted using
* the public key given to this instance of the class. This method will
* throw an exception if no decryption keys were defined. The private key used
* must be part of the original private key
* @param cipher
* @param x1
* @return The modified cipher text which can be finally decrypted by the other
* part of the original private key
* @throws CryptoException
*/
public
BigInteger
[]
proxyDecription
(
BigInteger
[]
cipher
)
throws
CryptoException
{
BigInteger
cipherA
=
cipher
[
0
];
BigInteger
cipherB
=
cipher
[
1
];
BigInteger
[]
new_cipher
=
new
BigInteger
[
2
];
BigInteger
new_cipherB
;
BigInteger
Ainv
;
if
(
!
canDecrypt
)
{
throw
new
CryptoException
(
"Cannot decrypt value "
+
cipherB
.
longValue
()
+
" because no private key was defined."
);
}
Ainv
=
(
cipherA
.
modPow
(
x
,
nsqr
)).
modInverse
(
nsqr
);
new_cipherB
=
(
cipherB
.
multiply
(
Ainv
)).
mod
(
nsqr
);
new_cipher
[
0
]
=
cipherA
;
new_cipher
[
1
]
=
new_cipherB
;
return
new_cipher
;
}
}
Event Timeline
Log In to Comment