Skip to content

Instantly share code, notes, and snippets.

@i-am-mani
Created February 7, 2021 17:42
Show Gist options
  • Select an option

  • Save i-am-mani/326b2fe8afc41d38ec2a236f8fa2a008 to your computer and use it in GitHub Desktop.

Select an option

Save i-am-mani/326b2fe8afc41d38ec2a236f8fa2a008 to your computer and use it in GitHub Desktop.
/**
* Made by: Mandeep Choutapelly
* Block Chain Practical #3.1.
*/
import java.security.KeyPairGenerator
import javax.crypto.KeyGenerator
import javax.crypto.spec.SecretKeySpec
/**
* Method demonstrates the use of Symmetric key cryptography, by generating Key for any Symmetric
* Cryptography Algorithm
*
* KeyGenerator Function in java.crypto library is used for generating key.
*
* The method below also verifies the keys generated are same.
*/
fun generateSymmetricKey(algorithm: String){
val key = KeyGenerator.getInstance(algorithm)
val generateKey = key.generateKey()
val primaryKeyByteArray = generateKey.encoded
val secretKeySpec = SecretKeySpec(primaryKeyByteArray, generateKey.algorithm)
println("Algorithm Used Is = $algorithm")
println("key 1 = ${primaryKeyByteArray.decodeToString()}")
println("key 2 = ${secretKeySpec.encoded.decodeToString()}")
println(primaryKeyByteArray!!.contentEquals(secretKeySpec.encoded))
}
/**
* Method demonstrates the key generation process for Asymmetric key cryptography.
*
* KeyPairGenerator Function is used to create public/private keys based on algorithm passed.
* The method also does a trivial test whether the keys are same. Which will always be false.
*/
fun generateASymmetricKey(algorithm: String){
val keyPairGenerator = KeyPairGenerator.getInstance(algorithm)
val keyPair = keyPairGenerator.genKeyPair()
val publicKey = keyPair.public
val privateKey = keyPair.private
val encodedPublicKey = publicKey.encoded
val encodedPrivateKey = privateKey.encoded
println("Algorithm Used Is = $algorithm")
println("key 1 = ${encodedPublicKey.decodeToString()}")
println("key 2 = ${encodedPrivateKey.decodeToString()}")
println(encodedPublicKey!!.contentEquals(encodedPrivateKey))
}
/**
* Driver Program
*/
fun main() {
generateSymmetricKey("AES")
generateSymmetricKey("DES")
generateSymmetricKey("BlowFish")
generateSymmetricKey("DESede")
println("\n-----------------------------------------------\n")
generateASymmetricKey("RSA")
generateASymmetricKey("DSA")
generateASymmetricKey("DH")
}
/**
---------------:Output:-----------------------
Algorithm Used Is = AES
key 1 = th}� ��`���73~_�
key 2 = th}� ��`���73~_�
true
Algorithm Used Is = DES
key 1 = ȺąT#
key 2 = ȺąT#
true
Algorithm Used Is = BlowFish
key 1 = �w��R�%8���1�\
key 2 = �w��R�%8���1�\
true
Algorithm Used Is = DESede
key 1 = �p4������RQ7�1R,&bJX�
key 2 = �p4������RQ7�1R,&bJX�
true
-----------------------------------------------
Algorithm Used Is = RSA
 � 0�
��qhLK�o�Jc(Ŋ"MP�K�z�F�]��!*E�O���V��ȩlK��`�QҰ$-(!�g�<����9tz�D�=�ۃ� 
��qhLK�o�Jc(Ŋ"MP�K�z�F�]��!*E�O���V��ȩlK��`�QҰ$-(!�g�<����9tz�D�=�ۃ� � _�n��� ~�.\��ey�S Kk���D��l"��a�rl����DI�۪���6(��W� 8q�7y�CG=�����ǵ|������P� C�Bt��x��s��~)��� �����ߧ;˕��J� < �zJ+b������sA륽v���
k�c\�@+y�I��ɫM_F�'͓��bZ�Ø �����W+�O�э⧮ŊNK�O�[���n��ׁHmu�A�W�*�:l�<���$���Y�S�T'�￉凨��W[��� �@GE�4'�J�ߘ Α�ۚ�:��Բ��k���fX�r�/[(I�D��h��T�z�g_v�\4C��O-Xr��%'����X����b�2�Pn�I�j�V��5+Ia��V����
)"�4�)���ۃ���vw�s��� ����lP.F����UII�9�A'݊���B,DwsJ���;��;<�ւc�Y�p,F �p�@cXWa`��Bʀ�t���$���o�����P�i1�vrAI4VG�
zg��~3fO�����5s[Wn,A���w�$E�/�ݙ������{}��_�&���'�}�� �:m�)Tv��*�`���P��Ʃ%�HN���=:6���qe„����'*�Z�{�8��'t���;j�"���I9��n������7m�:O��~Y��$Y��q8Q���1$��R^�z �v�5�f�Є��w��Fcd � �
p��,^Q���X�#���q���5ڧ�=����bpz�0y�� י�nm���γ�
false
Algorithm Used Is = DSA
key 1 = 0�B0�5*�H�80�(� �y5ٹ�鿫�z�IQ��.Ş;�7��Ė>�6�CQ��39� �®S��G[�����y�uI�i\���(�/�'
key 2 = 0�\ 0�5*�H�80�(� �y5ٹ�鿫�z�IQ��.Ş;�7��Ė>�6�CQ��39� �®S��G[�����y�uI�i\���(�/�'
�0�2$�'�ڋ�K)"ٺ�����<R� ƈ���C��������<�=l�y�̻5����'%���
false
Algorithm Used Is = DH
0� � ���������ڢ!h�4��b����)�g�t ��;�"Qy�4����:C0+
m�_7O�5mmQ�E䅵vb^~��LB�7�k �\�����8k�Z�����$|K�I(fQ��[=� |��c���H6UӚi?��$�_�e]#ܣ��b�V �R���)p��mg 5NJ���t�!|2�^F.6�;�w,��'�������]�oLR��+���X9�I|�j��&��r�Z���h�������� � � L�E�A���2��LuF'kAp�'�;�il��z�N�]��Οm�/�� ������"�ܬ���R����l����P�h��� :L�/i��ңϠ�2���H���������<
�Zl
0� � ���������ڢ!h�4��b����)�g�t ��;�"Qy�4����:C0+
�@�����>���;�U�?$��u�k�R-H����\���W?
false
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment