def __init__(self, bArray, bOffset, bLength): self.rsa_KeyPair = KeyPair( KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024 ) self.rsa_KeyPair.genKeyPair(); self.rsa_PublicKey = self.rsa_KeyPair.getPublic(); self.rsa_PrivateKey = self.rsa_KeyPair.getPrivate(); self.cipherRSA = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, False); self.register(bArray, bOffset + 1, bArray[bOffset]);
def testInit(self): c = Cipher.getInstance(Cipher.ALG_RSA_NOPAD, False) self.assertEquals(Cipher.ALG_RSA_NOPAD, c.getAlgorithm()) try: c.update([], 0, 0, [], 0) self.fail() except CryptoException, ce: self.assertEquals(CryptoException.INVALID_INIT, ce.getReason())
def testDES(self): KeyArray = [1,2,3,4,5,6,7,8] bytBuffer = [0 for i in xrange(8)] MyBuffer = [7,5,6,8] MyDesKey = KeyBuilder.buildKey(KeyBuilder.TYPE_DES, KeyBuilder.LENGTH_DES, False) crypt_des = Cipher.getInstance(Cipher.ALG_DES_ECB_PKCS5, False) MyDesKey.setKey(KeyArray, 0); crypt_des.init(MyDesKey, Cipher.MODE_ENCRYPT); length = crypt_des.doFinal(MyBuffer, 0, len(MyBuffer), bytBuffer, 0) crypt_des.init(MyDesKey, Cipher.MODE_DECRYPT); crypt_des.doFinal(bytBuffer, 0, length, MyBuffer, 0) self.assertEquals([7,5,6,8], MyBuffer)
def testRSASignVerify(self): kp = KeyPair(KeyPair.ALG_RSA, KeyBuilder.LENGTH_RSA_1024) kp.genKeyPair() pubk = kp.getPublic() self.assertEquals(1024, pubk.getSize()) privk = kp.getPrivate() self.assertEquals(1024, privk.getSize()) c = Cipher.getInstance(Cipher.ALG_RSA_PKCS1, False) c.init(privk, Cipher.MODE_ENCRYPT) res = [0]*1024 l = c.doFinal([0,1,2,3,4,5], 0, 6, res, 0) c.init(pubk, Cipher.MODE_DECRYPT) res2 = [0]*1024 l = c.doFinal(res, 0, l, res2, 0) self.assertEquals([0,1,2,3,4,5], res2[:l])