MohammadTofi
MohammadTofi

Reputation: 385

Why always give me false in signature?

I try in this code to verify my code, I have public key , my data and signature.I am read my signature from file and convert my string to public key then get my data and verify to signature.

 public static boolean verify () {
            String publickey = "MIGfMA0GCSqGSIb3DQE";
            byte[] encKey = Base64.decodeBase64(publickey.getBytes());
            try {
                byte[] MACaddress = GetData();
                BufferedReader in = new BufferedReader(new FileReader(
                        "EndSignatuer.txt"));
                FileInputStream keyfis = new FileInputStream("EndSignatuer.txt");
                byte[] Signen = new byte[keyfis.available()];
                keyfis.read(Signen);
                keyfis.close();

                X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
                KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, pubKey);
                byte[] deSignen = Base64.decodeBase64(Signen);
                byte[] decrypted_digest = cipher.doFinal(deSignen);

                MessageDigest md5_digest = MessageDigest.getInstance("MD5");
                md5_digest.update(MACaddress);
                byte[] digest = md5_digest.digest();

                   if (decrypted_digest == digest) {
                        return true;
                    }else {
                        return false;//her why give me false 
                    }

Code encryption:

public static void GenarationKEY(byte[] data) {

        try {
            File fileEndSignatuer = new File("EndSignatuer.txt");
            FileOutputStream fopEndSignatuer = new FileOutputStream(
                    fileEndSignatuer);
            // /Read private key from file
            FileInputStream keyfis = new FileInputStream("PiveteKey.txt");
            byte[] PrivateKeyB = new byte[keyfis.available()];
            keyfis.read(PrivateKeyB);
            keyfis.close();
            byte[] decodePrivetekey = Base64.decodeBase64(PrivateKeyB);
            // /get private key
            PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(
                    decodePrivetekey);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PrivateKey privKey = keyFactory.generatePrivate(pubKeySpec);
            // / make hash
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, privKey);
            // /make encoding
            MessageDigest md5_digest = MessageDigest.getInstance("MD5");
            byte[] digest = md5_digest.digest(data);
            byte[] cipherText = cipher.doFinal(digest);
            byte[] degnatureencode = Base64.encodeBase64(cipherText);
            fopEndSignatuer.write(degnatureencode);
            fopEndSignatuer.flush();
            fopEndSignatuer.close();
} 

Upvotes: 0

Views: 139

Answers (1)

divanov
divanov

Reputation: 6339

First of all, what you do is not quite RSA digital signature. Use Signature class instead of combination Cipher and MessageDigest. Or if you insist to do it on low level, then consult specification, particularly section 9.2.

Secondly, string MIGfMA0GCSqGSIb3DQE doesn't represent RSA public key neither it is proper Base64-encoded data.

Also you want to use Arrays.equals(byte[], byte[]) instead of equality operator, as the latter just ensures that array object is the same, while the former compares actual content of the arrays.

Upvotes: 1

Related Questions