A digital signature is a cryptographic mechanism used to verify the authenticity and integrity of a message or document. It provides a way to ensure that a message has not been altered and that it comes from the expected sender.
Java provides the Java Cryptography Architecture (JCA) to create digital signatures using algorithms such as RSA, DSA, and ECDSA.
Below is an example of how to create and verify a digital signature using the RSA algorithm in Java.
Steps Involved:
- Generate a key pair (public and private keys).
- Create a digital signature using the private key.
- Verify the digital signature using the public key.
১. Digital Signature Creation (Signing a Message)
This code demonstrates how to create a digital signature using the private key.
import java.security.*;
import java.util.Base64;
public class DigitalSignatureExample {
public static void main(String[] args) {
try {
// Generate RSA Key Pair (Private and Public Keys)
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048); // 2048-bit key size
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// Message to be signed
String message = "This is a confidential message";
// Create a Signature object
Signature signature = Signature.getInstance("SHA256withRSA");
// Initialize the Signature object with the private key
signature.initSign(privateKey);
// Update the Signature object with the message data
signature.update(message.getBytes());
// Generate the digital signature
byte[] digitalSignature = signature.sign();
// Print the digital signature (Base64 encoded for readability)
String encodedSignature = Base64.getEncoder().encodeToString(digitalSignature);
System.out.println("Digital Signature (Base64 encoded): " + encodedSignature);
// Now verify the signature using the public key
boolean isVerified = verifySignature(message, encodedSignature, publicKey);
System.out.println("Signature Verified: " + isVerified);
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to verify the digital signature using the public key
public static boolean verifySignature(String message, String encodedSignature, PublicKey publicKey) {
try {
// Decode the Base64 encoded signature
byte[] signatureBytes = Base64.getDecoder().decode(encodedSignature);
// Create a Signature object for verification
Signature signature = Signature.getInstance("SHA256withRSA");
// Initialize the Signature object with the public key
signature.initVerify(publicKey);
// Update the Signature object with the message data
signature.update(message.getBytes());
// Verify the signature
return signature.verify(signatureBytes);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
ব্যাখ্যা:
- Key Pair Generation:
KeyPairGeneratorক্লাস ব্যবহার করে RSA কীগুলি তৈরি করা হয় (2048-বিট)। এটি একটি PrivateKey এবং একটি PublicKey তৈরি করে।
- Digital Signature Creation:
Signature.getInstance("SHA256withRSA")ব্যবহার করে আমরা SHA-256 হ্যাশিং এলগরিদমের মাধ্যমে RSA ডিজিটাল সিগনেচার তৈরি করি।- প্রথমে private key দিয়ে সিগনেচার সাইন করা হয়।
- এরপর সিগনেচার তৈরি করার জন্য
signature.sign()পদ্ধতি ব্যবহার করা হয়।
- Digital Signature Verification:
- সিগনেচার যাচাই করতে public key ব্যবহার করা হয়।
signature.verify(signatureBytes)পদ্ধতি দিয়ে যাচাই করা হয় যে, সিগনেচার সঠিক এবং মূল বার্তা (message) সঙ্গে সঙ্গতিপূর্ণ।
- Base64 Encoding:
- ডিজিটাল সিগনেচারটি বাইনারি ফর্মে থাকে, তাই এটি মানবপঠনযোগ্য করার জন্য Base64 encoding করা হয়।
আউটপুট:
Digital Signature (Base64 encoded): <Base64_Encoded_Signature>
Signature Verified: true
২. KeyPair Generation (KeyPair)
- Private Key: এটি সিগনেচার সাইন করার জন্য ব্যবহৃত হয়। এটি গোপন রাখা উচিত।
- Public Key: এটি সিগনেচার যাচাই করার জন্য ব্যবহৃত হয় এবং এটি অন্যদের সাথে শেয়ার করা যায়।
৩. Digital Signature Verification
- ডিজিটাল সিগনেচার যাচাই করার জন্য public key ব্যবহার করা হয়। যদি সিগনেচার সঠিক হয় এবং বার্তা পরিবর্তন না হয়ে থাকে, তবে এটি true ফেরত দেয়।
এই উদাহরণটি দেখিয়েছে কিভাবে আপনি জাভাতে ডিজিটাল সিগনেচার তৈরি এবং যাচাই করতে পারেন। এটি একটি নিরাপদ পদ্ধতি যা বার্তার অখণ্ডতা এবং প্রমাণীকরণের জন্য ব্যবহৃত হয়।
Read more