guland 发表于 2010-1-26 09:27:24

关于RSA加密解密的问题

本帖最后由 guland 于 2010-2-1 12:01 编辑

RSA加解密如何实现,谁知道过来讨论讨论

hzxymkb 发表于 2010-1-26 10:59:42

本帖最后由 hzxymkb 于 2010-1-26 11:18 编辑

我帮不上忙!帮楼主顶起来了!

cheng4ever 发表于 2010-1-26 15:07:15

帮顶   现在用bes64加密

rolaka 发表于 2010-1-26 22:29:43

:face (31):autoit不适合大数计算...肯定悲剧

算法有人写过0 0 官方论坛有...

netegg 发表于 2010-1-28 00:58:50

两个都有函数,大数计算和rsa解密

guland 发表于 2010-1-29 13:44:50

我没找到啊

netegg 发表于 2010-1-30 03:17:56

本帖最后由 netegg 于 2010-1-30 04:23 编辑

rsa参考以下地址
http://www.autoitscript.com/forum/index.php?showtopic=104025&st=0&p=736370&hl=rsa&fromsearch=1&#entry736370

guland 发表于 2010-2-1 09:54:46

我E文不好,好像是调用外部文件实现的RSA?

netegg 发表于 2010-2-1 10:27:47

本帖最后由 netegg 于 2010-2-1 10:31 编辑

回复 8# guland

RSA
Global $KEY
Global $P
Global $Q
Global $PHI

#Region GUI
$GUI = GUICreate("RSA Algorithm",320,320,-1,-1,0x16C80000,0x00000181)
$P_LABEL = GUICtrlCreateLabel("(P)",5,5,100,20,BitOR(0x01,0x1000))
$P_INPUT = GUICtrlCreateInput("",5,30,100,20,0x01)
$Q_LABEL = GUICtrlCreateLabel("(Q)",110,5,100,20,BitOR(0x01,0x1000))
$Q_INPUT = GUICtrlCreateInput("",110,30,100,20,0x01)
$PHI_LABEL = GUICtrlCreateLabel("(PHI)",215,5,100,20,BitOR(0x01,0x1000))
$PHI_INP = GUICtrlCreateInput("",215,30,100,20,0x01)
$E_LABEL = GUICtrlCreateLabel("(E)",5,55,100,20,BitOR(0x01,0x1000))
$E_INP = GUICtrlCreateInput("",5,80,100,20,0x01)
$N_LABEL = GUICtrlCreateLabel("(N)",110,55,100,20,BitOR(0x01,0x1000))
$N_INP = GUICtrlCreateInput("",110,80,100,20,0x01)
$D_LABEL = GUICtrlCreateLabel("(D)",215,55,100,20,BitOR(0x01,0x1000))
$D_INP = GUICtrlCreateInput("",215,80,100,20,0x01)
$TEXT_BOX = GUICtrlCreateEdit("",5,105,310,180,0x00200000)
$GEN_KEY = GUICtrlCreateButton("Genereaza Chei",5,290,100,25)
$ENC_BUTTON = GUICtrlCreateButton("ENCRYPT",110,290,100,25)
$DEC_BUTTON = GUICtrlCreateButton("DECRYPT",215,290,100,25)
GUISetState(@SW_SHOW,$GUI)
#EndRegion

#Region Main Loop
While True
    $MSG = GUIGetMsg()
    Switch $MSG
      Case $GEN_KEY
            GenerateKey()
            GUICtrlSetData($P_INPUT,$P)
            GUICtrlSetData($Q_INPUT,$Q)
            GUICtrlSetData($PHI_INP,$PHI)
            GUICtrlSetData($E_INP,$KEY)
            GUICtrlSetData($N_INP,$KEY)
            GUICtrlSetData($D_INP,$KEY)
      Case $ENC_BUTTON
            $TEXT = GUICtrlRead($TEXT_BOX)
            $DATA = Encrypt($TEXT,$KEY,$KEY)
            GUICtrlSetData($TEXT_BOX,$DATA)
      Case $DEC_BUTTON
            $TEXT = GUICtrlRead($TEXT_BOX)
            $DATA = Decrypt($TEXT,$KEY,$KEY)
            GUICtrlSetData($TEXT_BOX,$DATA)
      Case -3
            Exit
    EndSwitch
    Sleep(20)
WEnd
#EndRegion

Func GenerateKey()
    Local Const $UP_LIMIT = 9999
    Local Const $LW_LIMIT = 3170
    Local Const $KEY_LIMIT = 10000000
    Do
      Do
            $P = Int(($UP_LIMIT - $LW_LIMIT + 1) * Random() + $LW_LIMIT)
            $Q = Int(($UP_LIMIT - $LW_LIMIT + 1) * Random() + $LW_LIMIT)
      Until IsPrime($P) And IsPrime($Q)
      $N = $P * $Q
      $PHI = ($P - 1) * ($Q - 1)
      $E = CMMDC($PHI)
      $D = GenerateEuler($E,$PHI)
    Until $D > $KEY_LIMIT
    $KEY = $E
    $KEY = $D
    $KEY = $N
    Return $KEY
EndFunc

Func IsPrime($NUM)
    $SQRT = Sqrt($NUM)
    If $NUM < 2 Then
      $IS_PRIME = False
      Return $IS_PRIME
    EndIf
    $COUNT = 2
    $IS_PRIME = True
    If Mod($NUM,$COUNT) = 0 Then
      $IS_PRIME = False
      Return $IS_PRIME
    EndIf
    $COUNT = 3
    For $X = $COUNT To $SQRT Step 2
      If Mod($NUM,$X) = 0 Then
            $IS_PRIME = False
            Return $IS_PRIME
      EndIf
    Next
    Return $IS_PRIME
EndFunc

Func CMMDC($PHI_NUM)
    Local Const $UP_LIMIT = 99999999
    Local Const $LW_LIMIT = 10000000
    Local $E = Int(($UP_LIMIT - $LW_LIMIT + 1) * Random() + $LW_LIMIT)
    While True
      $X = Mod($PHI_NUM,$E)
      $Y = Mod($X,$E)
      If $Y <> 0 And IsPrime($E) Then
            $CMMDC = $E
            Return $CMMDC
      Else
            $E += 1
      EndIf
    WEnd
EndFunc

Func GenerateEuler($E_NUM,$PHI_NUM)
    Local $U1 = 1
    Local $U2 = 0
    Local $U3 = $PHI_NUM
    Local $V1 = 0
    Local $V2 = 1
    Local $V3 = $E_NUM
    Do
      $QU = Int($U3/$V3)
      $T1 = $U1 - $QU * $V1
      $T2 = $U2 - $QU * $V2
      $T3 = $U3 - $QU * $V3
      $U1 = $V1
      $U2 = $V2
      $U3 = $V3
      $V1 = $T1
      $V2 = $T2
      $V3 = $T3
      $Z = 1
    Until $V3 = 0
    $UU = $U1
    $VV = $U2
    If ($VV < 0) Then
      $INVERSE = $VV + $PHI_NUM
    Else
      $INVERSE = $VV
    EndIf
    Return $INVERSE
EndFunc

Func Mult($X,$P_NUM,$M)
    Local $Y = 1
    While $P_NUM > 0
      While ($P_NUM/2) = Int(($P_NUM/2))
            $X = NMod(($X*$X),$M)
            $P_NUM = $P_NUM/2
      WEnd
      $Y = NMod(($X*$Y),$M)
      $P_NUM = $P_NUM-1
    WEnd
    Return $Y
EndFunc

Func Encrypt($TEXT,$EE,$EN)
    Local $ENC = ""
    If $TEXT = "" Then Return ""
    For $INDEX = 1 To StringLen($TEXT)
      $ENC &= Mult(Asc(StringMid($TEXT,$INDEX,1)),$EE,$EN) & "+"
    Next
    Return StringTrimRight($ENC,1)
EndFunc

Func Decrypt($TEXT,$DD,$DN)
    Local $DEC = ""
    $SPLIT = StringSplit($TEXT,"+")
    For $INDEX = 1 To $SPLIT
      $TOK = Number($SPLIT[$INDEX])
      $DEC &= Chr(Mult($TOK,$DD,$DN))
    Next
    Return $DEC
EndFunc

Func NMod($X,$Y)
    $Z = $X - (Int($X/$Y)* $Y)
    Return $Z
EndFunc

guland 发表于 2010-2-1 12:01:08

OK了 ,谢谢,我去研究研究

chenzhi1210 发表于 2011-7-24 06:34:58

有没有实例呀?

chenzhi1210 发表于 2011-7-24 06:43:11

9楼的不能加密汉字。

netegg 发表于 2011-7-24 07:50:08

回复 12# chenzhi1210
汉字都没法加密,必须转码为unicode编码才行
页: [1]
查看完整版本: 关于RSA加密解密的问题